-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Could store.observer be automatically cancelled on component destruction? #1181
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
Comments
I'm not sure there's a good way to achieve this — when you call oncreate() {
const observer = this.store.observe('foo', ...);
this.on('destroy', observer.cancel);
} |
I was thinking it's possible maybe to monkeypatch the instance of |
Can we not do |
@ekhaled's suggestion should work, but doesn't (in Monkey-patching would be a bit gross — I think |
Fixed the bug with observing |
Perfect, thanks a lot! Observing $foo actually was the first thing I tried, but didn't see it in the docs. Glad it's supposed to work! |
sveltejs#1181) * docs: Added docs link for March 2025 What's New - server route resolution addition Added the docs link for consistency with other What's New changes and news. * Update apps/svelte.dev/content/blog/2025-03-01-whats-new-in-svelte-march-2025.md --------- Co-authored-by: Rich Harris <[email protected]>
When destroying a component, observers observing component state get cancelled automatically while observers on store stay alive.
This came as a surprise to me, with a bit of foot-gun potential. Would it make sense to autocancel store-observers, too?
Here is a slightly contrived REPL. Nested.html has observers spammig the console. The store.observer keeps logging after pressing the button.
The text was updated successfully, but these errors were encountered: