@@ -1776,36 +1776,41 @@ declare module 'svelte/reactivity' {
17761776 #private;
17771777 }
17781778 /**
1779- * Returns a function that, when invoked in a reactive context, calls the `start` function once,
1780- * and calls the `stop` function returned from `start` when all reactive contexts it's called in
1781- * are destroyed. This is useful for creating a notifier that starts and stops when the
1782- * "subscriber" count goes from 0 to 1 and back to 0. Call the `update` function passed to the
1783- * `start` function to notify subscribers of an update.
1779+ * Returns a `subscribe` function that, if called in an effect (including expressions in the template),
1780+ * calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
17841781 *
1785- * Usage example (reimplementing `MediaQuery`):
1782+ * If `start` returns a function, it will be called when the effect is destroyed.
1783+ *
1784+ * If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
1785+ * are active, and the returned teardown function will only be called when all effects are destroyed.
1786+ *
1787+ * It's best understood with an example. Here's an implementation of [`MediaQuery`](https://svelte.dev/docs/svelte/svelte-reactivity#MediaQuery):
17861788 *
17871789 * ```js
1788- * import { createSubscriber, on } from 'svelte/reactivity';
1790+ * import { createSubscriber } from 'svelte/reactivity';
1791+ * import { on } from 'svelte/events';
17891792 *
17901793 * export class MediaQuery {
17911794 * #query;
1792- * #subscribe = createSubscriber((update) => {
1793- * // add an event listener to update all subscribers when the match changes
1794- * return on(this.#query, 'change', update);
1795- * });
1795+ * #subscribe;
1796+ *
1797+ * constructor(query) {
1798+ * this.#query = window.matchMedia(`(${query})`);
1799+ *
1800+ * this.#subscribe = createSubscriber((update) => {
1801+ * // when the `change` event occurs, re-run any effects that read `this.current`
1802+ * const off = on(this.#query, 'change', update);
1803+ *
1804+ * // stop listening when all the effects are destroyed
1805+ * return () => off();
1806+ * });
1807+ * }
17961808 *
17971809 * get current() {
1798- * // If the `current` property is accessed in a reactive context, start a new
1799- * // subscription if there isn't one already. The subscription will under the
1800- * // hood ensure that whatever reactive context reads `current` will rerun when
1801- * // the match changes
18021810 * this.#subscribe();
1803- * // Return the current state of the query
1804- * return this.#query.matches;
1805- * }
18061811 *
1807- * constructor( query) {
1808- * this.#query = window.matchMedia(`(${query})`) ;
1812+ * // Return the current state of the query, whether or not we're in an effect
1813+ * return this.#query.matches ;
18091814 * }
18101815 * }
18111816 * ```
0 commit comments