diff options
| author | Fuwn <[email protected]> | 2026-03-28 06:26:19 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-28 06:26:19 +0000 |
| commit | c1475d4adb535b374cbf4a4aababd95986cfbabb (patch) | |
| tree | f80d2ef2c839778529b31dd0d625efa18e52cfe9 /src | |
| parent | fix(notifications): stabilize browser subscription identity (diff) | |
| download | due.moe-c1475d4adb535b374cbf4a4aababd95986cfbabb.tar.xz due.moe-c1475d4adb535b374cbf4a4aababd95986cfbabb.zip | |
fix(cache): keep pre-hydration state changes
Diffstat (limited to 'src')
| -rw-r--r-- | src/stores/identity.ts | 14 | ||||
| -rw-r--r-- | src/stores/stateBin.ts | 14 |
2 files changed, 26 insertions, 2 deletions
diff --git a/src/stores/identity.ts b/src/stores/identity.ts index 52e0cba7..abe0b40d 100644 --- a/src/stores/identity.ts +++ b/src/stores/identity.ts @@ -13,16 +13,28 @@ const createStore = () => { const store = writable<UserIdentity>(defaultIdentity); let state: UserIdentity = defaultIdentity; let hydrated = !browser; + let changedBeforeHydration = false; + let initialEmission = true; + let applyingStoredValue = false; store.subscribe((value) => { state = value; + if (browser && !hydrated && !initialEmission && !applyingStoredValue) + changedBeforeHydration = true; + if (browser && hydrated) localforage.setItem("identity", value); + + initialEmission = false; }); if (browser) localforage.getItem<UserIdentity>("identity").then(async (value) => { - if (value && typeof value === "object") store.set(value); + if (value && typeof value === "object" && !changedBeforeHydration) { + applyingStoredValue = true; + store.set(value); + applyingStoredValue = false; + } hydrated = true; diff --git a/src/stores/stateBin.ts b/src/stores/stateBin.ts index 1d3f5dee..ca61ed77 100644 --- a/src/stores/stateBin.ts +++ b/src/stores/stateBin.ts @@ -15,10 +15,17 @@ const STORAGE_KEY = "stateBin"; const baseStore = writable<StateBin>({}); let hydrated = !browser; let state: StateBin = {}; +let changedBeforeHydration = false; +let initialEmission = true; +let applyingStoredValue = false; if (browser) { localforage.getItem<StateBin>(STORAGE_KEY).then((value) => { - if (value && typeof value === "object") baseStore.set(value); + if (value && typeof value === "object" && !changedBeforeHydration) { + applyingStoredValue = true; + baseStore.set(value); + applyingStoredValue = false; + } hydrated = true; @@ -28,7 +35,12 @@ if (browser) { baseStore.subscribe((value) => { state = value; + if (browser && !hydrated && !initialEmission && !applyingStoredValue) + changedBeforeHydration = true; + if (hydrated) localforage.setItem(STORAGE_KEY, value); + + initialEmission = false; }); } |