UNPKG

svelte

Version:

Cybernetically enhanced web apps

37 lines (31 loc) 913 B
/** @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; }