Consider the following code snippet
const interval$ = Rx.Observable.interval(1000).share();
interval$.subscribe(x=> console.log('a=' + a));
setTimeout(() => {
interval$.subscribe(x=> console.log('b=' + b));
}, 1500);
I need the late subscriber to also return the last emitted value (which is 0 in this case). How can this be done with RxJS (Note I need a shared observable)
To create a shared observable that replays the last emitted value to late subscribers, replace:
const interval$ = Rx.Observable
.interval(1000)
.share();
with:
const interval$ = Rx.Observable
.interval(1000)
.publishReplay(1)
.refCount();
share
is similar to .publish().refCount()
, so if you want to replay the last emitted value, publishReplay
is what you could use instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.