All files / src/store utils.js

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

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 32 332x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 663x 12x 12x 12x 12x 12x 12x 12x 12x 651x 651x 651x 651x 651x 651x 651x 651x 651x 651x 663x 663x  
/** @import { Readable } from './public' */
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
	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;
}