feat: add indexDB

This commit is contained in:
Dogtiti
2024-08-26 21:13:35 +08:00
parent 718782f5b1
commit 4060e367ad
4 changed files with 29 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
import { StateStorage } from "zustand/middleware";
import { get, set, del } from "idb-keyval";
class IndexDBStorage implements StateStorage {
constructor() {}
public async getItem(name: string): Promise<string | null> {
return (await get(name)) || localStorage.getItem(name);
}
public async setItem(name: string, value: string): Promise<void> {
await set(name, value);
}
public async removeItem(name: string): Promise<void> {
await del(name);
}
}
export const indexDBStorage = new IndexDBStorage();

View File

@@ -1,7 +1,8 @@
import { create } from "zustand";
import { combine, persist } from "zustand/middleware";
import { combine, persist, createJSONStorage } from "zustand/middleware";
import { Updater } from "../typing";
import { deepClone } from "./clone";
import { indexDBStorage } from "@/app/utils/indexDB-storage";
type SecondParam<T> = T extends (
_f: infer _F,
@@ -31,6 +32,7 @@ export function createPersistStore<T extends object, M>(
) => M,
persistOptions: SecondParam<typeof persist<T & M & MakeUpdater<T>>>,
) {
persistOptions.storage = createJSONStorage(() => indexDBStorage);
return create(
persist(
combine(