| 10 | * @returns {() => void} |
| 11 | */ |
| 12 | export function subscribe_to_store(store, run, invalidate) { |
| 13 | if (store == null) { |
| 14 | // @ts-expect-error |
| 15 | run(undefined); |
| 16 | |
| 17 | // @ts-expect-error |
| 18 | if (invalidate) invalidate(undefined); |
| 19 | |
| 20 | return noop; |
| 21 | } |
| 22 | |
| 23 | // Svelte store takes a private second argument |
| 24 | // StartStopNotifier could mutate state, and we want to silence the corresponding validation error |
| 25 | const unsub = untrack(() => |
| 26 | store.subscribe( |
| 27 | run, |
| 28 | // @ts-expect-error |
| 29 | invalidate |
| 30 | ) |
| 31 | ); |
| 32 | |
| 33 | // Also support RxJS |
| 34 | // @ts-expect-error TODO fix this in the types? |
| 35 | return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub; |
| 36 | } |