[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

@@ -8,8 +8,9 @@
"windows": ["main"],
"permissions": [
"global-shortcut:default",
"store:default",
"store:allow-set",
"store:allow-get",
"store:allow-get-store",
"store:allow-has",
"store:allow-delete",
"store:allow-clear",

View File

@@ -1 +1 @@
{"desktop-capability":{"identifier":"desktop-capability","description":"","local":true,"windows":["main"],"permissions":["global-shortcut:default","store:allow-set","store:allow-get","store:allow-has","store:allow-delete","store:allow-clear","store:allow-values","store:allow-save","store:allow-load","store:allow-reset","store:allow-entries"],"platforms":["macOS","windows","linux"]},"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["main"],"permissions":["core:default","fs:allow-read-file","fs:allow-write-file","fs:allow-read-dir","fs:allow-copy-file","fs:allow-mkdir","fs:allow-remove","fs:allow-remove","fs:allow-rename","fs:allow-exists","core:window:allow-create","core:window:allow-center","core:window:allow-request-user-attention","core:window:allow-set-resizable","core:window:allow-set-maximizable","core:window:allow-set-minimizable","core:window:allow-set-closable","core:window:allow-set-title","core:window:allow-maximize","core:window:allow-unmaximize","core:window:allow-minimize","core:window:allow-unminimize","core:window:allow-show","core:window:allow-hide","core:window:allow-close","core:window:allow-set-decorations","core:window:allow-set-always-on-top","core:window:allow-set-content-protected","core:window:allow-set-size","core:window:allow-set-min-size","core:window:allow-set-max-size","core:window:allow-set-position","core:window:allow-set-fullscreen","core:window:allow-set-focus","core:window:allow-set-icon","core:window:allow-set-skip-taskbar","core:window:allow-set-cursor-grab","core:window:allow-set-cursor-visible","core:window:allow-set-cursor-icon","core:window:allow-set-cursor-position","core:window:allow-set-ignore-cursor-events","core:window:allow-start-dragging","core:webview:allow-print","shell:allow-execute","shell:allow-open","dialog:allow-open","dialog:allow-save","dialog:allow-message","dialog:allow-ask","dialog:allow-confirm","http:default","notification:default","global-shortcut:allow-is-registered","global-shortcut:allow-register","global-shortcut:allow-register-all","global-shortcut:allow-unregister","global-shortcut:allow-unregister-all","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","process:allow-restart","process:allow-exit","clipboard-manager:allow-read-text","clipboard-manager:allow-write-text","core:app:allow-app-show","core:app:allow-app-hide","process:default","fs:default","dialog:default","os:default","clipboard-manager:default"]}}
{"desktop-capability":{"identifier":"desktop-capability","description":"","local":true,"windows":["main"],"permissions":["global-shortcut:default","store:default","store:allow-set","store:allow-get-store","store:allow-has","store:allow-delete","store:allow-clear","store:allow-values","store:allow-save","store:allow-load","store:allow-reset","store:allow-entries"],"platforms":["macOS","windows","linux"]},"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["main"],"permissions":["core:default","fs:allow-read-file","fs:allow-write-file","fs:allow-read-dir","fs:allow-copy-file","fs:allow-mkdir","fs:allow-remove","fs:allow-remove","fs:allow-rename","fs:allow-exists","core:window:allow-create","core:window:allow-center","core:window:allow-request-user-attention","core:window:allow-set-resizable","core:window:allow-set-maximizable","core:window:allow-set-minimizable","core:window:allow-set-closable","core:window:allow-set-title","core:window:allow-maximize","core:window:allow-unmaximize","core:window:allow-minimize","core:window:allow-unminimize","core:window:allow-show","core:window:allow-hide","core:window:allow-close","core:window:allow-set-decorations","core:window:allow-set-always-on-top","core:window:allow-set-content-protected","core:window:allow-set-size","core:window:allow-set-min-size","core:window:allow-set-max-size","core:window:allow-set-position","core:window:allow-set-fullscreen","core:window:allow-set-focus","core:window:allow-set-icon","core:window:allow-set-skip-taskbar","core:window:allow-set-cursor-grab","core:window:allow-set-cursor-visible","core:window:allow-set-cursor-icon","core:window:allow-set-cursor-position","core:window:allow-set-ignore-cursor-events","core:window:allow-start-dragging","core:webview:allow-print","shell:allow-execute","shell:allow-open","dialog:allow-open","dialog:allow-save","dialog:allow-message","dialog:allow-ask","dialog:allow-confirm","http:default","notification:default","global-shortcut:allow-is-registered","global-shortcut:allow-register","global-shortcut:allow-register-all","global-shortcut:allow-unregister","global-shortcut:allow-unregister-all","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","process:allow-restart","process:allow-exit","clipboard-manager:allow-read-text","clipboard-manager:allow-write-text","core:app:allow-app-show","core:app:allow-app-hide","process:default","fs:default","dialog:default","os:default","clipboard-manager:default"]}}

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"

51
src/utils/old.ts Normal file
View File

@@ -0,0 +1,51 @@
import { Store } from "@tauri-apps/plugin-store"
import { throttle } from "throttle-debounce"
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { StateStorage } from "zustand/middleware"
// import { useThrottleFn } from '@reactuses/core';
// 节流设置
export const THROTTLE_TIME = 300
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`, {
autoSave: THROTTLE_TIME_STORE,
})
const doSet = throttle(
THROTTLE_TIME,
async (key: string, value: unknown) => await store.set(key, value),
{
noTrailing: !THROTTLE_TRAILING,
noLeading: !THROTTLE_LEADING,
},
)
const set = (key: string, value: unknown) => {
doSet(key, value)
}
const get = async (key: string) => await store.get(key)
// 自定义存储对象
const storage: StateStorage = {
setItem: async (name: string, value: string): Promise<void> => {
await store.set(name, value)
// console.log(name, 'has been set to', value)
},
getItem: async (name: string): Promise<string | null> => {
// console.log(name, 'has been get')
return (await store.get(name)) || null
},
removeItem: async (name: string): Promise<void> => {
// console.log(name, 'has been deleted')
await store.delete(name)
},
}
return { store, get, set, storage }
}

View File

@@ -31,21 +31,6 @@ export async function tauriStore(name: string) {
const get = async (key: string) => await store.get(key)
// 自定义存储对象
const storage: StateStorage = {
setItem: async (name: string, value: string): Promise<void> => {
await store.set(name, value)
// console.log(name, 'has been set to', value)
},
getItem: async (name: string): Promise<string | null> => {
// console.log(name, 'has been get')
return (await store.get(name)) || null
},
removeItem: async (name: string): Promise<void> => {
// console.log(name, 'has been deleted')
await store.delete(name)
},
}
return { store, get, set, storage }
return { store, get, set }
}