question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does shareReplay's teardown function get called?

See original GitHub issue

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 how shareReplay 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 own Subscriber, 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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
martinsikcommented, Jan 5, 2018

Is there any resolution on this issue? I’ve bumped into exactly the same thing yesterday and was wondering why it doesn’t work…

1reaction
cartantcommented, Nov 22, 2017

Actually, as the this context is already a Subscriber, the innerSub subscription seems unnecessary - the subscription to the ReplaySubject will be added to the Subscriber when this is passed to subscribe. Unsubscription from the subject will occur when the Subscriber is unsubscribed. So the implementation could be:

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 refCount is unnesessary. In fact, the entire teardown looks to be redundant, to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJs Error Handling: Complete Practical Guide
a success handler function, which is called each time that the stream emits a value; an error handler function, that gets called only...
Read more >
Set Up and Tear Down State in Your Tests - Apple Developer
When XCTest finishes running all the test methods and the test class completes, XCTest calls the XCTestCase tearDown() class method. Use this method...
Read more >
Observable | RxJS API Document - ReactiveX
Returns an Observable that mirrors the source Observable, but will call a specified function when the source terminates on complete or error.
Read more >
TearDown - NUnit Docs
The TearDown attribute is inherited from any base class. Therefore, if a base class has defined a TearDown method, that method will be...
Read more >
shareReplay - Learn RxJS
You generally want to use shareReplay when you have side-effects or taxing computations that you do not wish to be executed amongst multiple...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found