@composy/store-vue 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -0
- package/dist/index.js +1536 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +1008 -0
- package/dist/index.min.js.map +1 -0
- package/es/composables/defineSetupStore.d.ts +120 -0
- package/es/composables/defineSetupStore.d.ts.map +1 -0
- package/es/composables/defineSetupStore.js +29 -0
- package/es/composables/defineSetupStore.js.map +1 -0
- package/es/composables/index.d.ts +24 -0
- package/es/composables/index.d.ts.map +1 -0
- package/es/composables/index.js +21 -0
- package/es/composables/useFormStore.d.ts +131 -0
- package/es/composables/useFormStore.d.ts.map +1 -0
- package/es/composables/useFormStore.js +189 -0
- package/es/composables/useFormStore.js.map +1 -0
- package/es/composables/useOptimistic.d.ts +67 -0
- package/es/composables/useOptimistic.d.ts.map +1 -0
- package/es/composables/useOptimistic.js +64 -0
- package/es/composables/useOptimistic.js.map +1 -0
- package/es/composables/usePersist.d.ts +68 -0
- package/es/composables/usePersist.d.ts.map +1 -0
- package/es/composables/usePersist.js +96 -0
- package/es/composables/usePersist.js.map +1 -0
- package/es/composables/usePersistedState.d.ts +59 -0
- package/es/composables/usePersistedState.d.ts.map +1 -0
- package/es/composables/usePersistedState.js +121 -0
- package/es/composables/usePersistedState.js.map +1 -0
- package/es/composables/useSharedState.d.ts +62 -0
- package/es/composables/useSharedState.d.ts.map +1 -0
- package/es/composables/useSharedState.js +94 -0
- package/es/composables/useSharedState.js.map +1 -0
- package/es/composables/useStore.d.ts +99 -0
- package/es/composables/useStore.d.ts.map +1 -0
- package/es/composables/useStore.js +61 -0
- package/es/composables/useStore.js.map +1 -0
- package/es/composables/useStoreDevtools.d.ts +31 -0
- package/es/composables/useStoreDevtools.d.ts.map +1 -0
- package/es/composables/useStoreDevtools.js +112 -0
- package/es/composables/useStoreDevtools.js.map +1 -0
- package/es/composables/useStoreSnapshot.d.ts +122 -0
- package/es/composables/useStoreSnapshot.d.ts.map +1 -0
- package/es/composables/useStoreSnapshot.js +209 -0
- package/es/composables/useStoreSnapshot.js.map +1 -0
- package/es/composables/useSubscribe.d.ts +90 -0
- package/es/composables/useSubscribe.d.ts.map +1 -0
- package/es/composables/useSubscribe.js +53 -0
- package/es/composables/useSubscribe.js.map +1 -0
- package/es/context/injection-keys.d.ts +8 -0
- package/es/context/injection-keys.d.ts.map +1 -0
- package/es/context/injection-keys.js +17 -0
- package/es/context/injection-keys.js.map +1 -0
- package/es/index.d.ts +24 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +32 -0
- package/es/index.js.map +1 -0
- package/es/plugins/engine-plugin.d.ts +36 -0
- package/es/plugins/engine-plugin.d.ts.map +1 -0
- package/es/plugins/engine-plugin.js +83 -0
- package/es/plugins/engine-plugin.js.map +1 -0
- package/es/plugins/enhanced-persist-plugin.d.ts +3 -0
- package/es/plugins/enhanced-persist-plugin.d.ts.map +1 -0
- package/es/plugins/enhanced-persist-plugin.js +12 -0
- package/es/plugins/index.d.ts +7 -0
- package/es/plugins/index.d.ts.map +1 -0
- package/es/plugins/index.js +15 -0
- package/es/plugins/pinia-plugin.d.ts +68 -0
- package/es/plugins/pinia-plugin.d.ts.map +1 -0
- package/es/plugins/pinia-plugin.js +172 -0
- package/es/plugins/pinia-plugin.js.map +1 -0
- package/es/plugins/registry-plugin.d.ts +2 -0
- package/es/plugins/registry-plugin.d.ts.map +1 -0
- package/es/plugins/registry-plugin.js +12 -0
- package/es/plugins/vue-plugin.d.ts +10 -0
- package/es/plugins/vue-plugin.d.ts.map +1 -0
- package/es/plugins/vue-plugin.js +24 -0
- package/es/plugins/vue-plugin.js.map +1 -0
- package/es/runtime/install-store-app.d.ts +23 -0
- package/es/runtime/install-store-app.d.ts.map +1 -0
- package/es/runtime/install-store-app.js +54 -0
- package/es/runtime/install-store-app.js.map +1 -0
- package/es/runtime/store-plugin-options.d.ts +40 -0
- package/es/runtime/store-plugin-options.d.ts.map +1 -0
- package/es/runtime/store-plugin-options.js +24 -0
- package/es/runtime/store-plugin-options.js.map +1 -0
- package/es/runtime/store-registry.d.ts +64 -0
- package/es/runtime/store-registry.d.ts.map +1 -0
- package/es/runtime/store-registry.js +102 -0
- package/es/runtime/store-registry.js.map +1 -0
- package/es/runtime/store-runtime.d.ts +46 -0
- package/es/runtime/store-runtime.d.ts.map +1 -0
- package/es/runtime/store-runtime.js +58 -0
- package/es/runtime/store-runtime.js.map +1 -0
- package/es/types.d.ts +167 -0
- package/es/types.d.ts.map +1 -0
- package/es/types.js +10 -0
- package/esm/composables/defineSetupStore.d.ts +120 -0
- package/esm/composables/defineSetupStore.d.ts.map +1 -0
- package/esm/composables/defineSetupStore.js +29 -0
- package/esm/composables/defineSetupStore.js.map +1 -0
- package/esm/composables/index.d.ts +24 -0
- package/esm/composables/index.d.ts.map +1 -0
- package/esm/composables/index.js +21 -0
- package/esm/composables/useFormStore.d.ts +131 -0
- package/esm/composables/useFormStore.d.ts.map +1 -0
- package/esm/composables/useFormStore.js +189 -0
- package/esm/composables/useFormStore.js.map +1 -0
- package/esm/composables/useOptimistic.d.ts +67 -0
- package/esm/composables/useOptimistic.d.ts.map +1 -0
- package/esm/composables/useOptimistic.js +64 -0
- package/esm/composables/useOptimistic.js.map +1 -0
- package/esm/composables/usePersist.d.ts +68 -0
- package/esm/composables/usePersist.d.ts.map +1 -0
- package/esm/composables/usePersist.js +96 -0
- package/esm/composables/usePersist.js.map +1 -0
- package/esm/composables/usePersistedState.d.ts +59 -0
- package/esm/composables/usePersistedState.d.ts.map +1 -0
- package/esm/composables/usePersistedState.js +121 -0
- package/esm/composables/usePersistedState.js.map +1 -0
- package/esm/composables/useSharedState.d.ts +62 -0
- package/esm/composables/useSharedState.d.ts.map +1 -0
- package/esm/composables/useSharedState.js +94 -0
- package/esm/composables/useSharedState.js.map +1 -0
- package/esm/composables/useStore.d.ts +99 -0
- package/esm/composables/useStore.d.ts.map +1 -0
- package/esm/composables/useStore.js +61 -0
- package/esm/composables/useStore.js.map +1 -0
- package/esm/composables/useStoreDevtools.d.ts +31 -0
- package/esm/composables/useStoreDevtools.d.ts.map +1 -0
- package/esm/composables/useStoreDevtools.js +112 -0
- package/esm/composables/useStoreDevtools.js.map +1 -0
- package/esm/composables/useStoreSnapshot.d.ts +122 -0
- package/esm/composables/useStoreSnapshot.d.ts.map +1 -0
- package/esm/composables/useStoreSnapshot.js +209 -0
- package/esm/composables/useStoreSnapshot.js.map +1 -0
- package/esm/composables/useSubscribe.d.ts +90 -0
- package/esm/composables/useSubscribe.d.ts.map +1 -0
- package/esm/composables/useSubscribe.js +53 -0
- package/esm/composables/useSubscribe.js.map +1 -0
- package/esm/context/injection-keys.d.ts +8 -0
- package/esm/context/injection-keys.d.ts.map +1 -0
- package/esm/context/injection-keys.js +17 -0
- package/esm/context/injection-keys.js.map +1 -0
- package/esm/index.d.ts +24 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +34 -0
- package/esm/index.js.map +1 -0
- package/esm/plugins/engine-plugin.d.ts +36 -0
- package/esm/plugins/engine-plugin.d.ts.map +1 -0
- package/esm/plugins/engine-plugin.js +83 -0
- package/esm/plugins/engine-plugin.js.map +1 -0
- package/esm/plugins/enhanced-persist-plugin.d.ts +3 -0
- package/esm/plugins/enhanced-persist-plugin.d.ts.map +1 -0
- package/esm/plugins/enhanced-persist-plugin.js +12 -0
- package/esm/plugins/index.d.ts +7 -0
- package/esm/plugins/index.d.ts.map +1 -0
- package/esm/plugins/index.js +16 -0
- package/esm/plugins/pinia-plugin.d.ts +68 -0
- package/esm/plugins/pinia-plugin.d.ts.map +1 -0
- package/esm/plugins/pinia-plugin.js +172 -0
- package/esm/plugins/pinia-plugin.js.map +1 -0
- package/esm/plugins/registry-plugin.d.ts +2 -0
- package/esm/plugins/registry-plugin.d.ts.map +1 -0
- package/esm/plugins/registry-plugin.js +12 -0
- package/esm/plugins/vue-plugin.d.ts +10 -0
- package/esm/plugins/vue-plugin.d.ts.map +1 -0
- package/esm/plugins/vue-plugin.js +24 -0
- package/esm/plugins/vue-plugin.js.map +1 -0
- package/esm/runtime/install-store-app.d.ts +23 -0
- package/esm/runtime/install-store-app.d.ts.map +1 -0
- package/esm/runtime/install-store-app.js +54 -0
- package/esm/runtime/install-store-app.js.map +1 -0
- package/esm/runtime/store-plugin-options.d.ts +40 -0
- package/esm/runtime/store-plugin-options.d.ts.map +1 -0
- package/esm/runtime/store-plugin-options.js +24 -0
- package/esm/runtime/store-plugin-options.js.map +1 -0
- package/esm/runtime/store-registry.d.ts +64 -0
- package/esm/runtime/store-registry.d.ts.map +1 -0
- package/esm/runtime/store-registry.js +94 -0
- package/esm/runtime/store-registry.js.map +1 -0
- package/esm/runtime/store-runtime.d.ts +46 -0
- package/esm/runtime/store-runtime.d.ts.map +1 -0
- package/esm/runtime/store-runtime.js +58 -0
- package/esm/runtime/store-runtime.js.map +1 -0
- package/esm/types.d.ts +167 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +10 -0
- package/lib/composables/defineSetupStore.cjs +33 -0
- package/lib/composables/defineSetupStore.cjs.map +1 -0
- package/lib/composables/defineSetupStore.d.ts +120 -0
- package/lib/composables/index.cjs +46 -0
- package/lib/composables/index.d.ts +24 -0
- package/lib/composables/useFormStore.cjs +197 -0
- package/lib/composables/useFormStore.cjs.map +1 -0
- package/lib/composables/useFormStore.d.ts +131 -0
- package/lib/composables/useOptimistic.cjs +66 -0
- package/lib/composables/useOptimistic.cjs.map +1 -0
- package/lib/composables/useOptimistic.d.ts +67 -0
- package/lib/composables/usePersist.cjs +100 -0
- package/lib/composables/usePersist.cjs.map +1 -0
- package/lib/composables/usePersist.d.ts +68 -0
- package/lib/composables/usePersistedState.cjs +123 -0
- package/lib/composables/usePersistedState.cjs.map +1 -0
- package/lib/composables/usePersistedState.d.ts +59 -0
- package/lib/composables/useSharedState.cjs +96 -0
- package/lib/composables/useSharedState.cjs.map +1 -0
- package/lib/composables/useSharedState.d.ts +62 -0
- package/lib/composables/useStore.cjs +65 -0
- package/lib/composables/useStore.cjs.map +1 -0
- package/lib/composables/useStore.d.ts +99 -0
- package/lib/composables/useStoreDevtools.cjs +114 -0
- package/lib/composables/useStoreDevtools.cjs.map +1 -0
- package/lib/composables/useStoreDevtools.d.ts +31 -0
- package/lib/composables/useStoreSnapshot.cjs +211 -0
- package/lib/composables/useStoreSnapshot.cjs.map +1 -0
- package/lib/composables/useStoreSnapshot.d.ts +122 -0
- package/lib/composables/useSubscribe.cjs +57 -0
- package/lib/composables/useSubscribe.cjs.map +1 -0
- package/lib/composables/useSubscribe.d.ts +90 -0
- package/lib/context/injection-keys.cjs +21 -0
- package/lib/context/injection-keys.cjs.map +1 -0
- package/lib/context/injection-keys.d.ts +8 -0
- package/lib/index.cjs +235 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.ts +24 -0
- package/lib/package.json +3 -0
- package/lib/plugins/engine-plugin.cjs +90 -0
- package/lib/plugins/engine-plugin.cjs.map +1 -0
- package/lib/plugins/engine-plugin.d.ts +36 -0
- package/lib/plugins/enhanced-persist-plugin.cjs +15 -0
- package/lib/plugins/enhanced-persist-plugin.d.ts +3 -0
- package/lib/plugins/index.cjs +28 -0
- package/lib/plugins/index.d.ts +7 -0
- package/lib/plugins/pinia-plugin.cjs +177 -0
- package/lib/plugins/pinia-plugin.cjs.map +1 -0
- package/lib/plugins/pinia-plugin.d.ts +68 -0
- package/lib/plugins/registry-plugin.cjs +18 -0
- package/lib/plugins/registry-plugin.d.ts +2 -0
- package/lib/plugins/vue-plugin.cjs +26 -0
- package/lib/plugins/vue-plugin.cjs.map +1 -0
- package/lib/plugins/vue-plugin.d.ts +10 -0
- package/lib/runtime/install-store-app.cjs +57 -0
- package/lib/runtime/install-store-app.cjs.map +1 -0
- package/lib/runtime/install-store-app.d.ts +23 -0
- package/lib/runtime/store-plugin-options.cjs +26 -0
- package/lib/runtime/store-plugin-options.cjs.map +1 -0
- package/lib/runtime/store-plugin-options.d.ts +40 -0
- package/lib/runtime/store-registry.cjs +108 -0
- package/lib/runtime/store-registry.cjs.map +1 -0
- package/lib/runtime/store-registry.d.ts +64 -0
- package/lib/runtime/store-runtime.cjs +62 -0
- package/lib/runtime/store-runtime.cjs.map +1 -0
- package/lib/runtime/store-runtime.d.ts +46 -0
- package/lib/types.cjs +12 -0
- package/lib/types.d.ts +167 -0
- package/package.json +115 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pinia-plugin.js","names":[],"sources":["../../src/plugins/pinia-plugin.ts"],"sourcesContent":["/* ============================================================\n * pinia-plugin.ts\n * Pinia 持久化插件\n * 提供将 Pinia Store 状态自动持久化到浏览器存储的能力\n * 支持 TTL 过期、跨标签页同步、include/exclude 路径过滤、\n * 防抖保存、$dispose 生命周期钩子等特性\n * ============================================================ */\n\n/* 从 Pinia 导入插件相关类型 */\nimport type { PiniaPlugin, PiniaPluginContext, StoreGeneric } from 'pinia'\n/* 从本地类型文件导入持久化选项 */\nimport type { PersistOptions } from '../types'\n/* 从 store-core 导入工具函数 */\nimport { debounce, deepClone, deepMerge, deleteByPath, getByPath, setByPath } from '@composy/store-core'\n\n/* 用于标记 store 的 $dispose 是否已被 patch 的 Symbol 键 */\nconst STORE_PERSIST_DISPOSE_KEY = Symbol('ldesign-store-persist-dispose')\n\n/* 浏览器存储类型 */\ntype BrowserStorage = 'localStorage' | 'sessionStorage'\n\n/* 可被 patch $dispose 的 Store 扩展类型 */\ntype DisposableStore = StoreGeneric & {\n [STORE_PERSIST_DISPOSE_KEY]?: () => void /* 恢复原始 $dispose 的函数 */\n}\n\n/**\n * 持久化存储项的内部格式接口\n * 包含序列化的状态、时间戳、过期时间和版本号\n */\ninterface PersistedStorageItem {\n state: string /* 序列化后的状态字符串 */\n timestamp: number /* 保存时间戳 */\n expiresAt?: number /* 过期时间戳(可选) */\n version: number /* 数据格式版本号 */\n}\n\n/**\n * 持久化插件控制器接口\n * @property plugin - Pinia 插件函数\n * @property dispose - 清理函数,用于移除所有持久化订阅\n */\ninterface PersistPluginController {\n plugin: PiniaPlugin\n dispose: () => void\n}\n\n/**\n * 持久化插件全局配置选项\n * @property storage - 默认存储类型\n * @property keyPrefix - 存储键名前缀\n * @property debounce - 防抖延迟(毫秒)\n * @property debug - 是否开启调试日志\n * @property syncTabs - 是否默认启用跨标签页同步\n * @property defaultTTL - 默认过期时间(毫秒)\n */\nexport interface PersistPluginOptions {\n storage?: BrowserStorage\n keyPrefix?: string\n debounce?: number\n debug?: boolean\n syncTabs?: boolean\n defaultTTL?: number\n}\n\n/* 增强版持久化插件选项(与基础版相同,预留扩展) */\nexport interface EnhancedPersistPluginOptions extends PersistPluginOptions {}\n\n/**\n * 增强版持久化选项(Store 级别配置)\n * 在基础 PersistOptions 上增加 excludePaths、syncTabs、ttl\n */\nexport interface EnhancedPersistOptions extends PersistOptions {\n syncTabs?: boolean\n ttl?: number\n excludePaths?: string[]\n}\n\n/* 活跃的持久化控制器集合,用于全局清理 */\nconst activePersistControllers = new Set<() => void>()\n\n/**\n * 获取浏览器存储对象\n * 在 SSR 环境下返回 null\n */\nfunction getStorage(type: BrowserStorage): Storage | null {\n if (typeof window === 'undefined') {\n return null\n }\n\n return type === 'localStorage' ? window.localStorage : window.sessionStorage\n}\n\n/**\n * 规范化 Store 的 persist 配置\n * 将 boolean/对象/undefined 统一为 EnhancedPersistOptions | null\n */\nfunction normalizePersistConfig(\n value: boolean | PersistOptions | EnhancedPersistOptions | undefined,\n): EnhancedPersistOptions | null {\n if (!value) {\n return null /* 未配置持久化 */\n }\n\n /* boolean true 表示使用默认配置的空对象 */\n return typeof value === 'boolean' ? {} : value\n}\n\n/**\n * 根据 include/exclude 路径过滤状态\n * 优先使用 includePaths(白名单),其次使用 excludePaths(黑名单)\n *\n * @param state - 完整状态对象\n * @param includePaths - 包含路径列表(白名单模式)\n * @param excludePaths - 排除路径列表(黑名单模式)\n * @returns 过滤后的状态对象\n */\nfunction filterState(\n state: Record<string, unknown>,\n includePaths?: string[],\n excludePaths?: string[],\n): Record<string, unknown> {\n /* 白名单模式:只保留指定路径 */\n if (includePaths?.length) {\n const pickedState: Record<string, unknown> = {}\n includePaths.forEach((path) => {\n const value = getByPath(state, path)\n if (value !== undefined) {\n setByPath(pickedState, path, deepClone(value))\n }\n })\n return pickedState\n }\n\n /* 黑名单模式:排除指定路径 */\n if (excludePaths?.length) {\n const nextState = deepClone(state)\n excludePaths.forEach((path) => {\n deleteByPath(nextState, path)\n })\n return nextState\n }\n\n /* 无过滤:返回状态的深拷贝 */\n return deepClone(state)\n}\n\n/**\n * 将持久化的状态恢复到 Store\n * 使用 $patch + deepMerge 避免覆盖整个 state 引用\n */\nfunction restoreStoreState(\n store: StoreGeneric,\n nextState: Record<string, unknown>,\n): void {\n store.$patch((state) => {\n deepMerge(state as Record<string, unknown>, nextState)\n })\n}\n\n/**\n * 为 Store 的 $dispose 方法打补丁\n * 在 Store 被销毁时自动执行清理逻辑(取消订阅、移除事件监听等)\n *\n * @param store - 目标 Store\n * @param cleanup - 清理回调函数\n */\nfunction patchPersistDispose(store: DisposableStore, cleanup: () => void): void {\n /* 如果之前已有补丁,先恢复原始方法 */\n const restorePatchedDispose = store[STORE_PERSIST_DISPOSE_KEY]\n if (restorePatchedDispose) {\n restorePatchedDispose()\n }\n\n /* 保存原始 $dispose 方法 */\n const originalDispose = store.$dispose\n /* 替换为带清理逻辑的版本 */\n store.$dispose = function patchedDispose() {\n cleanup() /* 先执行清理 */\n return originalDispose.call(this) /* 再调用原始 dispose */\n }\n\n /* 保存恢复函数,供后续补丁使用 */\n store[STORE_PERSIST_DISPOSE_KEY] = () => {\n store.$dispose = originalDispose\n delete store[STORE_PERSIST_DISPOSE_KEY]\n }\n}\n\n/**\n * 创建持久化插件控制器\n * 返回一个 Pinia 插件函数和对应的 dispose 清理函数\n *\n * @param options - 插件全局配置\n * @returns 控制器对象(包含 plugin 和 dispose)\n */\nexport function createPersistPluginController(\n options: PersistPluginOptions = {},\n): PersistPluginController {\n /* 解构配置选项,设置默认值 */\n const {\n storage: defaultStorage = 'localStorage', /* 默认存储类型 */\n keyPrefix = 'ldesign-store:', /* 存储键前缀 */\n debounce: debounceDelay = 100, /* 默认 100ms 防抖 */\n debug = false, /* 调试模式 */\n syncTabs: defaultSyncTabs = false, /* 默认不跨标签页同步 */\n defaultTTL, /* 默认 TTL */\n } = options\n\n /* 清理函数映射表(key → cleanup) */\n const cleanupMap = new Map<string, () => void>()\n\n /**\n * 全局 dispose 函数\n * 清理所有 Store 的持久化订阅\n */\n const dispose = (): void => {\n cleanupMap.forEach(cleanup => cleanup())\n cleanupMap.clear()\n activePersistControllers.delete(dispose)\n }\n\n /* 注册到活跃控制器集合 */\n activePersistControllers.add(dispose)\n\n /**\n * Pinia 插件函数\n * 每当创建/使用一个 Pinia Store 时自动调用\n */\n const plugin: PiniaPlugin = (context: PiniaPluginContext): void => {\n const { store, options: storeOptions } = context\n /* 规范化该 Store 的持久化配置 */\n const config = normalizePersistConfig(\n (storeOptions as { persist?: boolean | PersistOptions | EnhancedPersistOptions }).persist,\n )\n\n /* 该 Store 未配置持久化则跳过 */\n if (!config) {\n return\n }\n\n /* 解析该 Store 的存储配置(Store 级别配置优先于全局配置) */\n const storageType = config.storage === 'sessionStorage' ? 'sessionStorage' : defaultStorage\n const storage = getStorage(storageType)\n const serializer = config.serializer?.serialize ?? JSON.stringify /* 序列化函数 */\n const deserialize = config.serializer?.deserialize ?? JSON.parse /* 反序列化函数 */\n const syncTabs = config.syncTabs ?? defaultSyncTabs /* 跨标签页同步 */\n const ttl = config.ttl ?? defaultTTL /* 过期时间 */\n const key = `${keyPrefix}${config.key || store.$id}` /* 存储键名 */\n\n /* 存储不可用则跳过 */\n if (!storage) {\n if (debug) {\n console.warn(`[PersistPlugin] Storage 不可用 (${store.$id})`)\n }\n return\n }\n\n /* 标记:是否正在从存储恢复状态(防止恢复过程中触发保存) */\n let isRestoring = false\n\n /**\n * 将持久化的原始状态字符串应用到 Store\n * 反序列化 → beforeRestore 钩子 → 恢复状态 → afterRestore 钩子\n */\n const applyPersistedValue = (rawState: string): void => {\n const nextState = deserialize(rawState) as Record<string, unknown>\n isRestoring = true /* 标记恢复中 */\n try {\n config.beforeRestore?.({ store }) /* 恢复前钩子 */\n restoreStoreState(store, nextState) /* 恢复状态 */\n config.afterRestore?.({ store }) /* 恢复后钩子 */\n }\n finally {\n isRestoring = false /* 恢复结束 */\n }\n }\n\n /**\n * 从存储恢复 Store 状态\n * 检查过期 → 反序列化 → 应用到 Store\n */\n const restoreState = (): void => {\n try {\n const stored = storage.getItem(key)\n if (!stored) {\n return /* 无持久化数据 */\n }\n\n const storageItem = JSON.parse(stored) as PersistedStorageItem\n /* 检查是否过期 */\n if (storageItem.expiresAt && Date.now() > storageItem.expiresAt) {\n storage.removeItem(key) /* 过期则清除 */\n return\n }\n\n /* 应用持久化值 */\n applyPersistedValue(storageItem.state)\n\n if (debug) {\n console.info(`[PersistPlugin] 已恢复状态 (${store.$id})`)\n }\n }\n catch (error) {\n console.error(`[PersistPlugin] 恢复状态失败 (${store.$id}):`, error)\n }\n }\n\n /**\n * 将当前 Store 状态持久化到存储\n * 过滤状态 → 序列化 → 包装为存储项 → 写入存储\n */\n const persistState = (): void => {\n /* 正在恢复时跳过保存,避免循环 */\n if (isRestoring) {\n return\n }\n\n try {\n /* 根据 include/exclude 路径过滤状态 */\n const filteredState = filterState(\n store.$state as Record<string, unknown>,\n config.paths,\n config.excludePaths,\n )\n\n /* 构建存储项 */\n const payload: PersistedStorageItem = {\n state: serializer(filteredState), /* 序列化过滤后的状态 */\n timestamp: Date.now(), /* 保存时间 */\n expiresAt: ttl ? Date.now() + ttl : undefined, /* 过期时间 */\n version: 1, /* 数据版本号 */\n }\n\n /* 写入存储 */\n storage.setItem(key, JSON.stringify(payload))\n\n if (debug) {\n console.info(`[PersistPlugin] 已保存状态 (${store.$id})`)\n }\n }\n catch (error) {\n console.error(`[PersistPlugin] 保存状态失败 (${store.$id}):`, error)\n }\n }\n\n /* 创建防抖版的保存函数 */\n const saveState = debounce(persistState, config.debounce ?? debounceDelay)\n\n /* 订阅 Store 状态变化,自动触发保存 */\n const stopSubscribe = store.$subscribe(() => {\n /* 恢复中不保存 */\n if (!isRestoring) {\n saveState()\n }\n })\n\n /* ── 跨标签页同步 ── */\n let stopStorageSync = (): void => {} /* 清理函数占位 */\n if (syncTabs && typeof window !== 'undefined') {\n /* 监听 storage 事件,同步其他标签页的状态变化 */\n const handleStorage = (event: StorageEvent): void => {\n /* 只关注目标 key 且有新值 */\n if (event.key !== key || !event.newValue) {\n return\n }\n\n try {\n const storageItem = JSON.parse(event.newValue) as PersistedStorageItem\n /* 跳过过期数据 */\n if (storageItem.expiresAt && Date.now() > storageItem.expiresAt) {\n return\n }\n\n /* 应用其他标签页的状态 */\n applyPersistedValue(storageItem.state)\n }\n catch (error) {\n console.error(`[PersistPlugin] 同步状态失败 (${store.$id}):`, error)\n }\n }\n\n window.addEventListener('storage', handleStorage)\n /* 保存清理函数 */\n stopStorageSync = () => {\n window.removeEventListener('storage', handleStorage)\n }\n }\n\n /**\n * 该 Store 的清理函数\n * 取消防抖、停止订阅、移除事件监听\n */\n const cleanup = (): void => {\n saveState.cancel() /* 取消待执行的防抖保存 */\n stopSubscribe() /* 取消状态订阅 */\n stopStorageSync() /* 移除跨标签页同步监听 */\n cleanupMap.delete(key) /* 从清理映射中移除 */\n }\n\n /* 注册清理函数 */\n cleanupMap.set(key, cleanup)\n /* 为 Store 的 $dispose 打补丁,确保销毁时自动清理 */\n patchPersistDispose(store as DisposableStore, cleanup)\n /* 立即尝试从存储恢复状态 */\n restoreState()\n }\n\n return {\n plugin, /* Pinia 插件函数 */\n dispose, /* 全局清理函数 */\n }\n}\n\n/**\n * 创建基础版持久化插件\n * @param options - 插件配置\n * @returns Pinia 插件函数\n */\nexport function createPersistPlugin(options: PersistPluginOptions = {}): PiniaPlugin {\n return createPersistPluginController(options).plugin\n}\n\n/**\n * 创建增强版持久化插件\n * 与基础版功能相同,类型签名保留扩展空间\n * @param options - 插件配置\n * @returns Pinia 插件函数\n */\nexport function createEnhancedPersistPlugin(\n options: EnhancedPersistPluginOptions = {},\n): PiniaPlugin {\n return createPersistPluginController(options).plugin\n}\n\n/**\n * 全局清理所有活跃的持久化控制器\n * 通常在应用卸载或测试中调用\n */\nexport function cleanupEnhancedPersist(): void {\n Array.from(activePersistControllers).forEach(dispose => dispose())\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry-plugin.d.ts","sourceRoot":"","sources":["../../src/plugins/registry-plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,qBAAqB,EAErB,mBAAmB,EAEnB,yBAAyB,EAEzB,gBAAgB,EAEhB,sBAAsB,GACvB,MAAM,2BAA2B,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { createRegistryMethods, createStoreRegistry, createStoreRegistryPlugin, getStoreRegistry, setGlobalStoreRegistry } from "../runtime/store-registry.js";
|
|
11
|
+
export { createRegistryMethods, createStoreRegistry, createStoreRegistryPlugin, getStoreRegistry, setGlobalStoreRegistry };
|
|
12
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin } from 'vue';
|
|
2
|
+
import type { StoreVuePluginOptions } from '../runtime/store-plugin-options';
|
|
3
|
+
export type { StoreVuePluginOptions } from '../runtime/store-plugin-options';
|
|
4
|
+
/**
|
|
5
|
+
* 创建 store Vue 插件
|
|
6
|
+
* @param options - store Vue 插件配置选项
|
|
7
|
+
* @returns 返回 Vue Plugin 对象,可通过 app.use() 安装
|
|
8
|
+
*/
|
|
9
|
+
export declare function createStoreVuePlugin(options?: StoreVuePluginOptions): Plugin;
|
|
10
|
+
//# sourceMappingURL=vue-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vue-plugin.d.ts","sourceRoot":"","sources":["../../src/plugins/vue-plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAO,MAAM,EAAE,MAAM,KAAK,CAAA;AAEtC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA;AAO5E,YAAY,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA;AAE5E;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,qBAA0B,GAAG,MAAM,CAoBhF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { createStoreRuntime } from "../runtime/store-runtime.js";
|
|
11
|
+
import { installStoreRuntime } from "../runtime/install-store-app.js";
|
|
12
|
+
//#region src/plugins/vue-plugin.ts
|
|
13
|
+
function createStoreVuePlugin(options = {}) {
|
|
14
|
+
return { install(app) {
|
|
15
|
+
const runtime = createStoreRuntime(options);
|
|
16
|
+
const installedRuntime = installStoreRuntime(app, runtime);
|
|
17
|
+
if (installedRuntime !== runtime) runtime.dispose();
|
|
18
|
+
if (installedRuntime.options.debug) console.info("[StoreVuePlugin] installed");
|
|
19
|
+
} };
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { createStoreVuePlugin };
|
|
23
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
24
|
+
//# sourceMappingURL=vue-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vue-plugin.js","names":[],"sources":["../../src/plugins/vue-plugin.ts"],"sourcesContent":["// 从 vue 导入 App 和 Plugin 类型\nimport type { App, Plugin } from 'vue'\n// 导入 store Vue 插件选项类型\nimport type { StoreVuePluginOptions } from '../runtime/store-plugin-options'\n// 导入安装 store 运行时的函数\nimport { installStoreRuntime } from '../runtime/install-store-app'\n// 导入创建 store 运行时的函数\nimport { createStoreRuntime } from '../runtime/store-runtime'\n\n// 重新导出 store Vue 插件选项类型\nexport type { StoreVuePluginOptions } from '../runtime/store-plugin-options'\n\n/**\n * 创建 store Vue 插件\n * @param options - store Vue 插件配置选项\n * @returns 返回 Vue Plugin 对象,可通过 app.use() 安装\n */\nexport function createStoreVuePlugin(options: StoreVuePluginOptions = {}): Plugin {\n return {\n // 安装插件到 Vue 应用\n install(app: App): void {\n // 创建 store 运行时实例\n const runtime = createStoreRuntime(options)\n // 将运行时安装到 Vue 应用,如果已安装则返回已有的运行时\n const installedRuntime = installStoreRuntime(app, runtime)\n\n // 如果安装的运行时不是新创建的,则销毁新建的运行时以避免内存泄漏\n if (installedRuntime !== runtime) {\n runtime.dispose()\n }\n\n // 如果开启了调试模式,输出安装成功日志\n if (installedRuntime.options.debug) {\n console.info('[StoreVuePlugin] installed')\n }\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;AAiBO,SAAS,qBAAqB,UAAiC,CAAC,GAAW;CAChF,OAAO,EAEL,QAAQ,KAAgB;EAEtB,MAAM,UAAU,mBAAmB,OAAO;EAE1C,MAAM,mBAAmB,oBAAoB,KAAK,OAAO;EAGzD,IAAI,qBAAqB,SACvB,QAAQ,QAAQ;EAIlB,IAAI,iBAAiB,QAAQ,OAC3B,QAAQ,KAAK,4BAA4B;CAE7C,EACF;AACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { App } from 'vue';
|
|
2
|
+
import type { StoreRuntime } from './store-runtime';
|
|
3
|
+
/**
|
|
4
|
+
* 获取 Vue App 上已安装的 Store 运行时
|
|
5
|
+
* @param app - Vue 应用实例
|
|
6
|
+
* @returns 已安装的运行时或 undefined
|
|
7
|
+
*/
|
|
8
|
+
export declare function getInstalledStoreRuntime(app: App): StoreRuntime | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* 将 Store 运行时安装到 Vue App
|
|
11
|
+
* 这是 Vue 适配层的核心安装入口,负责:
|
|
12
|
+
* 1. 安装 Pinia 插件
|
|
13
|
+
* 2. 通过 provide 注入 Store 能力
|
|
14
|
+
* 3. 挂载到 globalProperties
|
|
15
|
+
* 4. 打 unmount 补丁进行生命周期管理
|
|
16
|
+
* 5. 设置全局运行时引用
|
|
17
|
+
*
|
|
18
|
+
* @param app - Vue 应用实例
|
|
19
|
+
* @param runtime - Store 运行时实例
|
|
20
|
+
* @returns 安装的运行时(如果已安装则返回已有的)
|
|
21
|
+
*/
|
|
22
|
+
export declare function installStoreRuntime(app: App, runtime: StoreRuntime): StoreRuntime;
|
|
23
|
+
//# sourceMappingURL=install-store-app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"install-store-app.d.ts","sourceRoot":"","sources":["../../src/runtime/install-store-app.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;AAE9B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AA0DnD;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,GAAG,GAAG,YAAY,GAAG,SAAS,CAE3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,YAAY,GACpB,YAAY,CAuCd"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { STORE_API_SYMBOL, STORE_PINIA_SYMBOL, STORE_RUNTIME_SYMBOL } from "../context/injection-keys.js";
|
|
11
|
+
import { setGlobalStoreRuntime } from "./store-runtime.js";
|
|
12
|
+
//#region src/runtime/install-store-app.ts
|
|
13
|
+
const STORE_APP_RUNTIME_KEY = /* @__PURE__ */ Symbol("ldesign-store-app-runtime");
|
|
14
|
+
function isPiniaInstalled(app, runtime) {
|
|
15
|
+
return app.config.globalProperties.$pinia === runtime.pinia;
|
|
16
|
+
}
|
|
17
|
+
function patchUnmount(app, runtime) {
|
|
18
|
+
const originalUnmount = app.unmount;
|
|
19
|
+
app.unmount = function patchedUnmount() {
|
|
20
|
+
if (app[STORE_APP_RUNTIME_KEY] === runtime) {
|
|
21
|
+
delete app[STORE_APP_RUNTIME_KEY];
|
|
22
|
+
runtime.dispose();
|
|
23
|
+
}
|
|
24
|
+
return originalUnmount.call(this);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function getInstalledStoreRuntime(app) {
|
|
28
|
+
return app[STORE_APP_RUNTIME_KEY];
|
|
29
|
+
}
|
|
30
|
+
function installStoreRuntime(app, runtime) {
|
|
31
|
+
const storeApp = app;
|
|
32
|
+
const installedRuntime = storeApp[STORE_APP_RUNTIME_KEY];
|
|
33
|
+
if (installedRuntime) {
|
|
34
|
+
setGlobalStoreRuntime(installedRuntime);
|
|
35
|
+
return installedRuntime;
|
|
36
|
+
}
|
|
37
|
+
if (!isPiniaInstalled(app, runtime)) app.use(runtime.pinia);
|
|
38
|
+
storeApp[STORE_APP_RUNTIME_KEY] = runtime;
|
|
39
|
+
if (runtime.options.provide) {
|
|
40
|
+
app.provide(STORE_RUNTIME_SYMBOL, runtime);
|
|
41
|
+
app.provide(STORE_API_SYMBOL, runtime.storeAPI);
|
|
42
|
+
app.provide(STORE_PINIA_SYMBOL, runtime.pinia);
|
|
43
|
+
app.provide("store", runtime.storeAPI);
|
|
44
|
+
app.provide("pinia", runtime.pinia);
|
|
45
|
+
}
|
|
46
|
+
if (runtime.options.globalProperties) app.config.globalProperties.$store = runtime.storeAPI;
|
|
47
|
+
patchUnmount(storeApp, runtime);
|
|
48
|
+
setGlobalStoreRuntime(runtime);
|
|
49
|
+
return runtime;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { getInstalledStoreRuntime, installStoreRuntime };
|
|
53
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
54
|
+
//# sourceMappingURL=install-store-app.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"install-store-app.js","names":[],"sources":["../../src/runtime/install-store-app.ts"],"sourcesContent":["/* ============================================================\n * install-store-app.ts\n * Store 运行时安装到 Vue App 的模块\n * 负责将 StoreRuntime 安装到 Vue 应用实例,管理 Pinia 注入、\n * provide/globalProperties 注入和 unmount 生命周期清理\n * ============================================================ */\n\n/* Vue App 类型 */\nimport type { App } from 'vue'\n/* Store 运行时类型 */\nimport type { StoreRuntime } from './store-runtime'\n/* 从注入键模块导入 Symbol 常量 */\nimport {\n STORE_API_SYMBOL, /* Store API 的注入键 */\n STORE_PINIA_SYMBOL, /* Pinia 实例的注入键 */\n STORE_RUNTIME_SYMBOL, /* 运行时的注入键 */\n} from '../context/injection-keys'\n/* 导入全局运行时设置函数 */\nimport { setGlobalStoreRuntime } from './store-runtime'\n\n/* 用于标记 App 上已安装的运行时的 Symbol 键 */\nconst STORE_APP_RUNTIME_KEY = Symbol('ldesign-store-app-runtime')\n\n/* 扩展 Vue App 类型,添加运行时引用 */\ntype StoreApp = App & {\n [STORE_APP_RUNTIME_KEY]?: StoreRuntime\n}\n\n/**\n * 检查 Pinia 是否已安装到 Vue App\n * 通过比较 app.config.globalProperties.$pinia 与 runtime 的 pinia 实例\n *\n * @param app - Vue 应用实例\n * @param runtime - Store 运行时\n * @returns Pinia 是否已安装\n */\nfunction isPiniaInstalled(app: App, runtime: StoreRuntime): boolean {\n return app.config.globalProperties.$pinia === runtime.pinia\n}\n\n/**\n * 为 Vue App 的 unmount 方法打补丁\n * 在应用卸载时自动清理 Store 运行时(dispose)\n * 确保资源不会泄漏\n *\n * @param app - Vue 应用实例\n * @param runtime - Store 运行时\n */\nfunction patchUnmount(app: StoreApp, runtime: StoreRuntime): void {\n /* 保存原始 unmount 方法 */\n const originalUnmount = app.unmount\n\n /* 替换为带清理逻辑的版本 */\n app.unmount = function patchedUnmount() {\n /* 检查当前运行时是否仍是这个 runtime(防止被后续安装覆盖) */\n const activeRuntime = app[STORE_APP_RUNTIME_KEY]\n if (activeRuntime === runtime) {\n /* 清除 App 上的运行时引用 */\n delete app[STORE_APP_RUNTIME_KEY]\n /* 执行运行时的 dispose(清理持久化、注册表等) */\n runtime.dispose()\n }\n\n /* 调用原始 unmount 方法 */\n return originalUnmount.call(this)\n }\n}\n\n/**\n * 获取 Vue App 上已安装的 Store 运行时\n * @param app - Vue 应用实例\n * @returns 已安装的运行时或 undefined\n */\nexport function getInstalledStoreRuntime(app: App): StoreRuntime | undefined {\n return (app as StoreApp)[STORE_APP_RUNTIME_KEY]\n}\n\n/**\n * 将 Store 运行时安装到 Vue App\n * 这是 Vue 适配层的核心安装入口,负责:\n * 1. 安装 Pinia 插件\n * 2. 通过 provide 注入 Store 能力\n * 3. 挂载到 globalProperties\n * 4. 打 unmount 补丁进行生命周期管理\n * 5. 设置全局运行时引用\n *\n * @param app - Vue 应用实例\n * @param runtime - Store 运行时实例\n * @returns 安装的运行时(如果已安装则返回已有的)\n */\nexport function installStoreRuntime(\n app: App,\n runtime: StoreRuntime,\n): StoreRuntime {\n const storeApp = app as StoreApp\n /* 检查是否已安装(幂等性) */\n const installedRuntime = storeApp[STORE_APP_RUNTIME_KEY]\n\n if (installedRuntime) {\n /* 已安装则设置全局引用并返回已有的运行时 */\n setGlobalStoreRuntime(installedRuntime)\n return installedRuntime\n }\n\n /* 如果 Pinia 尚未安装,使用 app.use() 安装 */\n if (!isPiniaInstalled(app, runtime)) {\n app.use(runtime.pinia)\n }\n\n /* 在 App 上保存运行时引用(标记已安装) */\n storeApp[STORE_APP_RUNTIME_KEY] = runtime\n\n /* 如果配置了 provide 注入,通过 Vue 的 provide/inject 注入 Store 能力 */\n if (runtime.options.provide) {\n app.provide(STORE_RUNTIME_SYMBOL, runtime) /* 注入运行时 */\n app.provide(STORE_API_SYMBOL, runtime.storeAPI) /* 注入 Store API */\n app.provide(STORE_PINIA_SYMBOL, runtime.pinia) /* 注入 Pinia 实例 */\n app.provide('store', runtime.storeAPI) /* 简写键 'store' */\n app.provide('pinia', runtime.pinia) /* 简写键 'pinia' */\n }\n\n /* 如果配置了 globalProperties,挂载到全局属性上 */\n if (runtime.options.globalProperties) {\n app.config.globalProperties.$store = runtime.storeAPI\n }\n\n /* 打 unmount 补丁,确保卸载时自动清理 */\n patchUnmount(storeApp, runtime)\n /* 设置为全局运行时引用 */\n setGlobalStoreRuntime(runtime)\n\n return runtime\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,MAAM,wBAAwB,uBAAO,2BAA2B;AAehE,SAAS,iBAAiB,KAAU,SAAgC;CAClE,OAAO,IAAI,OAAO,iBAAiB,WAAW,QAAQ;AACxD;AAUA,SAAS,aAAa,KAAe,SAA6B;CAEhE,MAAM,kBAAkB,IAAI;CAG5B,IAAI,UAAU,SAAS,iBAAiB;EAGtC,IADsB,IAAI,2BACJ,SAAS;GAE7B,OAAO,IAAI;GAEX,QAAQ,QAAQ;EAClB;EAGA,OAAO,gBAAgB,KAAK,IAAI;CAClC;AACF;AAOO,SAAS,yBAAyB,KAAoC;CAC3E,OAAQ,IAAiB;AAC3B;AAeO,SAAS,oBACd,KACA,SACc;CACd,MAAM,WAAW;CAEjB,MAAM,mBAAmB,SAAS;CAElC,IAAI,kBAAkB;EAEpB,sBAAsB,gBAAgB;EACtC,OAAO;CACT;CAGA,IAAI,CAAC,iBAAiB,KAAK,OAAO,GAChC,IAAI,IAAI,QAAQ,KAAK;CAIvB,SAAS,yBAAyB;CAGlC,IAAI,QAAQ,QAAQ,SAAS;EAC3B,IAAI,QAAQ,sBAAsB,OAAO;EACzC,IAAI,QAAQ,kBAAkB,QAAQ,QAAQ;EAC9C,IAAI,QAAQ,oBAAoB,QAAQ,KAAK;EAC7C,IAAI,QAAQ,SAAS,QAAQ,QAAQ;EACrC,IAAI,QAAQ,SAAS,QAAQ,KAAK;CACpC;CAGA,IAAI,QAAQ,QAAQ,kBAClB,IAAI,OAAO,iBAAiB,SAAS,QAAQ;CAI/C,aAAa,UAAU,OAAO;CAE9B,sBAAsB,OAAO;CAE7B,OAAO;AACT"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Pinia } from 'pinia';
|
|
2
|
+
import type { EnhancedPersistPluginOptions } from '../plugins/pinia-plugin';
|
|
3
|
+
/**
|
|
4
|
+
* store 运行时配置选项接口
|
|
5
|
+
* 用于配置 store 运行时的行为
|
|
6
|
+
*/
|
|
7
|
+
export interface StoreRuntimeOptions {
|
|
8
|
+
pinia?: Pinia;
|
|
9
|
+
persist?: boolean;
|
|
10
|
+
persistOptions?: EnhancedPersistPluginOptions;
|
|
11
|
+
globalProperties?: boolean;
|
|
12
|
+
provide?: boolean;
|
|
13
|
+
debug?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* store Vue 插件选项接口
|
|
17
|
+
* 继承自 StoreRuntimeOptions,当前无额外扩展
|
|
18
|
+
*/
|
|
19
|
+
export interface StoreVuePluginOptions extends StoreRuntimeOptions {
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 解析后的 store 运行时选项接口
|
|
23
|
+
* 所有字段均已填充默认值,不再包含可选标记
|
|
24
|
+
*/
|
|
25
|
+
export interface ResolvedStoreRuntimeOptions {
|
|
26
|
+
pinia?: Pinia;
|
|
27
|
+
persist: boolean;
|
|
28
|
+
persistOptions: EnhancedPersistPluginOptions;
|
|
29
|
+
globalProperties: boolean;
|
|
30
|
+
provide: boolean;
|
|
31
|
+
debug: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 将用户传入的 store 运行时选项规范化为完整配置
|
|
35
|
+
* 为每个可选字段填充默认值
|
|
36
|
+
* @param options - 用户传入的配置选项
|
|
37
|
+
* @returns 规范化后的完整配置对象
|
|
38
|
+
*/
|
|
39
|
+
export declare function normalizeStoreRuntimeOptions(options?: StoreRuntimeOptions): ResolvedStoreRuntimeOptions;
|
|
40
|
+
//# sourceMappingURL=store-plugin-options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-plugin-options.d.ts","sourceRoot":"","sources":["../../src/runtime/store-plugin-options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAElC,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAA;AAE3E;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAElC,KAAK,CAAC,EAAE,KAAK,CAAA;IAEb,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB,cAAc,CAAC,EAAE,4BAA4B,CAAA;IAE7C,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAE1B,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,qBAAsB,SAAQ,mBAAmB;CAAG;AAErE;;;GAGG;AACH,MAAM,WAAW,2BAA2B;IAE1C,KAAK,CAAC,EAAE,KAAK,CAAA;IAEb,OAAO,EAAE,OAAO,CAAA;IAEhB,cAAc,EAAE,4BAA4B,CAAA;IAE5C,gBAAgB,EAAE,OAAO,CAAA;IAEzB,OAAO,EAAE,OAAO,CAAA;IAEhB,KAAK,EAAE,OAAO,CAAA;CACf;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,mBAAwB,GAChC,2BAA2B,CAS7B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
//#region src/runtime/store-plugin-options.ts
|
|
11
|
+
function normalizeStoreRuntimeOptions(options = {}) {
|
|
12
|
+
return {
|
|
13
|
+
pinia: options.pinia,
|
|
14
|
+
persist: options.persist ?? false,
|
|
15
|
+
persistOptions: options.persistOptions ?? {},
|
|
16
|
+
globalProperties: options.globalProperties ?? true,
|
|
17
|
+
provide: options.provide ?? true,
|
|
18
|
+
debug: options.debug ?? false
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { normalizeStoreRuntimeOptions };
|
|
23
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
24
|
+
//# sourceMappingURL=store-plugin-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-plugin-options.js","names":[],"sources":["../../src/runtime/store-plugin-options.ts"],"sourcesContent":["// 从 pinia 导入 Pinia 类型\nimport type { Pinia } from 'pinia'\n// 导入增强持久化插件选项类型\nimport type { EnhancedPersistPluginOptions } from '../plugins/pinia-plugin'\n\n/**\n * store 运行时配置选项接口\n * 用于配置 store 运行时的行为\n */\nexport interface StoreRuntimeOptions {\n // 可选的 Pinia 实例,如果不提供则自动创建\n pinia?: Pinia\n // 是否启用持久化功能\n persist?: boolean\n // 持久化插件的详细配置选项\n persistOptions?: EnhancedPersistPluginOptions\n // 是否将 store API 注册到 Vue 全局属性\n globalProperties?: boolean\n // 是否通过 provide/inject 提供 store 实例\n provide?: boolean\n // 是否开启调试模式\n debug?: boolean\n}\n\n/**\n * store Vue 插件选项接口\n * 继承自 StoreRuntimeOptions,当前无额外扩展\n */\nexport interface StoreVuePluginOptions extends StoreRuntimeOptions {}\n\n/**\n * 解析后的 store 运行时选项接口\n * 所有字段均已填充默认值,不再包含可选标记\n */\nexport interface ResolvedStoreRuntimeOptions {\n // Pinia 实例\n pinia?: Pinia\n // 是否启用持久化\n persist: boolean\n // 持久化插件配置\n persistOptions: EnhancedPersistPluginOptions\n // 是否注册全局属性\n globalProperties: boolean\n // 是否通过 provide 提供\n provide: boolean\n // 是否开启调试\n debug: boolean\n}\n\n/**\n * 将用户传入的 store 运行时选项规范化为完整配置\n * 为每个可选字段填充默认值\n * @param options - 用户传入的配置选项\n * @returns 规范化后的完整配置对象\n */\nexport function normalizeStoreRuntimeOptions(\n options: StoreRuntimeOptions = {},\n): ResolvedStoreRuntimeOptions {\n return {\n pinia: options.pinia, // 透传 Pinia 实例\n persist: options.persist ?? false, // 默认不启用持久化\n persistOptions: options.persistOptions ?? {}, // 默认空持久化配置\n globalProperties: options.globalProperties ?? true, // 默认注册全局属性\n provide: options.provide ?? true, // 默认通过 provide 提供\n debug: options.debug ?? false, // 默认关闭调试\n }\n}\n"],"mappings":";;;;;;;;;;AAuDO,SAAS,6BACd,UAA+B,CAAC,GACH;CAC7B,OAAO;EACL,OAAO,QAAQ;EACf,SAAS,QAAQ,WAAW;EAC5B,gBAAgB,QAAQ,kBAAkB,CAAC;EAC3C,kBAAkB,QAAQ,oBAAoB;EAC9C,SAAS,QAAQ,WAAW;EAC5B,OAAO,QAAQ,SAAS;CAC1B;AACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Pinia, PiniaPluginContext, StoreGeneric } from 'pinia';
|
|
2
|
+
import type { GlobalStoreAPI } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Store 注册表接口
|
|
5
|
+
* 提供对所有活跃 Store 的集中管理能力
|
|
6
|
+
* @property stores - Store 实例映射(storeId → Store)
|
|
7
|
+
* @property register - 注册一个 Store
|
|
8
|
+
* @property unregister - 注销一个 Store
|
|
9
|
+
* @property hasStore - 检查 Store 是否已注册
|
|
10
|
+
* @property getStore - 获取指定 ID 的 Store
|
|
11
|
+
* @property getStoreIds - 获取所有已注册的 Store ID
|
|
12
|
+
* @property resetAll - 重置所有 Store 到初始状态
|
|
13
|
+
* @property clear - 清空注册表(不重置 Store)
|
|
14
|
+
* @property registerExistingStores - 从 Pinia 实例中批量注册已有的 Store
|
|
15
|
+
* @property createStoreAPI - 创建全局 Store API 对象
|
|
16
|
+
*/
|
|
17
|
+
export interface StoreRegistry {
|
|
18
|
+
readonly stores: Map<string, StoreGeneric>;
|
|
19
|
+
register: (store: StoreGeneric) => void;
|
|
20
|
+
unregister: (storeId: string) => void;
|
|
21
|
+
hasStore: (storeId: string) => boolean;
|
|
22
|
+
getStore: <T = unknown>(storeId: string) => T | undefined;
|
|
23
|
+
getStoreIds: () => string[];
|
|
24
|
+
resetAll: () => void;
|
|
25
|
+
clear: () => void;
|
|
26
|
+
registerExistingStores: (pinia: Pinia) => void;
|
|
27
|
+
createStoreAPI: (pinia: Pinia) => GlobalStoreAPI;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 创建 Store 注册表实例
|
|
31
|
+
* @returns StoreRegistry 注册表对象
|
|
32
|
+
*/
|
|
33
|
+
export declare function createStoreRegistry(): StoreRegistry;
|
|
34
|
+
/**
|
|
35
|
+
* 设置全局注册表
|
|
36
|
+
* 传 null 则创建新的空注册表
|
|
37
|
+
* @param registry - 新的注册表或 null
|
|
38
|
+
*/
|
|
39
|
+
export declare function setGlobalStoreRegistry(registry: StoreRegistry | null): void;
|
|
40
|
+
/**
|
|
41
|
+
* 获取全局 Store 映射(兼容旧接口)
|
|
42
|
+
* @returns Store 映射(类型擦除版)
|
|
43
|
+
*/
|
|
44
|
+
export declare function getStoreRegistry(): Map<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* 创建注册表方法集合
|
|
47
|
+
* 从注册表中提取常用的查询方法
|
|
48
|
+
* @param registry - 注册表实例,默认使用全局注册表
|
|
49
|
+
* @returns 方法集合对象
|
|
50
|
+
*/
|
|
51
|
+
export declare function createRegistryMethods(registry?: StoreRegistry): {
|
|
52
|
+
getStore: <T = unknown>(storeId: string) => T | undefined;
|
|
53
|
+
hasStore: (storeId: string) => boolean;
|
|
54
|
+
getStoreIds: () => string[];
|
|
55
|
+
resetAll: () => void;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* 创建注册表 Pinia 插件
|
|
59
|
+
* 当 Store 被创建/使用时自动注册到注册表
|
|
60
|
+
* @param registry - 注册表实例,默认使用全局注册表
|
|
61
|
+
* @returns Pinia 插件函数
|
|
62
|
+
*/
|
|
63
|
+
export declare function createStoreRegistryPlugin(registry?: StoreRegistry): ({ store }: PiniaPluginContext) => void;
|
|
64
|
+
//# sourceMappingURL=store-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-registry.d.ts","sourceRoot":"","sources":["../../src/runtime/store-registry.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAEpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAe9C;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;IAC1C,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACvC,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IACrC,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAA;IACtC,QAAQ,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC,GAAG,SAAS,CAAA;IACzD,WAAW,EAAE,MAAM,MAAM,EAAE,CAAA;IAC3B,QAAQ,EAAE,MAAM,IAAI,CAAA;IACpB,KAAK,EAAE,MAAM,IAAI,CAAA;IACjB,sBAAsB,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAC9C,cAAc,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,cAAc,CAAA;CACjD;AAgCD;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,aAAa,CAoHnD;AAOD;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAE3E;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAEvD;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,GAAE,aAA8B;eA5LjE,CAAC,qBAAqB,MAAM,KAAK,CAAC,GAAG,SAAS;wBADrC,MAAM,KAAK,OAAO;uBAEnB,MAAM,EAAE;oBACX,IAAI;EAiMrB;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,QAAQ,GAAE,aAA8B,IACxE,WAAW,kBAAkB,KAAG,IAAI,CAG7C"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
//#region src/runtime/store-registry.ts
|
|
11
|
+
const STORE_DISPOSE_PATCH_KEY = /* @__PURE__ */ Symbol("ldesign-store-registry-dispose");
|
|
12
|
+
function patchStoreDispose(store, unregister) {
|
|
13
|
+
const restorePatchedDispose = store[STORE_DISPOSE_PATCH_KEY];
|
|
14
|
+
if (restorePatchedDispose) restorePatchedDispose();
|
|
15
|
+
const originalDispose = store.$dispose;
|
|
16
|
+
store.$dispose = function patchedDispose() {
|
|
17
|
+
unregister(store.$id);
|
|
18
|
+
return originalDispose.call(this);
|
|
19
|
+
};
|
|
20
|
+
store[STORE_DISPOSE_PATCH_KEY] = () => {
|
|
21
|
+
store.$dispose = originalDispose;
|
|
22
|
+
delete store[STORE_DISPOSE_PATCH_KEY];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function createStoreRegistry() {
|
|
26
|
+
const stores = /* @__PURE__ */ new Map();
|
|
27
|
+
const unregister = (storeId) => {
|
|
28
|
+
stores.delete(storeId);
|
|
29
|
+
};
|
|
30
|
+
const register = (store) => {
|
|
31
|
+
stores.set(store.$id, store);
|
|
32
|
+
patchStoreDispose(store, unregister);
|
|
33
|
+
};
|
|
34
|
+
const hasStore = (storeId) => {
|
|
35
|
+
return stores.has(storeId);
|
|
36
|
+
};
|
|
37
|
+
const getStore = (storeId) => {
|
|
38
|
+
return stores.get(storeId);
|
|
39
|
+
};
|
|
40
|
+
const getStoreIds = () => {
|
|
41
|
+
return Array.from(stores.keys());
|
|
42
|
+
};
|
|
43
|
+
const resetAll = () => {
|
|
44
|
+
stores.forEach((store) => {
|
|
45
|
+
if (typeof store.$reset !== "function") return;
|
|
46
|
+
try {
|
|
47
|
+
store.$reset();
|
|
48
|
+
} catch {}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const clear = () => {
|
|
52
|
+
stores.clear();
|
|
53
|
+
};
|
|
54
|
+
const registerExistingStores = (pinia) => {
|
|
55
|
+
pinia._s?.forEach((store) => register(store));
|
|
56
|
+
};
|
|
57
|
+
const createStoreAPI = (pinia) => {
|
|
58
|
+
return {
|
|
59
|
+
pinia,
|
|
60
|
+
getStore,
|
|
61
|
+
hasStore,
|
|
62
|
+
getStoreIds,
|
|
63
|
+
resetAll
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
stores,
|
|
68
|
+
register,
|
|
69
|
+
unregister,
|
|
70
|
+
hasStore,
|
|
71
|
+
getStore,
|
|
72
|
+
getStoreIds,
|
|
73
|
+
resetAll,
|
|
74
|
+
clear,
|
|
75
|
+
registerExistingStores,
|
|
76
|
+
createStoreAPI
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
let globalRegistry = createStoreRegistry();
|
|
80
|
+
function setGlobalStoreRegistry(registry) {
|
|
81
|
+
globalRegistry = registry ?? createStoreRegistry();
|
|
82
|
+
}
|
|
83
|
+
function getStoreRegistry() {
|
|
84
|
+
return globalRegistry.stores;
|
|
85
|
+
}
|
|
86
|
+
function createRegistryMethods(registry = globalRegistry) {
|
|
87
|
+
return {
|
|
88
|
+
getStore: registry.getStore,
|
|
89
|
+
hasStore: registry.hasStore,
|
|
90
|
+
getStoreIds: registry.getStoreIds,
|
|
91
|
+
resetAll: registry.resetAll
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function createStoreRegistryPlugin(registry = globalRegistry) {
|
|
95
|
+
return ({ store }) => {
|
|
96
|
+
registry.register(store);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { createRegistryMethods, createStoreRegistry, createStoreRegistryPlugin, getStoreRegistry, setGlobalStoreRegistry };
|
|
101
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
102
|
+
//# sourceMappingURL=store-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-registry.js","names":[],"sources":["../../src/runtime/store-registry.ts"],"sourcesContent":["/* ============================================================\n * store-registry.ts\n * Store 注册表模块\n * 管理所有活跃的 Pinia Store 实例,提供注册/注销/查询/重置能力\n * 通过 patch $dispose 方法实现 Store 销毁时自动注销\n * ============================================================ */\n\n/* 从 Pinia 导入类型 */\nimport type { Pinia, PiniaPluginContext, StoreGeneric } from 'pinia'\n/* 导入全局 Store API 类型 */\nimport type { GlobalStoreAPI } from '../types'\n\n/* 用于标记 store 的 $dispose 是否已被 patch 的 Symbol 键 */\nconst STORE_DISPOSE_PATCH_KEY = Symbol('ldesign-store-registry-dispose')\n\n/* 可被 patch $dispose 的 Store 扩展类型 */\ntype DisposableStore = StoreGeneric & {\n [STORE_DISPOSE_PATCH_KEY]?: () => void /* 恢复原始 $dispose 的函数 */\n}\n\n/* 扩展 Pinia 类型,访问内部的 _s(Store 实例映射) */\ntype PiniaWithStores = Pinia & {\n _s?: Map<string, StoreGeneric>\n}\n\n/**\n * Store 注册表接口\n * 提供对所有活跃 Store 的集中管理能力\n * @property stores - Store 实例映射(storeId → Store)\n * @property register - 注册一个 Store\n * @property unregister - 注销一个 Store\n * @property hasStore - 检查 Store 是否已注册\n * @property getStore - 获取指定 ID 的 Store\n * @property getStoreIds - 获取所有已注册的 Store ID\n * @property resetAll - 重置所有 Store 到初始状态\n * @property clear - 清空注册表(不重置 Store)\n * @property registerExistingStores - 从 Pinia 实例中批量注册已有的 Store\n * @property createStoreAPI - 创建全局 Store API 对象\n */\nexport interface StoreRegistry {\n readonly stores: Map<string, StoreGeneric>\n register: (store: StoreGeneric) => void\n unregister: (storeId: string) => void\n hasStore: (storeId: string) => boolean\n getStore: <T = unknown>(storeId: string) => T | undefined\n getStoreIds: () => string[]\n resetAll: () => void\n clear: () => void\n registerExistingStores: (pinia: Pinia) => void\n createStoreAPI: (pinia: Pinia) => GlobalStoreAPI\n}\n\n/**\n * 为 Store 的 $dispose 方法打补丁\n * 在 Store 被销毁时自动从注册表中注销\n * 确保注册表中的 Store 引用不会过期\n *\n * @param store - 目标 Store\n * @param unregister - 注销回调函数\n */\nfunction patchStoreDispose(store: DisposableStore, unregister: (storeId: string) => void): void {\n /* 如果之前已有补丁,先恢复原始方法 */\n const restorePatchedDispose = store[STORE_DISPOSE_PATCH_KEY]\n if (restorePatchedDispose) {\n restorePatchedDispose()\n }\n\n /* 保存原始 $dispose 方法 */\n const originalDispose = store.$dispose\n /* 替换为带注销逻辑的版本 */\n store.$dispose = function patchedDispose() {\n unregister(store.$id) /* 从注册表注销 */\n return originalDispose.call(this) /* 调用原始 dispose */\n }\n\n /* 保存恢复函数,供后续补丁使用 */\n store[STORE_DISPOSE_PATCH_KEY] = () => {\n store.$dispose = originalDispose\n delete store[STORE_DISPOSE_PATCH_KEY]\n }\n}\n\n/**\n * 创建 Store 注册表实例\n * @returns StoreRegistry 注册表对象\n */\nexport function createStoreRegistry(): StoreRegistry {\n /* Store 实例映射(storeId → Store) */\n const stores = new Map<string, StoreGeneric>()\n\n /**\n * 注销指定 ID 的 Store\n * @param storeId - Store ID\n */\n const unregister = (storeId: string): void => {\n stores.delete(storeId)\n }\n\n /**\n * 注册一个 Store\n * 同时为 $dispose 打补丁,确保销毁时自动注销\n * @param store - 要注册的 Store 实例\n */\n const register = (store: StoreGeneric): void => {\n stores.set(store.$id, store)\n patchStoreDispose(store as DisposableStore, unregister)\n }\n\n /**\n * 检查 Store 是否已注册\n * @param storeId - Store ID\n * @returns 是否已注册\n */\n const hasStore = (storeId: string): boolean => {\n return stores.has(storeId)\n }\n\n /**\n * 获取指定 ID 的 Store\n * @param storeId - Store ID\n * @returns Store 实例或 undefined\n */\n const getStore = <T = unknown>(storeId: string): T | undefined => {\n return stores.get(storeId) as T | undefined\n }\n\n /**\n * 获取所有已注册的 Store ID 列表\n * @returns Store ID 数组\n */\n const getStoreIds = (): string[] => {\n return Array.from(stores.keys())\n }\n\n /**\n * 重置所有 Store 到初始状态\n * 调用每个 Store 的 $reset() 方法\n * 注意:Setup stores 可能不支持 $reset\n */\n const resetAll = (): void => {\n stores.forEach((store) => {\n /* 检查是否有 $reset 方法 */\n if (typeof store.$reset !== 'function') {\n return\n }\n\n try {\n store.$reset()\n }\n catch {\n /* Setup stores 可能暴露了 $reset 但实际拒绝直接重置 */\n }\n })\n }\n\n /**\n * 清空注册表\n * 只清除映射,不重置 Store\n */\n const clear = (): void => {\n stores.clear()\n }\n\n /**\n * 从 Pinia 实例中批量注册已有的 Store\n * 访问 Pinia 内部的 _s 映射(所有已创建的 Store)\n * @param pinia - Pinia 实例\n */\n const registerExistingStores = (pinia: Pinia): void => {\n const typedPinia = pinia as PiniaWithStores\n typedPinia._s?.forEach(store => register(store))\n }\n\n /**\n * 创建全局 Store API 对象\n * 提供只读的 Store 查询和管理能力\n * @param pinia - Pinia 实例\n * @returns GlobalStoreAPI 对象\n */\n const createStoreAPI = (pinia: Pinia): GlobalStoreAPI => {\n return {\n pinia, /* Pinia 实例引用 */\n getStore, /* 获取 Store */\n hasStore, /* 检查 Store 是否存在 */\n getStoreIds, /* 获取所有 Store ID */\n resetAll, /* 重置所有 Store */\n }\n }\n\n /* 返回完整的注册表对象 */\n return {\n stores,\n register,\n unregister,\n hasStore,\n getStore,\n getStoreIds,\n resetAll,\n clear,\n registerExistingStores,\n createStoreAPI,\n }\n}\n\n/* ── 全局注册表单例 ── */\n\n/* 全局注册表实例(模块级单例) */\nlet globalRegistry = createStoreRegistry()\n\n/**\n * 设置全局注册表\n * 传 null 则创建新的空注册表\n * @param registry - 新的注册表或 null\n */\nexport function setGlobalStoreRegistry(registry: StoreRegistry | null): void {\n globalRegistry = registry ?? createStoreRegistry()\n}\n\n/**\n * 获取全局 Store 映射(兼容旧接口)\n * @returns Store 映射(类型擦除版)\n */\nexport function getStoreRegistry(): Map<string, unknown> {\n return globalRegistry.stores as Map<string, unknown>\n}\n\n/**\n * 创建注册表方法集合\n * 从注册表中提取常用的查询方法\n * @param registry - 注册表实例,默认使用全局注册表\n * @returns 方法集合对象\n */\nexport function createRegistryMethods(registry: StoreRegistry = globalRegistry) {\n return {\n getStore: registry.getStore,\n hasStore: registry.hasStore,\n getStoreIds: registry.getStoreIds,\n resetAll: registry.resetAll,\n }\n}\n\n/**\n * 创建注册表 Pinia 插件\n * 当 Store 被创建/使用时自动注册到注册表\n * @param registry - 注册表实例,默认使用全局注册表\n * @returns Pinia 插件函数\n */\nexport function createStoreRegistryPlugin(registry: StoreRegistry = globalRegistry) {\n return ({ store }: PiniaPluginContext): void => {\n registry.register(store)\n }\n}\n"],"mappings":";;;;;;;;;;AAaA,MAAM,0BAA0B,uBAAO,gCAAgC;AA+CvE,SAAS,kBAAkB,OAAwB,YAA6C;CAE9F,MAAM,wBAAwB,MAAM;CACpC,IAAI,uBACF,sBAAsB;CAIxB,MAAM,kBAAkB,MAAM;CAE9B,MAAM,WAAW,SAAS,iBAAiB;EACzC,WAAW,MAAM,GAAG;EACpB,OAAO,gBAAgB,KAAK,IAAI;CAClC;CAGA,MAAM,iCAAiC;EACrC,MAAM,WAAW;EACjB,OAAO,MAAM;CACf;AACF;AAMO,SAAS,sBAAqC;CAEnD,MAAM,yBAAS,IAAI,IAA0B;CAM7C,MAAM,cAAc,YAA0B;EAC5C,OAAO,OAAO,OAAO;CACvB;CAOA,MAAM,YAAY,UAA8B;EAC9C,OAAO,IAAI,MAAM,KAAK,KAAK;EAC3B,kBAAkB,OAA0B,UAAU;CACxD;CAOA,MAAM,YAAY,YAA6B;EAC7C,OAAO,OAAO,IAAI,OAAO;CAC3B;CAOA,MAAM,YAAyB,YAAmC;EAChE,OAAO,OAAO,IAAI,OAAO;CAC3B;CAMA,MAAM,oBAA8B;EAClC,OAAO,MAAM,KAAK,OAAO,KAAK,CAAC;CACjC;CAOA,MAAM,iBAAuB;EAC3B,OAAO,SAAS,UAAU;GAExB,IAAI,OAAO,MAAM,WAAW,YAC1B;GAGF,IAAI;IACF,MAAM,OAAO;GACf,QACM,CAEN;EACF,CAAC;CACH;CAMA,MAAM,cAAoB;EACxB,OAAO,MAAM;CACf;CAOA,MAAM,0BAA0B,UAAuB;EAErD,MAAW,IAAI,SAAQ,UAAS,SAAS,KAAK,CAAC;CACjD;CAQA,MAAM,kBAAkB,UAAiC;EACvD,OAAO;GACL;GACA;GACA;GACA;GACA;EACF;CACF;CAGA,OAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF;AAKA,IAAI,iBAAiB,oBAAoB;AAOlC,SAAS,uBAAuB,UAAsC;CAC3E,iBAAiB,YAAY,oBAAoB;AACnD;AAMO,SAAS,mBAAyC;CACvD,OAAO,eAAe;AACxB;AAQO,SAAS,sBAAsB,WAA0B,gBAAgB;CAC9E,OAAO;EACL,UAAU,SAAS;EACnB,UAAU,SAAS;EACnB,aAAa,SAAS;EACtB,UAAU,SAAS;CACrB;AACF;AAQO,SAAS,0BAA0B,WAA0B,gBAAgB;CAClF,QAAQ,EAAE,YAAsC;EAC9C,SAAS,SAAS,KAAK;CACzB;AACF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Pinia } from 'pinia';
|
|
2
|
+
import type { GlobalStoreAPI } from '../types';
|
|
3
|
+
import type { ResolvedStoreRuntimeOptions, /* 解析后的运行时选项 */ StoreRuntimeOptions } from './store-plugin-options';
|
|
4
|
+
import type { StoreRegistry } from './store-registry';
|
|
5
|
+
/**
|
|
6
|
+
* Store 运行时接口
|
|
7
|
+
* 封装了 Pinia、注册表、API 和生命周期管理
|
|
8
|
+
* @property pinia - Pinia 实例
|
|
9
|
+
* @property registry - Store 注册表
|
|
10
|
+
* @property storeAPI - 全局 Store API
|
|
11
|
+
* @property options - 解析后的运行时选项
|
|
12
|
+
* @property dispose - 清理函数,释放所有资源
|
|
13
|
+
*/
|
|
14
|
+
export interface StoreRuntime {
|
|
15
|
+
pinia: Pinia;
|
|
16
|
+
registry: StoreRegistry;
|
|
17
|
+
storeAPI: GlobalStoreAPI;
|
|
18
|
+
options: ResolvedStoreRuntimeOptions;
|
|
19
|
+
dispose: () => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 获取全局 Store API
|
|
23
|
+
* @returns 全局 Store API 或 null(运行时未初始化时)
|
|
24
|
+
*/
|
|
25
|
+
export declare function getGlobalStoreAPI(): GlobalStoreAPI | null;
|
|
26
|
+
/**
|
|
27
|
+
* 设置全局运行时
|
|
28
|
+
* 同步更新全局注册表引用
|
|
29
|
+
* @param runtime - 新的运行时实例或 null
|
|
30
|
+
*/
|
|
31
|
+
export declare function setGlobalStoreRuntime(runtime: StoreRuntime | null): void;
|
|
32
|
+
/**
|
|
33
|
+
* 创建 Store 运行时实例
|
|
34
|
+
* 这是 Store 系统的核心工厂函数,负责:
|
|
35
|
+
* 1. 规范化配置选项
|
|
36
|
+
* 2. 创建或复用 Pinia 实例
|
|
37
|
+
* 3. 创建 Store 注册表
|
|
38
|
+
* 4. 配置持久化插件
|
|
39
|
+
* 5. 注册 Pinia 插件
|
|
40
|
+
* 6. 组装运行时对象
|
|
41
|
+
*
|
|
42
|
+
* @param options - 运行时配置选项
|
|
43
|
+
* @returns StoreRuntime 实例
|
|
44
|
+
*/
|
|
45
|
+
export declare function createStoreRuntime(options?: StoreRuntimeOptions): StoreRuntime;
|
|
46
|
+
//# sourceMappingURL=store-runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/store-runtime.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,KAAK,EAAgB,MAAM,OAAO,CAAA;AAEhD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAE9C,OAAO,KAAK,EACV,2BAA2B,EAAI,eAAe,CAC9C,mBAAmB,EACpB,MAAM,wBAAwB,CAAA;AAE/B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAyBrD;;;;;;;;GAQG;AACH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,KAAK,CAAA;IACZ,QAAQ,EAAE,aAAa,CAAA;IACvB,QAAQ,EAAE,cAAc,CAAA;IACxB,OAAO,EAAE,2BAA2B,CAAA;IACpC,OAAO,EAAE,MAAM,IAAI,CAAA;CACpB;AAKD;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,IAAI,CAEzD;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,GAAG,IAAI,CAIxE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,GAAE,mBAAwB,GAChC,YAAY,CA6Dd"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-vue v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:39:29 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { createPersistPluginController } from "../plugins/pinia-plugin.js";
|
|
11
|
+
import { normalizeStoreRuntimeOptions } from "./store-plugin-options.js";
|
|
12
|
+
import { createStoreRegistry, createStoreRegistryPlugin, setGlobalStoreRegistry } from "./store-registry.js";
|
|
13
|
+
import { createPinia } from "pinia";
|
|
14
|
+
//#region src/runtime/store-runtime.ts
|
|
15
|
+
const STORE_RUNTIME_KEY = /* @__PURE__ */ Symbol("ldesign-store-runtime");
|
|
16
|
+
let globalStoreRuntime = null;
|
|
17
|
+
function getGlobalStoreAPI() {
|
|
18
|
+
return globalStoreRuntime?.storeAPI ?? null;
|
|
19
|
+
}
|
|
20
|
+
function setGlobalStoreRuntime(runtime) {
|
|
21
|
+
globalStoreRuntime = runtime;
|
|
22
|
+
setGlobalStoreRegistry(runtime?.registry ?? null);
|
|
23
|
+
}
|
|
24
|
+
function createStoreRuntime(options = {}) {
|
|
25
|
+
const resolvedOptions = normalizeStoreRuntimeOptions(options);
|
|
26
|
+
const pinia = resolvedOptions.pinia ?? createPinia();
|
|
27
|
+
if (pinia[STORE_RUNTIME_KEY]) return pinia[STORE_RUNTIME_KEY];
|
|
28
|
+
const registry = createStoreRegistry();
|
|
29
|
+
const persistController = resolvedOptions.persist ? createPersistPluginController({
|
|
30
|
+
...resolvedOptions.persistOptions,
|
|
31
|
+
debug: resolvedOptions.debug || resolvedOptions.persistOptions.debug
|
|
32
|
+
}) : null;
|
|
33
|
+
pinia.use(createStoreRegistryPlugin(registry));
|
|
34
|
+
persistController && pinia.use(persistController.plugin);
|
|
35
|
+
registry.registerExistingStores(pinia);
|
|
36
|
+
const runtime = {
|
|
37
|
+
pinia,
|
|
38
|
+
registry,
|
|
39
|
+
storeAPI: registry.createStoreAPI(pinia),
|
|
40
|
+
options: resolvedOptions,
|
|
41
|
+
/**
|
|
42
|
+
* 清理函数
|
|
43
|
+
* 释放所有资源:清除运行时标记、持久化插件、注册表
|
|
44
|
+
*/
|
|
45
|
+
dispose: () => {
|
|
46
|
+
if (pinia[STORE_RUNTIME_KEY] === runtime) delete pinia[STORE_RUNTIME_KEY];
|
|
47
|
+
persistController?.dispose();
|
|
48
|
+
registry.clear();
|
|
49
|
+
if (globalStoreRuntime === runtime) setGlobalStoreRuntime(null);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
pinia[STORE_RUNTIME_KEY] = runtime;
|
|
53
|
+
return runtime;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { createStoreRuntime, getGlobalStoreAPI, setGlobalStoreRuntime };
|
|
57
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
58
|
+
//# sourceMappingURL=store-runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-runtime.js","names":[],"sources":["../../src/runtime/store-runtime.ts"],"sourcesContent":["/* ============================================================\n * store-runtime.ts\n * Store 运行时模块\n * 负责创建和管理 Store 运行时实例,包括 Pinia 创建、\n * 注册表初始化、持久化插件配置和生命周期管理\n * ============================================================ */\n\n/* 从 Pinia 导入类型 */\nimport type { Pinia, StoreGeneric } from 'pinia'\n/* 全局 Store API 类型 */\nimport type { GlobalStoreAPI } from '../types'\n/* 运行时选项类型 */\nimport type {\n ResolvedStoreRuntimeOptions, /* 解析后的运行时选项 */\n StoreRuntimeOptions, /* 原始运行时选项 */\n} from './store-plugin-options'\n/* 注册表类型 */\nimport type { StoreRegistry } from './store-registry'\n/* 从 Pinia 导入创建实例函数 */\nimport { createPinia } from 'pinia'\n/* 导入持久化插件控制器 */\nimport { createPersistPluginController } from '../plugins/pinia-plugin'\n/* 导入选项规范化函数 */\nimport {\n normalizeStoreRuntimeOptions,\n} from './store-plugin-options'\n/* 导入注册表相关函数 */\nimport {\n createStoreRegistry,\n createStoreRegistryPlugin,\n setGlobalStoreRegistry,\n} from './store-registry'\n\n/* 用于标记 Pinia 实例上已绑定的运行时的 Symbol 键 */\nconst STORE_RUNTIME_KEY = Symbol('ldesign-store-runtime')\n\n/* 扩展 Pinia 类型,添加运行时和 Store 映射引用 */\ntype PiniaWithRuntime = Pinia & {\n _s?: Map<string, StoreGeneric> /* Pinia 内部的 Store 实例映射 */\n [STORE_RUNTIME_KEY]?: StoreRuntime /* 绑定的运行时 */\n}\n\n/**\n * Store 运行时接口\n * 封装了 Pinia、注册表、API 和生命周期管理\n * @property pinia - Pinia 实例\n * @property registry - Store 注册表\n * @property storeAPI - 全局 Store API\n * @property options - 解析后的运行时选项\n * @property dispose - 清理函数,释放所有资源\n */\nexport interface StoreRuntime {\n pinia: Pinia\n registry: StoreRegistry\n storeAPI: GlobalStoreAPI\n options: ResolvedStoreRuntimeOptions\n dispose: () => void\n}\n\n/* 全局运行时实例(模块级单例) */\nlet globalStoreRuntime: StoreRuntime | null = null\n\n/**\n * 获取全局 Store API\n * @returns 全局 Store API 或 null(运行时未初始化时)\n */\nexport function getGlobalStoreAPI(): GlobalStoreAPI | null {\n return globalStoreRuntime?.storeAPI ?? null\n}\n\n/**\n * 设置全局运行时\n * 同步更新全局注册表引用\n * @param runtime - 新的运行时实例或 null\n */\nexport function setGlobalStoreRuntime(runtime: StoreRuntime | null): void {\n globalStoreRuntime = runtime\n /* 同步更新全局注册表 */\n setGlobalStoreRegistry(runtime?.registry ?? null)\n}\n\n/**\n * 创建 Store 运行时实例\n * 这是 Store 系统的核心工厂函数,负责:\n * 1. 规范化配置选项\n * 2. 创建或复用 Pinia 实例\n * 3. 创建 Store 注册表\n * 4. 配置持久化插件\n * 5. 注册 Pinia 插件\n * 6. 组装运行时对象\n *\n * @param options - 运行时配置选项\n * @returns StoreRuntime 实例\n */\nexport function createStoreRuntime(\n options: StoreRuntimeOptions = {},\n): StoreRuntime {\n /* 规范化配置选项(填充默认值) */\n const resolvedOptions = normalizeStoreRuntimeOptions(options)\n /* 创建或复用 Pinia 实例,扩展为带运行时标记的类型 */\n const pinia = (resolvedOptions.pinia ?? createPinia()) as PiniaWithRuntime\n\n /* 如果 Pinia 实例上已有运行时,直接复用(防止重复创建) */\n if (pinia[STORE_RUNTIME_KEY]) {\n return pinia[STORE_RUNTIME_KEY]\n }\n\n /* 创建 Store 注册表 */\n const registry = createStoreRegistry()\n\n /* 如果启用了持久化,创建持久化插件控制器 */\n const persistController = resolvedOptions.persist\n ? createPersistPluginController({\n ...resolvedOptions.persistOptions, /* 透传持久化选项 */\n debug: resolvedOptions.debug || resolvedOptions.persistOptions.debug, /* 调试模式 */\n })\n : null\n\n /* 注册注册表插件(自动注册新创建的 Store) */\n pinia.use(createStoreRegistryPlugin(registry))\n /* 如果有持久化插件,也注册到 Pinia */\n persistController && pinia.use(persistController.plugin)\n /* 批量注册 Pinia 中已有的 Store */\n registry.registerExistingStores(pinia)\n\n /* 组装运行时对象 */\n const runtime: StoreRuntime = {\n pinia, /* Pinia 实例 */\n registry, /* Store 注册表 */\n storeAPI: registry.createStoreAPI(pinia), /* 全局 Store API */\n options: resolvedOptions, /* 解析后的选项 */\n\n /**\n * 清理函数\n * 释放所有资源:清除运行时标记、持久化插件、注册表\n */\n dispose: () => {\n /* 清除 Pinia 上的运行时标记 */\n if (pinia[STORE_RUNTIME_KEY] === runtime) {\n delete pinia[STORE_RUNTIME_KEY]\n }\n\n /* 销毁持久化插件(取消订阅、移除事件监听等) */\n persistController?.dispose()\n /* 清空注册表 */\n registry.clear()\n\n /* 如果当前是全局运行时,也清除全局引用 */\n if (globalStoreRuntime === runtime) {\n setGlobalStoreRuntime(null)\n }\n },\n }\n\n /* 在 Pinia 实例上保存运行时引用(防止重复创建) */\n pinia[STORE_RUNTIME_KEY] = runtime\n return runtime\n}\n"],"mappings":";;;;;;;;;;;;;;AAkCA,MAAM,oBAAoB,uBAAO,uBAAuB;AA0BxD,IAAI,qBAA0C;AAMvC,SAAS,oBAA2C;CACzD,OAAO,oBAAoB,YAAY;AACzC;AAOO,SAAS,sBAAsB,SAAoC;CACxE,qBAAqB;CAErB,uBAAuB,SAAS,YAAY,IAAI;AAClD;AAeO,SAAS,mBACd,UAA+B,CAAC,GAClB;CAEd,MAAM,kBAAkB,6BAA6B,OAAO;CAE5D,MAAM,QAAS,gBAAgB,SAAS,YAAY;CAGpD,IAAI,MAAM,oBACR,OAAO,MAAM;CAIf,MAAM,WAAW,oBAAoB;CAGrC,MAAM,oBAAoB,gBAAgB,UACtC,8BAA8B;EAC5B,GAAG,gBAAgB;EACnB,OAAO,gBAAgB,SAAS,gBAAgB,eAAe;CACjE,CAAC,IACD;CAGJ,MAAM,IAAI,0BAA0B,QAAQ,CAAC;CAE7C,qBAAqB,MAAM,IAAI,kBAAkB,MAAM;CAEvD,SAAS,uBAAuB,KAAK;CAGrC,MAAM,UAAwB;EAC5B;EACA;EACA,UAAU,SAAS,eAAe,KAAK;EACvC,SAAS;;;;;EAMT,eAAe;GAEb,IAAI,MAAM,uBAAuB,SAC/B,OAAO,MAAM;GAIf,mBAAmB,QAAQ;GAE3B,SAAS,MAAM;GAGf,IAAI,uBAAuB,SACzB,sBAAsB,IAAI;EAE9B;CACF;CAGA,MAAM,qBAAqB;CAC3B,OAAO;AACT"}
|