UNPKG

tauri-mw-store

Version:

Declarative multi-window state management for Tauri apps with React hooks, schema-driven persistence, and automatic synchronization.

109 lines (104 loc) 3.76 kB
interface UseStoreStateOptions { /** 窗口创建时是否从其他窗口同步当前状态,默认为 true */ syncOnMount?: boolean; } type StoreKeyConfig<T> = { default: T; persist?: boolean | { /** 保存策略:'immediate' = 立即保存,'onClose' = 窗口关闭时保存 */ saveStrategy?: "immediate" | "onClose"; }; }; type StoreValue<T = any> = StoreKeyConfig<T> | T; type StoreSchema = Record<string, StoreValue<any>>; type InferValue<C> = C extends { default: infer T; } ? T : C; declare const storeConfig: <T>(config: StoreKeyConfig<T>) => StoreKeyConfig<T>; type Accessors<S extends StoreSchema> = { /** 初始化 store */ initAppStore: () => Promise<void>; } & { [K in keyof S & string as `get${Capitalize<K>}`]: () => InferValue<S[K]>; } & { [K in keyof S & string as `set${Capitalize<K>}`]: (v: InferValue<S[K]>, emitToWindows?: string[] | "all") => Promise<void>; } & { [K in keyof S & string as `use${Capitalize<K>}`]: (options?: UseStoreStateOptions) => InferValue<S[K]>; } & { [K in keyof S & string as `on${Capitalize<K>}Change`]: (cb: (v: InferValue<S[K]>) => void) => () => void; }; /** * 基于 schema 的统一初始化与类型安全访问器生成器 * * @example * ```typescript * const appStore = createMWStore({ * count: storeConfig({ default: 0, persist: true }), * name: storeConfig({ default: 'hello' }), * simpleValue: 42 // 简写形式,不持久化 * }); * * // 用户只会看到 api 对象的方法 * await appStore.initAppStore(); * const count = appStore.getCount(); * await appStore.setCount(5); * ``` */ declare function createMWStore<S extends StoreSchema>(schema: S): Accessors<S>; type EventHandler<T> = T extends void | undefined ? () => void | Promise<void> : (context: T) => void | Promise<void>; type WindowConfig<T = void> = { listeners?: Record<string, EventHandler<T>>; emitOnInit?: Array<string | { event: string; payload?: any; }>; onInit?: () => T | Promise<T>; }; declare function window<T>(config: WindowConfig<T>): WindowConfig<T>; type WindowEventsConfig = { [windowLabel: string]: WindowConfig<any>; }; type WindowEventsController = { dispose: () => void; }; /** * 根据当前窗口标签,按声明式配置注册事件监听与初始化触发。 * 使用者仅需在入口处调用一次。 * * onInit 返回的值会自动作为 listeners 中所有处理器的参数: * @example * ```ts * import { defineWindowEvents, window } from "tauri-mw-store"; * * await defineWindowEvents({ * // 使用 window() 辅助函数获得完整的类型提示 * main: window({ * onInit: () => { * const updater = new VersionUpdateUtils(); * return { updater }; // 返回共享状态 * }, * listeners: { * // 这里会有完整的类型提示:({ updater }: { updater: VersionUpdateUtils }) * [EventKey.CHECK_UPDATE]: ({ updater }) => updater.checkForUpdates(), * [EventKey.INSTALL_REQUEST]: ({ updater }) => updater.askAndInstall(), * }, * }), * * // 无返回值的窗口 * settings: window({ * onInit: () => console.log("✅ 设置窗口初始化完成"), * listeners: { * // 这里的 handler 不接收任何参数 * [EventKey.SOME_EVENT]: () => console.log("处理事件"), * }, * emitOnInit: [EventKey.CHECK_UPDATE], * }), * }); * ``` */ declare function defineWindowEvents(config: WindowEventsConfig): Promise<WindowEventsController | undefined>; /** * 获取当前存储中的所有键(用于调试) */ declare function getAllStoredKeys(): Promise<string[]>; export { createMWStore, defineWindowEvents, getAllStoredKeys, storeConfig, window };