Background
Using Typescript and RxJS, I'm trying to extend a behavior subject so that it takes an observable as a construction parameter and then subscribes to it so that each value from the observable gets set as the value of the behavior subject.
Question
How do I make it so that the behavior subject is only subscribed to the inner observable when the behavior subject itself has at least one subscriber?
Code
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
export class SubjectWithInnerObservable<T> extends BehaviorSubject<T> {
constructor(
initialState: any,
someObservable: Observable<T>
) {
super(initialState);
// I only want this subscription to exist when there are one or more subscribers to the behavior subject
someObservable.subscribe(this);
}
}
BehaviorSubject
not extend it. – Richard Matsenis only subscribed ... when ... has at least one subscriber
sounds likerefCount
. – Richard Matsen