[fix] tauri store async fn

This commit is contained in:
Purp1e
2024-11-11 11:19:29 +08:00
parent 918c6bf83a
commit fcead5a4f5
5 changed files with 11 additions and 26 deletions

View File

@@ -19,7 +19,7 @@ interface AppAction {
}
const STORE_NAME = "app"
const { /* store, */ storage } = tauriStore(STORE_NAME)
const { /* store, */ storage } = await tauriStore(STORE_NAME)
const useAppStore = create<AppState & AppAction>()(
persist(

View File

@@ -31,7 +31,7 @@ interface SteamAction {
}
const STORE_NAME = "steam"
const { /* store, */ storage } = tauriStore(STORE_NAME)
const { /* store, */ storage } = await tauriStore(STORE_NAME)
const useSteamStore = create<SteamState & SteamAction>()(
persist(

View File

@@ -16,7 +16,7 @@ interface ToolAction {
}
const STORE_NAME = "tool"
const { /* store, */ storage } = tauriStore(STORE_NAME)
const { /* store, */ storage } = await tauriStore(STORE_NAME)
const useToolStore = create<ToolState & ToolAction>()(
persist(

View File

@@ -6,31 +6,19 @@ import type { StateStorage } from "zustand/middleware"
// 节流设置
export const THROTTLE_TIME = 300
export const THROTTLE_TIME_STORE = 5000
export const THROTTLE_TIME_STORE = 2000
export const THROTTLE_LEADING = true
export const THROTTLE_TRAILING = true
// 自定义Store覆盖get, set等方法以适应tauri+zustand
export async function tauriStore(name: string) {
const store = await Store.load(`${name || "store"}.settings.json`)
// 自动保存
const autoSave = throttle(
THROTTLE_TIME_STORE,
async () => {
await store.save()
},
{
noTrailing: !THROTTLE_TRAILING,
noLeading: !THROTTLE_LEADING,
},
)
const store = await Store.load(`${name || "store"}.settings.json`, {
autoSave: THROTTLE_TIME_STORE,
})
const doSet = throttle(
THROTTLE_TIME,
async (key: string, value: unknown) => {
await store.set(key, value)
},
async (key: string, value: unknown) => await store.set(key, value),
{
noTrailing: !THROTTLE_TRAILING,
noLeading: !THROTTLE_LEADING,
@@ -39,12 +27,9 @@ export async function tauriStore(name: string) {
const set = (key: string, value: unknown) => {
doSet(key, value)
autoSave()
}
const get = (key: string) => {
return store.get(key)
}
const get = async (key: string) => await store.get(key)
// 自定义存储对象
const storage: StateStorage = {
@@ -62,5 +47,5 @@ export async function tauriStore(name: string) {
},
}
return { store, get, set, autoSave, storage }
return { store, get, set, storage }
}

View File

@@ -12,7 +12,7 @@
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,