Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refCount with delay #4033

Closed
OliverJAsh opened this issue Aug 17, 2018 · 4 comments · Fixed by #6169
Closed

refCount with delay #4033

OliverJAsh opened this issue Aug 17, 2018 · 4 comments · Fixed by #6169
Assignees
Labels
7.x Issues and PRs for version 7.x

Comments

@OliverJAsh
Copy link
Contributor

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

@aaronjensen
Copy link

Thank you for writing this up. I ran into the same thing. For now, I'm actually memoizing the slow operation (it was an http call) and allowing the stream to reset. It's not perfect, but it seems to work in my case.

The operator (or flag to switchMap) you're proposing here would be great.

@OliverJAsh
Copy link
Contributor Author

@aaronjensen IIRC, I ended up working with @cartant on a combineLatestHigherOrderArray operator, which felt like a better solution at the time: https://github.com/cartant/rxjs-etc/blob/master/source/observable/combineLatestHigherOrderArray-spec.ts. That might help in your case too!

@aaronjensen
Copy link

That's helpful, thank you @OliverJAsh. It looks like that package has refCountDelay as well and some other handy operators. I'll try it out.

@benlesh
Copy link
Member

benlesh commented Feb 20, 2021

This has come up a few times, and now that we've revamped the share-type operators in 7.x it might be time to have a look to see if we can accommodate this with that new API somehow.

Off the top of my head, maybe something like (not at all set on this, it requires some thought):

source$.pipe(
  share({
    resetOnRefCountZero: () => timer(1000) // Where it could be a boolean or a reset timer factory? 
  });
)

I'll add this to agenda items.

@benlesh benlesh added the AGENDA ITEM Flagged for discussion at core team meetings label Feb 20, 2021
@benlesh benlesh self-assigned this Feb 24, 2021
@benlesh benlesh added 7.x Issues and PRs for version 7.x and removed AGENDA ITEM Flagged for discussion at core team meetings labels May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
7.x Issues and PRs for version 7.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants