shareReplay not cleaning up subscriptions.
See original GitHub issueRxJS version: 5 or 6
Code to reproduce:
const source = interval(1000).pipe(
tap(() => console.log('tick'),
shareReplay(1),
);
const sub = source.subscribe(x => console.log(x));
sub.unsubscribe();
Expected behavior:
It should never log “tick”
Actual behavior:
It TOTALLY logs “tick”.
Additional information:
shareReplay
should definitely not recycle the underlying ReplaySubject when refCount drops to 0 due to unsubscription. However, it should end the subscription, which it’s currently not doing, because I’m a dolt.
It should be an easy fix.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:27
- Comments:26 (7 by maintainers)
Top Results From Across the Web
Be careful when using shareReplay
The shareReplay operator does not clean up streams when they have not yet completed. In our case, the interval$ keeps emitting values after ......
Read more >fixing a racecondition on rxjs subscription with shareReplay
The reason shareReplay isn't working for you is because you are applying it to the derived observable, rather than the source.
Read more >ShareReplay Doesn't Clear Its Replay Buffer
Here we'll demonstrate that the ShareReplay buffer is not cleared when the number of subscribers reaches zero:
Read more >using shareReplay as cache good or bad practice? : r/Angular2
As such, if you know an observable will complete then you do not need to worry about cleaning up any subscriptions.
Read more >RxJS: Avoiding memory leaks - Developer's Notes
First, we do not have any issue here because HttpClient from RxJS just ... The shareReplay operator does not clean up observables when...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
This workaround was given to me in gitter as I cried that my teardown was never run:
The
publishReplay(1).refCount()
above with a timer emitted a weird result when subscribed to with 0 refs (it initially gave the last result and then started fresh)@ksaldana1 You can use
publishReplay(1).refCount()
as an alternative for the time being.