diff options
Diffstat (limited to 'src/stores.js')
-rw-r--r-- | src/stores.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/stores.js b/src/stores.js new file mode 100644 index 0000000..f203bfd --- /dev/null +++ b/src/stores.js @@ -0,0 +1,22 @@ +import { writable } from "svelte/store"; + +const localStorage = window.localStorage; + +export const level = localStorageStore(0); + +function localStorageStore(key) { + const item = localStorage.getItem(key); + const { subscribe, set } = writable(JSON.parse(item) || null); + + return { + subscribe, + set: (value) => { + localStorage.setItem(key, JSON.stringify(value)); + set(value); + }, + clear: () => { + localStorage.removeItem(key); + set(null); + }, + }; +} |