diff options
| author | Fuwn <[email protected]> | 2025-06-11 23:32:39 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-06-11 23:32:39 -0700 |
| commit | b288ea46b769f407b25e18586dbd2fb36626cfb3 (patch) | |
| tree | 0747708aa27eafacdf93427f8eb2b7ad78ae428d /src/lib | |
| parent | fix(stores): Move anime and manga from localStorage to IndexedDB (diff) | |
| download | due.moe-b288ea46b769f407b25e18586dbd2fb36626cfb3.tar.xz due.moe-b288ea46b769f407b25e18586dbd2fb36626cfb3.zip | |
refactor(stores): Generic persistent storage facility
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Utility/persistentStore.ts | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/lib/Utility/persistentStore.ts b/src/lib/Utility/persistentStore.ts new file mode 100644 index 00000000..b08c825c --- /dev/null +++ b/src/lib/Utility/persistentStore.ts @@ -0,0 +1,19 @@ +import { writable, type Writable } from 'svelte/store'; +import { browser } from '$app/environment'; + +export const persistentStore = <T>(key: string, initial: T): Writable<T> => { + const store = writable<T>(initial); + + if (browser) + import('localforage').then((localforage) => { + localforage.default.getItem<T>(key).then((value) => { + if (value !== null) store.set(value); + }); + + store.subscribe((value) => { + localforage.default.setItem(key, value); + }); + }); + + return store; +}; |