Skip to content

refCount with delay #4033

@OliverJAsh

Description

@OliverJAsh

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 Observable exists in previous array and the new/current array, retain pre-existing subscription
  • If Observable did not exist in previous array but does exist in new/current array, create new subscription
  • If Observable existed 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:

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

Metadata

Metadata

Assignees

Labels

7.xIssues and PRs for version 7.x

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions