Skip to content

docs: tweak createSubscriber explanation #16398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/svelte/src/reactivity/create-subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { DEV } from 'esm-env';
import { queue_micro_task } from '../internal/client/dom/task.js';

/**
* Returns a `subscribe` function that, if called in an effect (including expressions in the template),
* calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
* Returns a `subscribe` function that integrates external event-based systems with Svelte's reactivity.
* It's particularly useful for integrating with web APIs like `MediaQuery`, `IntersectionObserver`, or `WebSocket`.
*
* If `start` returns a function, it will be called when the effect is destroyed.
* If `subscribe` is called inside an effect (including indirectly, for example inside a getter),
* the `start` callback will be called with an `update` function. Whenever `update` is called, the effect re-runs.
*
* If `start` returns a cleanup function, it will be called when the effect is destroyed.
*
* If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
* are active, and the returned teardown function will only be called when all effects are destroyed.
Expand Down Expand Up @@ -37,6 +40,7 @@ import { queue_micro_task } from '../internal/client/dom/task.js';
* }
*
* get current() {
* // This makes the getter reactive, if read in an effect
* this.#subscribe();
*
* // Return the current state of the query, whether or not we're in an effect
Expand Down
10 changes: 7 additions & 3 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2335,10 +2335,13 @@ declare module 'svelte/reactivity' {
constructor(query: string, fallback?: boolean | undefined);
}
/**
* Returns a `subscribe` function that, if called in an effect (including expressions in the template),
* calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
* Returns a `subscribe` function that integrates external event-based systems with Svelte's reactivity.
* It's particularly useful for integrating with web APIs like `MediaQuery`, `IntersectionObserver`, or `WebSocket`.
*
* If `start` returns a function, it will be called when the effect is destroyed.
* If `subscribe` is called inside an effect (including indirectly, for example inside a getter),
* the `start` callback will be called with an `update` function. Whenever `update` is called, the effect re-runs.
*
* If `start` returns a cleanup function, it will be called when the effect is destroyed.
*
* If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
* are active, and the returned teardown function will only be called when all effects are destroyed.
Expand Down Expand Up @@ -2366,6 +2369,7 @@ declare module 'svelte/reactivity' {
* }
*
* get current() {
* // This makes the getter reactive, if read in an effect
* this.#subscribe();
*
* // Return the current state of the query, whether or not we're in an effect
Expand Down
Loading