Skip to content

Commit c11c5ec

Browse files
authored
docs: tweak createSubscriber explanation (#16398)
* docs: tweak createSubscriber explanation * regenerate
1 parent 58788db commit c11c5ec

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { DEV } from 'esm-env';
66
import { queue_micro_task } from '../internal/client/dom/task.js';
77

88
/**
9-
* Returns a `subscribe` function that, if called in an effect (including expressions in the template),
10-
* calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
9+
* Returns a `subscribe` function that integrates external event-based systems with Svelte's reactivity.
10+
* It's particularly useful for integrating with web APIs like `MediaQuery`, `IntersectionObserver`, or `WebSocket`.
1111
*
12-
* If `start` returns a function, it will be called when the effect is destroyed.
12+
* If `subscribe` is called inside an effect (including indirectly, for example inside a getter),
13+
* the `start` callback will be called with an `update` function. Whenever `update` is called, the effect re-runs.
14+
*
15+
* If `start` returns a cleanup function, it will be called when the effect is destroyed.
1316
*
1417
* If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
1518
* are active, and the returned teardown function will only be called when all effects are destroyed.
@@ -37,6 +40,7 @@ import { queue_micro_task } from '../internal/client/dom/task.js';
3740
* }
3841
*
3942
* get current() {
43+
* // This makes the getter reactive, if read in an effect
4044
* this.#subscribe();
4145
*
4246
* // Return the current state of the query, whether or not we're in an effect

packages/svelte/types/index.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,10 +2335,13 @@ declare module 'svelte/reactivity' {
23352335
constructor(query: string, fallback?: boolean | undefined);
23362336
}
23372337
/**
2338-
* Returns a `subscribe` function that, if called in an effect (including expressions in the template),
2339-
* calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
2338+
* Returns a `subscribe` function that integrates external event-based systems with Svelte's reactivity.
2339+
* It's particularly useful for integrating with web APIs like `MediaQuery`, `IntersectionObserver`, or `WebSocket`.
23402340
*
2341-
* If `start` returns a function, it will be called when the effect is destroyed.
2341+
* If `subscribe` is called inside an effect (including indirectly, for example inside a getter),
2342+
* the `start` callback will be called with an `update` function. Whenever `update` is called, the effect re-runs.
2343+
*
2344+
* If `start` returns a cleanup function, it will be called when the effect is destroyed.
23422345
*
23432346
* If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
23442347
* are active, and the returned teardown function will only be called when all effects are destroyed.
@@ -2366,6 +2369,7 @@ declare module 'svelte/reactivity' {
23662369
* }
23672370
*
23682371
* get current() {
2372+
* // This makes the getter reactive, if read in an effect
23692373
* this.#subscribe();
23702374
*
23712375
* // Return the current state of the query, whether or not we're in an effect

0 commit comments

Comments
 (0)