Context: I've got many ConnectableObservable
s, almost all of which have a replay count of 1 or more. There are many observers of the observables subscribing and unsubscribing at any given time.
What I want: In many cases, when an observer subscribes to one of these observables, I don't care about the possible pre-existing emitted data that I'd receive because of the observable's data replay mechanism. The only data that the recently-subscribed observer is interested in, is data that is emitted after the moment of subscription.
const observable = Rx.Observable
.interval(100)
.take(4)
.publishReplay(3);
observable.connect();
Problem: As far as I can tell, when an observer subscribes to the observable, it has no way of knowing whether or not the data it observes was emitted before or after the moment of subscription.
observable.subscribe(x => console.log('observed', x));
setTimeout(() =>
observable.subscribe(y => console.log('delayed observed', y)),
400
);
The code above will output:
// => observed 0
// => observed 1
// => observed 2
// => delayed observed 0 **don't care**
// => delayed observed 1 **don't care**
// => delayed observed 2 **don't care**
// => observed 3
// => delayed observed 3
In this hypothetical situation, the delayed observer is only interested in data emitted after the moment of subscription; in this case, 3
.
I've scoured the RxJS 5 reference docs and can't seem to find a silver-bullet operator to accomplish what I'm after. Any ideas?