Skip to content
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

allow derivers to return cleanup functions #2730

Merged
merged 2 commits into from
May 12, 2019
Merged

allow derivers to return cleanup functions #2730

merged 2 commits into from
May 12, 2019

Conversation

Rich-Harris
Copy link
Member

fixes #2553. This makes it trivial to do things like this...

import { writable, derived } from 'svelte/store';

const hz = writable(60);

const heartbeat = derived(hz, ($hz, set) => {
  const interval = setInterval(() => {
    set(Date.now());
  }, 1000 / $hz);

  return () => clearInterval(interval);
});

...or, more practically, poll a URL for data where the variables can change. It doesn't attempt to fix your race conditions, but that's easy enough to do in userland.

@Rich-Harris
Copy link
Member Author

A possible addition to this would be add a final argument to the cleanup function, so that if you (say) had a subscription manager of some kind that you only wanted to destroy when the store was no longer needed, you could do if (final) manager.destroy() or whatever. Is that worth including?

@Rich-Harris Rich-Harris merged commit 644b8a7 into master May 12, 2019
@Rich-Harris Rich-Harris deleted the gh-2553 branch May 12, 2019 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No stop functionality in derived stores
1 participant