diff options
| author | Fuwn <[email protected]> | 2025-06-12 22:06:31 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-06-12 22:06:31 -0700 |
| commit | e8612618fb20f779ebe2e85edf32d71961d2f1d4 (patch) | |
| tree | fa8565afb8584bbf55f4f4d496c2c350a5a25210 /src/stores/lastPruneTimes.ts | |
| parent | refactor(List): Simplify get-set structure of stateBin usage (diff) | |
| download | due.moe-e8612618fb20f779ebe2e85edf32d71961d2f1d4.tar.xz due.moe-e8612618fb20f779ebe2e85edf32d71961d2f1d4.zip | |
feat: Move remaining localStorage usages to localforage
Diffstat (limited to 'src/stores/lastPruneTimes.ts')
| -rw-r--r-- | src/stores/lastPruneTimes.ts | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/src/stores/lastPruneTimes.ts b/src/stores/lastPruneTimes.ts index 9c4afc5b..212f2bc9 100644 --- a/src/stores/lastPruneTimes.ts +++ b/src/stores/lastPruneTimes.ts @@ -1,5 +1,6 @@ import { browser } from '$app/environment'; import { writable } from 'svelte/store'; +import localforage from 'localforage'; interface LastPruneTimes { anime: number; @@ -14,41 +15,42 @@ const defaultTimes: LastPruneTimes = { }; const createStore = () => { - const { subscribe, set, update } = writable<LastPruneTimes>( - JSON.parse( - browser - ? (localStorage.getItem('lastPruneTimes') ?? JSON.stringify(defaultTimes)) - : JSON.stringify(defaultTimes) - ) - ); - let state: LastPruneTimes; + const store = writable<LastPruneTimes>(defaultTimes); + let state: LastPruneTimes = defaultTimes; - subscribe((value) => (state = value)); + if (browser) + localforage.getItem<LastPruneTimes>('lastPruneTimes').then((value) => { + if (value && Object.keys(value).length === Object.keys(defaultTimes).length) store.set(value); + }); + + store.subscribe((value) => { + state = value; + + if (browser) localforage.setItem('lastPruneTimes', value); + }); return { - subscribe, - set, - update, - reset: () => set(defaultTimes), + subscribe: store.subscribe, + set: store.set, + update: store.update, + reset: () => store.set(defaultTimes), + get: () => { const keys = Object.keys(defaultTimes); - const lastPruneTimesKeys = Object.keys(state); + const stateKeys = Object.keys(state); - if (keys.length !== lastPruneTimesKeys.length) return defaultTimes; + if (keys.length !== stateKeys.length) return defaultTimes; - for (const key of keys) if (!lastPruneTimesKeys.includes(key)) return defaultTimes; + for (const key of keys) if (!stateKeys.includes(key)) return defaultTimes; return state; }, - setKey: (key: keyof LastPruneTimes, value: unknown) => - update((lastPruneTimes) => ({ ...lastPruneTimes, [key]: value })) + + setKey: (key: keyof LastPruneTimes, value: number) => + store.update((times) => ({ ...times, [key]: value })) }; }; const lastPruneTimes = createStore(); -lastPruneTimes.subscribe((value) => { - if (browser) localStorage.setItem('lastPruneTimes', JSON.stringify(value)); -}); - export default lastPruneTimes; |