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

Proposal: new operator similar to shareReplay() #3722

Closed
voithos opened this issue May 21, 2018 · 1 comment
Closed

Proposal: new operator similar to shareReplay() #3722

voithos opened this issue May 21, 2018 · 1 comment

Comments

@voithos
Copy link

voithos commented May 21, 2018

This is related to the apparent current mis-behavior of shareReplay() as described in #3354, specifically my comment here, parts of which I have included below.


This issue proposes adding an operator similar to shareReplay(), but one which does not re-subscribe to the source Observable if a previous subscription had completed. As an example, consider an Observable that represents an HTTP request. Ideally:

  • It would be cold until subscribed to, because there's no need to execute the expensive operation (in this case, an HTTP request) unless something needs the result. So we cannot use connect().
  • After completing, it would not re-subscribe to the source but instead would use something like a ReplaySubject(1) to retain the last value (or multiple values, if it's chunked). Thus, publishReplay(1).refCount() or the old shareReplay(1) behavior isn't ideal because it re-subscribes every time the refCount goes from 0 -> 1.
  • If the source subscription gets cancelled before completion, then re-subscribing is ok since we never got a value anyway and thus can't use the underlying ReplaySubject.

This new operator would behave similarly to publishReplay(), refCount() or the old shareReplay() implementation, except it would just keep the ReplaySubject() around, even after subscribers have reached 0, if the source observable completes. This would allow unsubscribing as normal (which is a problem with the current shareReplay() implementation, see #3354), but would ensure that the source observable's emissions are reused if it completes.

Would something like this be desired / useful? From a cursory search, I haven't found any prior art for such an operator.

As for naming, some ideas include shareOnce(), shareReplayOnce(), safeShare().

Alternatively, it may (?) be possible to do this by having a specialized version of refCount() that can be used alongside a normal publishReplay(), but I haven't looked into this.

@benlesh
Copy link
Member

benlesh commented May 22, 2018

This issue proposes adding an operator similar to shareReplay(), but one which does not re-subscribe to the source Observable if a previous subscription had completed.

shareReplay doesn't resubscribe to the source if the source completes, it caches the values and the completion and replays them on new subscriptions.

Either way, I'd recommend implementing a user-land operator and publishing it in this case. If it becomes popular or widely used, we can adopt it into the core library.

@benlesh benlesh closed this as completed May 22, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants