|
| 1 | +import { get, tick, untrack } from '../internal/client/runtime.js'; |
| 2 | +import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js'; |
| 3 | +import { source } from '../internal/client/reactivity/sources.js'; |
| 4 | +import { increment } from './utils.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Returns a `subscribe` function that, if called in an effect (including expressions in the template), |
| 8 | + * calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs. |
| 9 | + * |
| 10 | + * If `start` returns a function, it will be called when the effect is destroyed. |
| 11 | + * |
| 12 | + * If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects |
| 13 | + * are active, and the returned teardown function will only be called when all effects are destroyed. |
| 14 | + * |
| 15 | + * It's best understood with an example. Here's an implementation of [`MediaQuery`](https://svelte.dev/docs/svelte/svelte-reactivity#MediaQuery): |
| 16 | + * |
| 17 | + * ```js |
| 18 | + * import { createSubscriber } from 'svelte/reactivity'; |
| 19 | + * import { on } from 'svelte/events'; |
| 20 | + * |
| 21 | + * export class MediaQuery { |
| 22 | + * #query; |
| 23 | + * #subscribe; |
| 24 | + * |
| 25 | + * constructor(query) { |
| 26 | + * this.#query = window.matchMedia(`(${query})`); |
| 27 | + * |
| 28 | + * this.#subscribe = createSubscriber((update) => { |
| 29 | + * // when the `change` event occurs, re-run any effects that read `this.current` |
| 30 | + * const off = on(this.#query, 'change', update); |
| 31 | + * |
| 32 | + * // stop listening when all the effects are destroyed |
| 33 | + * return () => off(); |
| 34 | + * }); |
| 35 | + * } |
| 36 | + * |
| 37 | + * get current() { |
| 38 | + * this.#subscribe(); |
| 39 | + * |
| 40 | + * // Return the current state of the query, whether or not we're in an effect |
| 41 | + * return this.#query.matches; |
| 42 | + * } |
| 43 | + * } |
| 44 | + * ``` |
| 45 | + * @param {(update: () => void) => (() => void) | void} start |
| 46 | + * @since 5.7.0 |
| 47 | + */ |
| 48 | +export function createSubscriber(start) { |
| 49 | + let subscribers = 0; |
| 50 | + let version = source(0); |
| 51 | + /** @type {(() => void) | void} */ |
| 52 | + let stop; |
| 53 | + |
| 54 | + return () => { |
| 55 | + if (effect_tracking()) { |
| 56 | + get(version); |
| 57 | + |
| 58 | + render_effect(() => { |
| 59 | + if (subscribers === 0) { |
| 60 | + stop = untrack(() => start(() => increment(version))); |
| 61 | + } |
| 62 | + |
| 63 | + subscribers += 1; |
| 64 | + |
| 65 | + return () => { |
| 66 | + tick().then(() => { |
| 67 | + // Only count down after timeout, else we would reach 0 before our own render effect reruns, |
| 68 | + // but reach 1 again when the tick callback of the prior teardown runs. That would mean we |
| 69 | + // re-subcribe unnecessarily and create a memory leak because the old subscription is never cleaned up. |
| 70 | + subscribers -= 1; |
| 71 | + |
| 72 | + if (subscribers === 0) { |
| 73 | + stop?.(); |
| 74 | + stop = undefined; |
| 75 | + } |
| 76 | + }); |
| 77 | + }; |
| 78 | + }); |
| 79 | + } |
| 80 | + }; |
| 81 | +} |
0 commit comments