Files
cstb-next/src/store/app.ts
2024-11-11 17:17:08 +08:00

81 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 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"
// 节流设置
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 = 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