-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Feature Request
Is your feature request related to a problem? Please describe.
I have a Observable<Array<Observable<T>>> which I want to map to Observable<Array<T>>.
When a new array is emitted, the inner observables should unsubscribe/subscribe as follows:
- If
Observableexists in previous array and the new/current array, retain pre-existing subscription - If
Observabledid not exist in previous array but does exist in new/current array, create new subscription - If
Observableexisted in previous array but does not exist in new/current array, unsubscribe from pre-existing subscription
I hoped to achieve this using switchMap on the outer observable and then passing Array<Observable<T>> into combineLatest. However, switchMap will unsubscribe from its previous inner Observable before subscribing to the new inner Observable, which means inner subscriptions are not retained as desired.
Example:
- https://stackblitz.com/edit/typescript-b4wgr1
- https://stackoverflow.com/questions/51875557/combinelatest-switchmap-and-retaining-inner-subscriptions/51901191
Describe the solution you'd like
To offset the fact that switchMap unsubscribes to the previous inner observable before subscribing to the new inner observable, I would like the refCounting to be delayed, thereby ensuring that subscriptions to the inner observables are retained.
This is the solution suggested in this article. An alternative refCount with delay operator can be found here.
Describe alternatives you've considered
(If this is new operator request) describe reason it should be core operator
From my research this seems like something people want but it's not easy to do. I discovered issues/articles/examples on the internet, but there doesn't seem to be anything in the core to help with this.
Additional context
- Related discussion: Proposal: attack and release times for refCount #171
- Issue which needed this: https://stackoverflow.com/questions/51875557/combinelatest-switchmap-and-retaining-inner-subscriptions/51901191#51901191
- Example:
refCountWithDelayimplementation: https://gist.github.com/marinho/3637210b13c0f298e1692a0b7b104e64 - Example:
delayedRefCountimplementation: https://medium.com/@volkeron/rxjs-unsubscribe-delay-218a9ab2672e