[debug] rethink store

This commit is contained in:
Purp1e
2024-11-11 17:17:08 +08:00
parent fcead5a4f5
commit af7dbe0cac
7 changed files with 130 additions and 60 deletions

View File

@@ -1,47 +1,80 @@
import { tauriStore } from "@/utils/persist"
import { create } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"
import { Store } from "@tauri-apps/plugin-store"
// import { create } from "zustand"
// import { createJSONStorage, persist } from "zustand/middleware"
interface AppState {
version: string
hasUpdate: boolean
inited: boolean
notice: string
useMirror: boolean
}
// interface AppState {
// version: string
// hasUpdate: boolean
// inited: boolean
// notice: string
// useMirror: boolean
// }
interface AppAction {
setVersion: (version: string) => void
setHasUpdate: (hasUpdate: boolean) => void
setInited: (hasInit: boolean) => void
setNotice: (notice: string) => void
setUseMirror: (useMirror: boolean) => void
}
// interface AppAction {
// setVersion: (version: string) => void
// setHasUpdate: (hasUpdate: boolean) => void
// setInited: (hasInit: boolean) => void
// setNotice: (notice: string) => void
// setUseMirror: (useMirror: boolean) => void
// }
const STORE_NAME = "app"
const { /* store, */ storage } = await tauriStore(STORE_NAME)
// 节流设置
export const THROTTLE_TIME = 300
export const THROTTLE_TIME_STORE = 2000
export const THROTTLE_LEADING = true
export const THROTTLE_TRAILING = true
// const { /* store, */ storage } = await tauriStore(STORE_NAME)
const useAppStore = create<AppState & AppAction>()(
persist(
(set /* , get */) => ({
version: "0.0.1",
hasUpdate: false,
inited: false,
notice: "Man! What can I say?",
useMirror: true,
setVersion: (version: string) => set(() => ({ version: version })),
setHasUpdate: (hasUpdate: boolean) =>
set(() => ({ hasUpdate: hasUpdate })),
setInited: (inited: boolean) => set(() => ({ inited: inited })),
setNotice: (notice: string) => set(() => ({ notice: notice })),
setUseMirror: (useMirror: boolean) =>
set(() => ({ useMirror: useMirror })),
}),
{
name: STORE_NAME, // name of item in the storage (must be unique)
storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
},
),
)
// const useAppStore = create<AppState & AppAction>()(
// persist(
// (set /* , get */) => ({
// version: "0.0.1",
// hasUpdate: false,
// inited: false,
// notice: "Man! What can I say?",
// useMirror: true,
// setVersion: (version: string) => set(() => ({ version: version })),
// setHasUpdate: (hasUpdate: boolean) =>
// set(() => ({ hasUpdate: hasUpdate })),
// setInited: (inited: boolean) => set(() => ({ inited: inited })),
// setNotice: (notice: string) => set(() => ({ notice: notice })),
// setUseMirror: (useMirror: boolean) =>
// set(() => ({ useMirror: useMirror })),
// }),
// {
// name: STORE_NAME, // name of item in the storage (must be unique)
// storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
// },
// ),
// )
const useAppStore = async (): Promise<any> => {
let store = await Store.get(`${STORE_NAME || "store"}.settings.json`);
if (!store) {
store = await Store.load(`${STORE_NAME || "store"}.settings.json`, {
autoSave: THROTTLE_TIME_STORE,
});
}
// 生成随机字符串每1s打印
const randomString = Math.random().toString(36).substring(2, 15)
setInterval(() => {
console.log("app store", randomString)
}, 1000)
return {
version: async () => (await store.get<string>("version")) || "0.0.1",
hasUpdate: async () => (await store.get<boolean>("hasUpdate")) || false,
inited: async () => (await store.get<boolean>("inited")) || false,
notice: async () => (await store.get<string>("notice")) || "",
useMirror: async () => (await store.get<boolean>("useMirror")) || true,
setVersion: async (version: string) => await store.set("version", version),
setHasUpdate: async (hasUpdate: boolean) => await store.set("hasUpdate", hasUpdate),
setInited: async (inited: boolean) => await store.set("inited", inited),
setNotice: async (notice: string) => await store.set("notice", notice),
setUseMirror: async (useMirror: boolean) => await store.set("useMirror", useMirror),
}
}
export default useAppStore

View File

@@ -1,5 +1,5 @@
import type { SteamUser } from "@/types/steam"
import { tauriStore } from "@/utils/persist"
import { tauriStore } from "@/utils/old"
import { create } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"

View File

@@ -1,4 +1,4 @@
import { tauriStore } from "@/utils/persist"
import { tauriStore } from "@/utils/old"
import { create } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"