-
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
Does shareReplay's teardown function get called? #3127
Comments
The teardown logic is called every time an observer unsubscribes. Just this small part gets called only if the source Observable completed https://github.com/ReactiveX/rxjs/blob/master/src/operators/shareReplay.ts#L47. |
Thanks for the quick response. Can you elaborate — for My understanding is that the teardown function needs to get attached to a Subscriber in order for it to be called during an unsubscribe. That attachment happens during /* L200 */ if (operator) {
/* L201 */ operator.call(sink, this.source);
/* L202 */ } else {
/* L203 */ sink.add(this.source ? this._subscribe(sink) : this._trySubscribe(sink));
/* L204 */ } When we use Many other operators instantiate a Subscriber object, which takes care of attaching the teardown function in its constructor, but |
Hmm, I think you might be right. This line https://github.com/ReactiveX/rxjs/blob/master/src/operators/shareReplay.ts#L43 should be probably this:
instead of this:
|
Actually, as the subject.subscribe(this);
this.add(() => {
refCount--;
if (subscription && refCount === 0 && isComplete) {
subscription.unsubscribe();
}
}); I was tempted to write a PR for this, but with everything of interest in a closure, I can't see how to write an initial, failing test. It's also awkward as the unsubscription from the source can only occur if the source has completed - at which time the unsubscription is redundant. As far as I can see, the |
I think this demo shows not exactly the problem described here but it's not would I expect to happen:
This prints the following output:
I'd expect that I understand this was the point of of PR #2910 but it requires me to always complete the chain before using |
Is there any resolution on this issue? I've bumped into exactly the same thing yesterday and was wondering why it doesn't work... |
@martinsik My two cents: I think it's all down to However, I, too, would be interesting in knowing whether the problem you've highlighted was considered when PR #2910 was made. The change in behaviour was a surprise to me. |
The refCount is still relevant for subscribing the inner observable, no? So I don't think it's entirely ignored. I actually came to find that what I was looking for in #3238 is exactly the new behavior of shareReplay*: connect on subscribe, cache values even if the refCount goes to 0, but make the observable retryable in case of errors. In my case this is exactly how caching is supposed to work because I want to cache values even if there are no subscribers for a little while. *) Going off of the descriptions, I haven't yet tested it. |
FYI, in my case I complete the chain by using takeUntil with an observable that emits when the cache should be torn down. |
@Airblader As far as I can see, |
I hadn't actually checked the source yet and just assumed the refCount was also used to ensure connecting on the first subscription (since
Point taken and agreed. :-) |
Closing this in favour of #3336. |
RxJS version:
5.5.2
Code to reproduce:
This is more of a question so I don't have a reproducible example.
Expected behavior:
Same as above.
Actual behavior:
Same as above.
Additional information:
At first, I encountered what I thought was a bug with the
shareReplay
operator, but after finding an older issue that brought up the same concerns, I discovered that it was a misunderstanding on my part about howshareReplay
was supposed to work.However, in the process, I dug through some of the source code and now I have a question about the teardown logic in the
shareReplay
operator:https://github.com/ReactiveX/rxjs/blob/e159578eda80a96bb68b83418f503428cc23aa7f/src/operators/shareReplay.ts#L43...L49
Since the
shareReplay
operator does not create its ownSubscriber
, it looks like that teardown logic isn't getting attached to anything. Will the teardown logic ever get called?Thanks for all the hard work!
The text was updated successfully, but these errors were encountered: