Created
June 28, 2023 19:28
-
-
Save Aurumaker72/c0a3bd78c717ff09a2418e7b1f94f45f to your computer and use it in GitHub Desktop.
Svelte LocalStorageStore
This file contains hidden or 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 {Writable} from "svelte/store"; | |
| import {writable} from "svelte/store"; | |
| export function lsWritable<T>(path: string, default_value: T): Writable<T> { | |
| const store = writable<T>(localStorage.getItem(path) ? JSON.parse(localStorage.getItem(path)) as T : default_value); | |
| store.subscribe(x => { | |
| localStorage.setItem(path, JSON.stringify(x)); | |
| }); | |
| return store; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment