-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(shareReplay): properly retains history on subscribe #2910
Conversation
When the refCount hits zero, and the source has neither completed nor errored, `shareReplay` will now properly stay subscribed to the source, and retain the internal ReplaySubject that is caching values fixes #2908
Generated by 🚫 dangerJS |
@benlesh
But now you did it like
Are you sure it is correct??? I think unsubscribing even without error and completion was the only difference between |
I absolutely think the new behavior is better. If you want the inner subscription to end, just complete the observable. This new behavior allows caching values even if there's no subscriber to the cache for a while. |
But why can't you use It was the only difference between then to close inner subject when there is no more subscribers And there was a technique to reuse shared observable So this change destroyed all described in these videos by @staltz |
Because it takes care of neither subscribing the inner observable when a subscription to the outer one is made, nor does it take care of destroying the inner connection on errors, but instead the error will be cached. |
The internal Subject is an implementation detail and shouldn't be considered when using publishReplay. If some other specific behavior is required with regard to Subjects, you can always roll your own operator and use the multicast operator inside of it. |
@benlesh Thanks we already did so |
publishReplay doesn't connect when the ref count goes from 0 to 1. However, publishReplay+refCount will unsubscribe from the source when the ref count goes to 0, while shareReplay doesn't. Also, publishReplay will cache errors. |
@Airblader thank you! |
I encountered this behavior today and I'm uncertain about whether it is good or not. The typical use I have for The following is what I usually want.
With the old behavior, What is the use case for the new behavior? For now I can revert to using |
In my case, caching a value even if all observers go away for a while. There's no reason to recompute the costly information just because momentarily all observers go away. Given that your usecase is solved with a single refCount I just really don't see the need for a change. The lack of documentation is a general issue, but there is a separate project to tackle this already. |
@lorenzodallavecchia can you file an issue with a minimal reproduction of what your problem is? Also maybe say in there what you are trying to accomplish? |
https://codepen.io/anon/pen/qxoNeV?editors=0012 |
The behavior in that codepen seems perfectly reasonable to me. The source interval is still emitting, so each of its emissions gets passed into the shared ReplaySubject, executing the side effect. The
So where's the problem if you have a working solution? |
The codepen posted by @mgrinko represents my use case too. To clarify, I agree that this works correctly according to the new design. With my comment here I wanted to point out that the change in the behavior of And that's basically it. The rest is just related issues/ideas, like having a "cold" equivalent to |
The problem is that in 5.4 version of RxJS worked differently https://codepen.io/anon/pen/zRWoMV?editors=0012 So now we have tons of code which relies on old behavior, |
@benlesh titled this a bugfix, so apparently this old behavior was never intended. It's the nature of relying on buggy behavior that a bugfix might break your usecase. The real problem, I guess, is that the docs weren't clear enough to state how it should've behaved. |
agree |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
When the refCount hits zero, and the source has neither completed nor errored,
shareReplay
will now properly stay subscribed to the source, and retain the internal ReplaySubject that is caching valuesfixes #2908