Replay
ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items
See Also
Language-Specific Information
RxGroovy replay cache
RxJava 1․x cache replay
RxJS replay shareReplay
There is also a shareReplay
operator, which keeps track of the number of observers, and disconnects from the source Observable when that number drops to zero. shareReplay
takes three optional parameters and returns an ordinary Observable:
bufferSize
- the maximum number of items to buffer and replay to subsequent observers
window
- the age, in milliseconds, at which items in this buffer may be discarded without being emitted to subsequent observers
scheduler
- the Scheduler on which this operator will operate
Sample Code
var interval = Rx.Observable.interval(1000); var source = interval .take(4) .doAction(function (x) { console.log('Side effect'); }); var published = source .shareReplay(3); published.subscribe(createObserver('SourceA')); published.subscribe(createObserver('SourceB')); // Creating a third subscription after the previous two subscriptions have // completed. Notice that no side effects result from this subscription, // because the notifications are cached and replayed. Rx.Observable .return(true) .delay(6000) .flatMap(published) .subscribe(createObserver('SourceC')); function createObserver(tag) { return Rx.Observer.create( function (x) { console.log('Next: ' + tag + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); }); }
Side effect Next: SourceA0 Next: SourceB0 Side effect Next: SourceA1 Next: SourceB1 Side effect Next: SourceA2 Next: SourceB2 Side effect Next: SourceA3 Next: SourceB3 Completed Completed Next: SourceC1 Next: SourceC2 Next: SourceC3 Completed
replay
and shareReplay
are found in the following distributions:
rx.all.js
rx.all.compat.js
-
rx.binding.js
(requiresrx.js
orrx.compat.js
) rx.lite.js
rx.lite.compat.js
RxPHP replay shareReplay
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/replay.html