Created
December 21, 2023 15:53
-
-
Save colelawrence/40ae8f20c62c32eb729b22257b563436 to your computer and use it in GitHub Desktop.
Synchronously read a svelte 3/4 store / Readable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Readable } from "svelte/store"; | |
const UNSET = Symbol(); | |
export function read<T>(store: Readable<T>): T { | |
let value: T | typeof UNSET = UNSET; | |
store.subscribe((v) => (value = v))(); | |
if (UNSET === value) { | |
throw new Error("Readable did not react synchronously"); | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment