forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
36 lines (31 loc) · 913 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/** @import { Readable } from './public' */
import { untrack } from '../index-client.js';
import { noop } from '../internal/shared/utils.js';
/**
* @template T
* @param {Readable<T> | null | undefined} store
* @param {(value: T) => void} run
* @param {(value: T) => void} [invalidate]
* @returns {() => void}
*/
export function subscribe_to_store(store, run, invalidate) {
if (store == null) {
// @ts-expect-error
run(undefined);
// @ts-expect-error
if (invalidate) invalidate(undefined);
return noop;
}
// Svelte store takes a private second argument
// StartStopNotifier could mutate state, and we want to silence the corresponding validation error
const unsub = untrack(() =>
store.subscribe(
run,
// @ts-expect-error
invalidate
)
);
// Also support RxJS
// @ts-expect-error TODO fix this in the types?
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}