diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index dfb270dc97..fe981896b9 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -324,13 +324,13 @@ const time = readable(new Date(), set => { store = derived(a, callback: (a: any) => any) ``` ```js -store = derived(a, callback: (a: any, set: (value: any) => void) => void, initial_value: any) +store = derived(a, callback: (a: any, set: (value: any) => void) => void | () => void, initial_value: any) ``` ```js store = derived([a, ...b], callback: ([a: any, ...b: any[]]) => any) ``` ```js -store = derived([a, ...b], callback: ([a: any, ...b: any[]], set: (value: any) => void) => void, initial_value: any) +store = derived([a, ...b], callback: ([a: any, ...b: any[]], set: (value: any) => void) => void | () => void, initial_value: any) ``` --- @@ -359,6 +359,20 @@ const delayed = derived(a, ($a, set) => { }, 'one moment...'); ``` +If you return a function from the callback, it will be called when a) the callback runs again, or b) the last subscriber unsubscribes: + +```js +import { derived } from 'svelte/store'; + +const tick = derived(frequency, ($frequency, set) => { + const interval = setInterval(() => set(Date.now()), 1000 / frequency); + + return () => { + clearInterval(interval); + } +}, 'one moment...'); +``` + --- In both cases, an array of arguments can be passed as the first argument instead of a single store.