Store's user-supplied unsubscribe function not called on unsubscribe
See original GitHub issueDescribe the bug
If a store is created with a function as a second argument, that function is called when the subscriber count goes from zero to one. In theory, the function returned from that function is called when the subscriber count goes from one to zero. It doesn’t.
To Reproduce
https://svelte.dev/repl/77e4730d8b744fa4ab047a8354b9aedd?version=3.12.1
Expected behavior
When visible
becomes false, an alert should appear.
Severity
It’s not great, though in practice it probably only means the occasional memory leak.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Unsubscribe in subscribe callback function? - angular
Most of the time I'm just wanting the ONE response from my backend. And then I'll want to unsubscribe. So why not call...
Read more >To unsubscribe, or not to unsubscribe, that is the question.
One of the ways is to call .unsubscribe() on the Subscription we want to close. ... This function is passed to the new...
Read more >Unsubscribe - Amazon Simple Notification Service
If the Unsubscribe call does not require authentication and the requester is not the subscription owner, a final cancellation message is delivered to...
Read more >Purchasing - Rivian
Reserve without stepping into a dealership. It's easy and intuitive to reserve your Rivian online. When it comes time to buy, complete your...
Read more >6 Ways to Unsubscribe from Observables in Angular
If a subscription is not closed the function callback attached to it will ... We do so by calling the unsubscribe method in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi Rich, there’s a double subscribe in your REPL which is causing the issue.
Looking at the debugger, the auto-subscribe fires first, which drops into this code correctly:
However, you have another subscribe inside that
start
which bumps thesubscribers
length to 2, which causes this code fragment to be skipped ondestroy_component
:Here’s the debugger state:
Right before the component context is wiped out, we can see the store still has a subscriber with the proper
stop
function stored in it.I can see the usefulness of protecting against accidental repeated subscribes, though we’d have to add some tracking information so we tell if all subscriptions originate from same component. If they all have the same origin then we’d need to call the
stop()
function regardless of subscription count.I don’t think there’s a bug here. As others have noted, the on-first-subscriber function is adding its own first subscriber, which is not being removed until the on-removal-of-last-subscriber function is called, which will be never, as there’s still that extra subscriber hanging. Without that extra subscription, this works fine.