From 886ffc0af89b2bf09c8a1af16648b00a629b584e Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Tue, 3 Sep 2024 17:12:48 +0800 Subject: [PATCH 1/3] fix: hydrated for indexedDB --- app/utils/indexedDB-storage.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/utils/indexedDB-storage.ts b/app/utils/indexedDB-storage.ts index 51ee92b8185..b80c84ce932 100644 --- a/app/utils/indexedDB-storage.ts +++ b/app/utils/indexedDB-storage.ts @@ -4,7 +4,12 @@ import { get, set, del, clear } from "idb-keyval"; class IndexedDBStorage implements StateStorage { public async getItem(name: string): Promise { try { - return (await get(name)) || localStorage.getItem(name); + const value = (await get(name)) || localStorage.getItem(name); + const _value = JSON.parse(value); + if (_value?.state) { + _value.state._hasHydrated = true; + } + return JSON.stringify(_value); } catch (error) { return localStorage.getItem(name); } @@ -12,6 +17,10 @@ class IndexedDBStorage implements StateStorage { public async setItem(name: string, value: string): Promise { try { + const _value = JSON.parse(value); + if (!_value?.state?._hasHydrated) { + return; + } await set(name, value); } catch (error) { localStorage.setItem(name, value); From ed9aae531e0191d8b7fcbe594e0dc4e6176450da Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Tue, 3 Sep 2024 20:29:01 +0800 Subject: [PATCH 2/3] fix: hydrated --- app/store/prompt.ts | 2 ++ app/utils/indexedDB-storage.ts | 6 +----- app/utils/store.ts | 11 +++++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/store/prompt.ts b/app/store/prompt.ts index a25cda5813a..29a97abba5f 100644 --- a/app/store/prompt.ts +++ b/app/store/prompt.ts @@ -179,6 +179,8 @@ export const usePromptStore = createPersistStore( res.en.length + res.cn.length + res.tw.length; SearchService.init(allPromptsForSearch, userPrompts); }); + + return () => state.setHasHydrated(true); }, }, ); diff --git a/app/utils/indexedDB-storage.ts b/app/utils/indexedDB-storage.ts index b80c84ce932..86b30477788 100644 --- a/app/utils/indexedDB-storage.ts +++ b/app/utils/indexedDB-storage.ts @@ -5,11 +5,7 @@ class IndexedDBStorage implements StateStorage { public async getItem(name: string): Promise { try { const value = (await get(name)) || localStorage.getItem(name); - const _value = JSON.parse(value); - if (_value?.state) { - _value.state._hasHydrated = true; - } - return JSON.stringify(_value); + return value; } catch (error) { return localStorage.getItem(name); } diff --git a/app/utils/store.ts b/app/utils/store.ts index 13bef6d5dfe..02c8139b0ad 100644 --- a/app/utils/store.ts +++ b/app/utils/store.ts @@ -14,9 +14,11 @@ type SecondParam = T extends ( type MakeUpdater = { lastUpdateTime: number; + _hasHydrated: boolean; markUpdate: () => void; update: Updater; + setHasHydrated: (state: boolean) => void; }; type SetStoreState = ( @@ -33,12 +35,18 @@ export function createPersistStore( persistOptions: SecondParam>>, ) { persistOptions.storage = createJSONStorage(() => indexedDBStorage); + persistOptions.onRehydrateStorage = persistOptions.onRehydrateStorage + ? persistOptions.onRehydrateStorage + : (state) => { + return () => state.setHasHydrated(true); + }; return create( persist( combine( { ...state, lastUpdateTime: 0, + _hasHydrated: false, }, (set, get) => { return { @@ -57,6 +65,9 @@ export function createPersistStore( lastUpdateTime: Date.now(), }); }, + setHasHydrated: (state: boolean) => { + set({ _hasHydrated: state } as Partial>); + }, } as M & MakeUpdater; }, ), From 53dcae9e9ce1230f2f651b39f8f44330fe5408a4 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Wed, 4 Sep 2024 13:00:18 +0800 Subject: [PATCH 3/3] update --- app/store/prompt.ts | 2 -- app/utils/indexedDB-storage.ts | 1 + app/utils/store.ts | 11 ++++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/store/prompt.ts b/app/store/prompt.ts index 29a97abba5f..a25cda5813a 100644 --- a/app/store/prompt.ts +++ b/app/store/prompt.ts @@ -179,8 +179,6 @@ export const usePromptStore = createPersistStore( res.en.length + res.cn.length + res.tw.length; SearchService.init(allPromptsForSearch, userPrompts); }); - - return () => state.setHasHydrated(true); }, }, ); diff --git a/app/utils/indexedDB-storage.ts b/app/utils/indexedDB-storage.ts index 86b30477788..da309455013 100644 --- a/app/utils/indexedDB-storage.ts +++ b/app/utils/indexedDB-storage.ts @@ -15,6 +15,7 @@ class IndexedDBStorage implements StateStorage { try { const _value = JSON.parse(value); if (!_value?.state?._hasHydrated) { + console.warn("skip setItem", name); return; } await set(name, value); diff --git a/app/utils/store.ts b/app/utils/store.ts index 02c8139b0ad..ff15bee14ed 100644 --- a/app/utils/store.ts +++ b/app/utils/store.ts @@ -35,11 +35,12 @@ export function createPersistStore( persistOptions: SecondParam>>, ) { persistOptions.storage = createJSONStorage(() => indexedDBStorage); - persistOptions.onRehydrateStorage = persistOptions.onRehydrateStorage - ? persistOptions.onRehydrateStorage - : (state) => { - return () => state.setHasHydrated(true); - }; + const oldOonRehydrateStorage = persistOptions?.onRehydrateStorage; + persistOptions.onRehydrateStorage = (state) => { + oldOonRehydrateStorage?.(state); + return () => state.setHasHydrated(true); + }; + return create( persist( combine(