diff options
Diffstat (limited to 'src/stores/stateBin.ts')
| -rw-r--r-- | src/stores/stateBin.ts | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/stores/stateBin.ts b/src/stores/stateBin.ts index 74ea510f..082a07dc 100644 --- a/src/stores/stateBin.ts +++ b/src/stores/stateBin.ts @@ -1,17 +1,22 @@ import { browser } from '$app/environment'; import { writable, get, type Writable } from 'svelte/store'; +import localforage from 'localforage'; type StateBin = Record<string, unknown>; const STORAGE_KEY = 'stateBin'; -const initialState = browser ? JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') : {}; -const baseStore = writable<StateBin>(initialState); +const baseStore = writable<StateBin>({}); -if (browser) - baseStore.subscribe((val) => { - localStorage.setItem(STORAGE_KEY, JSON.stringify(val)); +if (browser) { + localforage.getItem<StateBin>(STORAGE_KEY).then((value) => { + if (value && typeof value === 'object') baseStore.set(value); }); + baseStore.subscribe((value) => { + localforage.setItem(STORAGE_KEY, value); + }); +} + const createProxyStore = (store: Writable<StateBin>) => { return new Proxy(store, { get(target, prop: string) { |