-
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
shareReplay with refCount doesn't restart due to unsubscriptions if the source completes first #5455
Comments
What you are seeing is the expected behaviour. Once the source completes, that's it. The completion is replayed. If you don't want this behaviour, use |
Hi @cartant and thanks for your quick reply.
I really thought that this was a bug. Please notice that in the PR that I've opened all the existing tests are passing and I also added a new one to cover this case. One of the reasons why I thought that this was a bug is that currently there is a test for
I don't understand why that shouldn't be the case if the source has completed and all the subscribers have unsubscribed. I thought that was the point of using Also, I know how to create my own operator with this behavior, but is there a way that I can get this behavior using Thanks a lot for looking into this. |
Look at it this way: if it resubscribes to the source it is not replaying the notifications. |
I do understand what you are saying, but another way to look at it could be: when using I'm not saying that you are wrong. I'm just saying that's what I expected/wished and when I found a test that seemed to state that, then that lead me to think that this was a bug. I also thought that if I wanted the behavior that you are describing as the expected one, then I could use: source.pipe(publishReplay(1), refCount()) I thought that by having the Anyways, well, if it isn't a bug then 2 questions:
Thanks! |
Yes, definitely!
Yes: |
Thanks a lot! I'm going to get some rest now and I will send a PR with that test tomorrow. Once again: thanks a lot for being so responsive and supportive! |
Hi, this doesn't work for me, and at first, I was very confused on how this should work, but I think what I functionally want isn't really possible, or not that I can find or think of. So if you could provide some help to do the following: "the source observable must be subscribed once during multiple subscriptions, and re-subscribed when it drops to 0 and a new subscriber subscribes, and for new subscribers, use the last emitted value." I though that this is what the new So what operators can be used to get this behavior? Or is this not possible, and I have to make sure the observable never errors nor completes? I tried the following, but that doesn't reset when refCount hits 0, and the source observable had been completed.
When turning on the reset on complete, new subscribers will reset the source observable, even if there are still others subscribed, and that is unwanted behavior. Maybe I should also sketch the actual use-case for this wanted behavior to make it more clear. So for our web application, we have build a custom store with RxJS, where we have resources. A resource can be seen as a single data instance, like patient 123, and to get the data of this patient, we subscribe to its observable. We start those resource observables with |
Bug Report
Current Behavior
shareReplay
doesn't restart due to unsubscriptions whenrefCount
istrue
if the source completes before the subscribers.Reproduction
Expected behavior
I would expect to restart the subscription to the source after the
refCount
has gone down to zero.Environment
The text was updated successfully, but these errors were encountered: