Created
May 3, 2024 02:19
-
-
Save SpenceDiNicolantonio/098f6d30f68d893b4776f054d69bb591 to your computer and use it in GitHub Desktop.
Svelte 5 Store to State #svelte #store #typescript
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
// !! This is yet untested | |
import type { Writable } from 'svelte/store'; | |
export const toState = <T>(store: Writable<T>, initial: T | null = null) => { | |
let value = $state<T | null>(initial); | |
const stateful = { | |
get: () => value, | |
set: (newValue: T) => { | |
store.set(newValue); | |
value = newValue; | |
}, | |
subscribe: (run: (value: T) => void) => { | |
const unsubscribeStore = store.subscribe(run); | |
return () => unsubscribeStore(); | |
}, | |
}; | |
return stateful; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment