dev-store #1

Merged
purp1e merged 16 commits from dev-store into master 2025-03-13 00:54:31 +08:00
5 changed files with 11 additions and 26 deletions
Showing only changes of commit fcead5a4f5 - Show all commits

View File

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

View File

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

View File

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

View File

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