From c2fc0b49797ef8b016949d9051bfad140326bdef Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Tue, 27 Aug 2024 09:57:07 +0800 Subject: [PATCH] feat: try catch indexedDB error --- app/store/chat.ts | 4 ++-- app/utils/indexDB-storage.ts | 22 -------------------- app/utils/indexedDB-storage.ts | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 24 deletions(-) delete mode 100644 app/utils/indexDB-storage.ts create mode 100644 app/utils/indexedDB-storage.ts diff --git a/app/store/chat.ts b/app/store/chat.ts index a6e4ee796a7..de2a6307850 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -27,7 +27,7 @@ import { createPersistStore } from "../utils/store"; import { collectModelsWithDefaultModel } from "../utils/model"; import { useAccessStore } from "./access"; import { isDalle3 } from "../utils"; -import { indexDBStorage } from "@/app/utils/indexDB-storage"; +import { indexedDBStorage } from "@/app/utils/indexedDB-storage"; export type ChatMessage = RequestMessage & { date: string; @@ -667,7 +667,7 @@ export const useChatStore = createPersistStore( }, async clearAllData() { - await indexDBStorage.clear(); + await indexedDBStorage.clear(); localStorage.clear(); location.reload(); }, diff --git a/app/utils/indexDB-storage.ts b/app/utils/indexDB-storage.ts deleted file mode 100644 index 5dee4c856cc..00000000000 --- a/app/utils/indexDB-storage.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { StateStorage } from "zustand/middleware"; -import { get, set, del, clear } from "idb-keyval"; - -class IndexDBStorage implements StateStorage { - public async getItem(name: string): Promise { - return (await get(name)) || localStorage.getItem(name); - } - - public async setItem(name: string, value: string): Promise { - await set(name, value); - } - - public async removeItem(name: string): Promise { - await del(name); - } - - public async clear(): Promise { - await clear(); - } -} - -export const indexDBStorage = new IndexDBStorage(); diff --git a/app/utils/indexedDB-storage.ts b/app/utils/indexedDB-storage.ts new file mode 100644 index 00000000000..51ee92b8185 --- /dev/null +++ b/app/utils/indexedDB-storage.ts @@ -0,0 +1,38 @@ +import { StateStorage } from "zustand/middleware"; +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); + } catch (error) { + return localStorage.getItem(name); + } + } + + public async setItem(name: string, value: string): Promise { + try { + await set(name, value); + } catch (error) { + localStorage.setItem(name, value); + } + } + + public async removeItem(name: string): Promise { + try { + await del(name); + } catch (error) { + localStorage.removeItem(name); + } + } + + public async clear(): Promise { + try { + await clear(); + } catch (error) { + localStorage.clear(); + } + } +} + +export const indexedDBStorage = new IndexedDBStorage();