Skip to content

Commit 1f0ad0a

Browse files
committed
tweak docs
1 parent c3b3e14 commit 1f0ad0a

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

packages/svelte/src/reactivity/create-subscriber.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,41 @@ import { source } from '../internal/client/reactivity/sources.js';
44
import { increment } from './utils.js';
55

66
/**
7-
* Returns a function that, when invoked in a reactive context, calls the `start` function once,
8-
* and calls the `stop` function returned from `start` when all reactive contexts it's called in
9-
* are destroyed. This is useful for creating a notifier that starts and stops when the
10-
* "subscriber" count goes from 0 to 1 and back to 0. Call the `update` function passed to the
11-
* `start` function to notify subscribers of an update.
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.
129
*
13-
* Usage example (reimplementing `MediaQuery`):
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):
1416
*
1517
* ```js
1618
* import { createSubscriber } from 'svelte/reactivity';
1719
* import { on } from 'svelte/events';
1820
*
1921
* export class MediaQuery {
2022
* #query;
21-
* #subscribe = createSubscriber((update) => {
22-
* // add an event listener to update all subscribers when the match changes
23-
* return on(this.#query, 'change', update);
24-
* });
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+
* }
2536
*
2637
* get current() {
27-
* // If the `current` property is accessed in a reactive context, start a new
28-
* // subscription if there isn't one already. The subscription will under the
29-
* // hood ensure that whatever reactive context reads `current` will rerun when
30-
* // the match changes
3138
* this.#subscribe();
32-
* // Return the current state of the query
33-
* return this.#query.matches;
34-
* }
3539
*
36-
* constructor(query) {
37-
* this.#query = window.matchMedia(`(${query})`);
40+
* // Return the current state of the query, whether or not we're in an effect
41+
* return this.#query.matches;
3842
* }
3943
* }
4044
* ```

packages/svelte/types/index.d.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)