All files / src/store utils.js

100% Statements 32/32
85.71% Branches 6/7
100% Functions 1/1
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 322x 2x 2x 2x 2x 2x 2x 2x 2x 2x 667x 12x 12x 12x 12x 12x 12x 12x 12x 655x 655x 655x 655x 655x 655x 655x 655x 655x 655x 667x 667x  
import { noop } from '../internal/shared/utils.js';
 
/**
 * @template T
 * @param {import('./public').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
	const unsub = 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;
}