Replies: 1 comment 2 replies
-
You can use import { tick } from 'svelte';
export default function readable(initial_value, start) {
let value = $state(initial_value);
let subscribers = 0;
let stop = null;
return {
get value() {
if ($effect.active()) {
$effect(() => {
if (subscribers++ === 0) {
stop = start(fn => value = fn(value));
}
return () => {
tick().then(() => {
if (--subscribers === 0) {
stop?.();
stop = null;
}
});
};
});
}
return value;
}
};
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been playing around with Svelte 5 by porting a project over, and one thing I'm not really seeing a good way to handle cleanup of side effects. For example, here's some runes code that creates a constantly-updating
Date
:In this example, how could I call
clearInterval()
once this reactive state is no longer being observed?For reference, here is how you could do this with stores:
I am aware stores are not going anywhere in Svelte 5. But is there a way to do this with runes, or is the word to keep using stores if you have side effects you need to manage?
Beta Was this translation helpful? Give feedback.
All reactions