diff options
| author | Fuwn <[email protected]> | 2026-03-28 05:49:07 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-28 05:49:07 +0000 |
| commit | 669115227593f51a49415da7587481ccc63c48b0 (patch) | |
| tree | 5fa7b93d7b7fabe9a3adc9c3bad8603e6bdb9070 /src/lib/Utility/persistentStore.ts | |
| parent | feat(manga): allow forcing automatic refresh (diff) | |
| download | due.moe-669115227593f51a49415da7587481ccc63c48b0.tar.xz due.moe-669115227593f51a49415da7587481ccc63c48b0.zip | |
fix(cache): respect AniList media list recache windows
Diffstat (limited to 'src/lib/Utility/persistentStore.ts')
| -rw-r--r-- | src/lib/Utility/persistentStore.ts | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/lib/Utility/persistentStore.ts b/src/lib/Utility/persistentStore.ts index 7df3049f..a3837a5b 100644 --- a/src/lib/Utility/persistentStore.ts +++ b/src/lib/Utility/persistentStore.ts @@ -5,14 +5,23 @@ 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); - }); + import("localforage").then(async (localforage) => { + let hydrated = false; + let pendingValue = initial; store.subscribe((value) => { - localforage.default.setItem(key, value); + pendingValue = value; + + if (hydrated) localforage.default.setItem(key, value); }); + + const value = await localforage.default.getItem<T>(key); + + if (value !== null) store.set(value); + + hydrated = true; + + await localforage.default.setItem(key, pendingValue); }); return store; |