Skip to content

Commit 8088719

Browse files
committed
tweak, types
1 parent 6e2bc3e commit 8088719

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { effect_tracking, render_effect } from '../internal/client/reactivity/ef
66
* and calls the `stop` function returned from `start` when all reactive contexts it's called in
77
* are destroyed. This is useful for creating a notifier that starts and stops when the
88
* "subscriber" count goes from 0 to 1 and back to 0.
9-
* @param {() => () => void} start
9+
* @param {() => (() => void) | void} start
1010
*/
1111
export function createSubscriber(start) {
1212
let subscribers = 0;
13-
/** @type {() => void} */
13+
/** @type {(() => void) | void} */
1414
let stop;
1515

1616
return () => {
@@ -30,7 +30,8 @@ export function createSubscriber(start) {
3030
subscribers -= 1;
3131

3232
if (subscribers === 0) {
33-
stop();
33+
stop?.();
34+
stop = undefined;
3435
}
3536
});
3637
};

packages/svelte/src/reactivity/index-client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { SvelteMap } from './map.js';
44
export { SvelteURL } from './url.js';
55
export { SvelteURLSearchParams } from './url-search-params.js';
66
export { MediaQuery } from './media-query.js';
7+
export { createSubscriber } from './create-subscriber.js';

packages/svelte/src/reactivity/index-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export class MediaQuery {
1818
/**
1919
* @param {any} _
2020
*/
21-
export function createStartStopNotifier(_) {
21+
export function createSubscriber(_) {
2222
return () => {};
2323
}

packages/svelte/types/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,13 +1730,20 @@ declare module 'svelte/reactivity' {
17301730
*/
17311731
export class MediaQuery {
17321732
/**
1733-
* @param query A media query string (don't forget the braces)
1733+
* @param query A media query string
17341734
* @param matches Fallback value for the server
17351735
*/
17361736
constructor(query: string, matches?: boolean | undefined);
17371737
get current(): boolean;
17381738
#private;
17391739
}
1740+
/**
1741+
* Returns a function that, when invoked in a reactive context, calls the `start` function once,
1742+
* and calls the `stop` function returned from `start` when all reactive contexts it's called in
1743+
* are destroyed. This is useful for creating a notifier that starts and stops when the
1744+
* "subscriber" count goes from 0 to 1 and back to 0.
1745+
* */
1746+
export function createSubscriber(start: () => (() => void) | void): () => void;
17401747

17411748
export {};
17421749
}

0 commit comments

Comments
 (0)