@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":"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,94 @@
|
|
|
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 createStoreRegistryPlugin(registry = globalRegistry) {
|
|
87
|
+
return ({ store }) => {
|
|
88
|
+
registry.register(store);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { createStoreRegistry, createStoreRegistryPlugin, getStoreRegistry, setGlobalStoreRegistry };
|
|
93
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
94
|
+
//# 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;AAuBO,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"}
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { Pinia } from 'pinia';
|
|
2
|
+
import type { Ref } from 'vue';
|
|
3
|
+
/**
|
|
4
|
+
* Store 基础配置选项
|
|
5
|
+
* @property persist - 持久化配置,true 使用默认配置,或传入 PersistOptions 对象
|
|
6
|
+
* @property devtools - 是否启用 Vue DevTools 支持
|
|
7
|
+
* @property strict - 是否启用严格模式(如 Pinia 的 $patch 严格检查)
|
|
8
|
+
*/
|
|
9
|
+
export interface StoreOptions {
|
|
10
|
+
persist?: boolean | PersistOptions;
|
|
11
|
+
devtools?: boolean;
|
|
12
|
+
strict?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 持久化配置选项
|
|
16
|
+
* 控制 Pinia Store 状态如何被保存到浏览器存储
|
|
17
|
+
* @property key - 自定义存储键名(默认使用 store.$id)
|
|
18
|
+
* @property storage - 存储类型:'localStorage' 或 'sessionStorage'
|
|
19
|
+
* @property paths - 只持久化指定路径的状态(白名单模式)
|
|
20
|
+
* @property excludePaths - 排除指定路径不持久化(黑名单模式)
|
|
21
|
+
* @property debounce - 防抖延迟(毫秒),避免频繁写入
|
|
22
|
+
* @property syncTabs - 是否跨标签页同步状态
|
|
23
|
+
* @property ttl - 存活时间(毫秒),过期后自动清除
|
|
24
|
+
* @property serializer - 自定义序列化/反序列化函数
|
|
25
|
+
* @property beforeRestore - 恢复前钩子
|
|
26
|
+
* @property afterRestore - 恢复后钩子
|
|
27
|
+
*/
|
|
28
|
+
export interface PersistOptions {
|
|
29
|
+
key?: string;
|
|
30
|
+
storage?: 'localStorage' | 'sessionStorage';
|
|
31
|
+
paths?: string[];
|
|
32
|
+
excludePaths?: string[];
|
|
33
|
+
debounce?: number;
|
|
34
|
+
syncTabs?: boolean;
|
|
35
|
+
ttl?: number;
|
|
36
|
+
serializer?: {
|
|
37
|
+
serialize: (value: unknown) => string;
|
|
38
|
+
deserialize: (value: string) => unknown;
|
|
39
|
+
};
|
|
40
|
+
beforeRestore?: (context: {
|
|
41
|
+
store: unknown;
|
|
42
|
+
}) => void;
|
|
43
|
+
afterRestore?: (context: {
|
|
44
|
+
store: unknown;
|
|
45
|
+
}) => void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Store Engine 插件配置选项
|
|
49
|
+
* 控制 Store 引擎插件的行为
|
|
50
|
+
* @property name - 插件名称
|
|
51
|
+
* @property version - 插件版本
|
|
52
|
+
* @property dependencies - 依赖的其他插件列表
|
|
53
|
+
* @property persist - 是否启用持久化
|
|
54
|
+
* @property persistOptions - 持久化配置选项
|
|
55
|
+
* @property devtools - 是否启用 DevTools
|
|
56
|
+
* @property debug - 是否开启调试日志
|
|
57
|
+
* @property globalProperties - 是否挂载到 app.config.globalProperties
|
|
58
|
+
* @property provide - 是否通过 Vue provide/inject 注入
|
|
59
|
+
* @property pinia - 外部传入的 Pinia 实例(可选,不传则自动创建)
|
|
60
|
+
*/
|
|
61
|
+
export interface StoreEnginePluginOptions {
|
|
62
|
+
name?: string;
|
|
63
|
+
version?: string;
|
|
64
|
+
dependencies?: string[];
|
|
65
|
+
persist?: boolean;
|
|
66
|
+
persistOptions?: PersistOptions;
|
|
67
|
+
devtools?: boolean;
|
|
68
|
+
debug?: boolean;
|
|
69
|
+
globalProperties?: boolean;
|
|
70
|
+
provide?: boolean;
|
|
71
|
+
pinia?: Pinia;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* useStore 返回值接口
|
|
75
|
+
* 提供响应式状态和常用操作方法
|
|
76
|
+
* @property state - 响应式状态引用
|
|
77
|
+
* @property reset - 重置状态到初始值
|
|
78
|
+
* @property patch - 部分更新状态(支持对象或函数)
|
|
79
|
+
* @property store - 底层 Pinia Store 实例
|
|
80
|
+
*/
|
|
81
|
+
export interface UseStoreReturn<S = unknown> {
|
|
82
|
+
state: Ref<S>;
|
|
83
|
+
reset: () => void;
|
|
84
|
+
patch: (partial: Partial<S> | ((state: S) => void)) => void;
|
|
85
|
+
store: unknown;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* usePersist 配置选项
|
|
89
|
+
* 简化版的持久化选项,用于 composable 场景
|
|
90
|
+
* @property key - 存储键名
|
|
91
|
+
* @property storage - 存储类型
|
|
92
|
+
* @property defaultValue - 默认值
|
|
93
|
+
* @property serialize - 自定义序列化函数
|
|
94
|
+
* @property deserialize - 自定义反序列化函数
|
|
95
|
+
*/
|
|
96
|
+
export interface UsePersistOptions {
|
|
97
|
+
key: string;
|
|
98
|
+
storage?: 'localStorage' | 'sessionStorage';
|
|
99
|
+
defaultValue?: unknown;
|
|
100
|
+
serialize?: (value: unknown) => string;
|
|
101
|
+
deserialize?: (value: string) => unknown;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* usePersist 返回值接口
|
|
105
|
+
* @property value - 持久化的响应式引用
|
|
106
|
+
* @property save - 手动保存
|
|
107
|
+
* @property restore - 从存储恢复
|
|
108
|
+
* @property clear - 清除存储
|
|
109
|
+
*/
|
|
110
|
+
export interface UsePersistReturn<T> {
|
|
111
|
+
value: Ref<T>;
|
|
112
|
+
save: () => void;
|
|
113
|
+
restore: () => void;
|
|
114
|
+
clear: () => void;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 订阅选项接口
|
|
118
|
+
* 控制 Store 订阅行为
|
|
119
|
+
* @property immediate - 是否立即触发一次回调
|
|
120
|
+
* @property deep - 是否深度监听
|
|
121
|
+
* @property detached - 是否脱离当前组件生命周期(组件卸载后继续监听)
|
|
122
|
+
*/
|
|
123
|
+
export interface SubscribeOptions {
|
|
124
|
+
immediate?: boolean;
|
|
125
|
+
deep?: boolean;
|
|
126
|
+
detached?: boolean;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 变更信息接口
|
|
130
|
+
* 描述 Store 状态变更的详细信息
|
|
131
|
+
* @property type - 变更类型:'direct'(直接赋值)、'patch object'(对象补丁)、'patch function'(函数补丁)
|
|
132
|
+
* @property storeId - Store ID
|
|
133
|
+
* @property events - 触发的事件列表
|
|
134
|
+
* @property oldState - 变更前的状态(部分)
|
|
135
|
+
*/
|
|
136
|
+
export interface MutationInfo<S = unknown> {
|
|
137
|
+
type: 'direct' | 'patch object' | 'patch function';
|
|
138
|
+
storeId: string;
|
|
139
|
+
events?: string[];
|
|
140
|
+
oldState?: Partial<S>;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Store Provider 组件 Props
|
|
144
|
+
* @property pinia - Pinia 实例
|
|
145
|
+
* @property children - 子组件(React 风格,Vue 中较少使用)
|
|
146
|
+
*/
|
|
147
|
+
export interface StoreProviderProps {
|
|
148
|
+
pinia?: Pinia;
|
|
149
|
+
children?: unknown;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 全局 Store API 接口
|
|
153
|
+
* 提供只读的 Store 查询和管理能力
|
|
154
|
+
* @property pinia - Pinia 实例引用
|
|
155
|
+
* @property getStore - 获取指定 ID 的 Store
|
|
156
|
+
* @property hasStore - 检查 Store 是否存在
|
|
157
|
+
* @property getStoreIds - 获取所有已注册的 Store ID
|
|
158
|
+
* @property resetAll - 重置所有 Store 到初始状态
|
|
159
|
+
*/
|
|
160
|
+
export interface GlobalStoreAPI {
|
|
161
|
+
pinia: Pinia;
|
|
162
|
+
getStore: <T>(id: string) => T | undefined;
|
|
163
|
+
hasStore: (id: string) => boolean;
|
|
164
|
+
getStoreIds: () => string[];
|
|
165
|
+
resetAll: () => void;
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"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"}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
12
|
+
let pinia = require("pinia");
|
|
13
|
+
let vue = require("vue");
|
|
14
|
+
//#region src/composables/defineSetupStore.ts
|
|
15
|
+
function defineSetupStore(id, setup) {
|
|
16
|
+
return (0, pinia.defineStore)(id, setup);
|
|
17
|
+
}
|
|
18
|
+
function createStoreState(initial) {
|
|
19
|
+
const result = {};
|
|
20
|
+
for (const key of Object.keys(initial)) result[key] = (0, vue.ref)(initial[key]);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
function createStoreGetters(getters) {
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const key of Object.keys(getters)) result[key] = (0, vue.computed)(getters[key]);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
exports.createStoreGetters = createStoreGetters;
|
|
30
|
+
exports.createStoreState = createStoreState;
|
|
31
|
+
exports.defineSetupStore = defineSetupStore;
|
|
32
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
33
|
+
//# sourceMappingURL=defineSetupStore.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineSetupStore.cjs","names":[],"sources":["../../src/composables/defineSetupStore.ts"],"sourcesContent":["/**\n * defineSetupStore - Setup 风格 Store 定义助手\n *\n * 提供更简洁的 composition API 风格来定义 Pinia Store。\n * 本质是对 defineStore 的 setup 语法的薄封装,增加类型推断和开发体验。\n *\n * @module composables/defineSetupStore\n */\n\nimport type {ComputedRef, Ref} from 'vue';\nimport { defineStore } from 'pinia'\nimport { computed, ref } from 'vue'\n\n/**\n * defineSetupStore - 使用 Composition API 风格定义 Store\n *\n * 与 defineStore 的 setup 语法等价,但提供更清晰的语义。\n * 适合喜欢 Composition API 风格的开发者。\n *\n * @param id - Store 唯一标识\n * @param setup - Setup 函数(在函数内使用 ref、computed、普通函数来定义 state、getters、actions)\n * @returns Pinia Store 定义\n *\n * @example\n * ```typescript\n * import { defineSetupStore } from '@composy/store-vue'\n * import { ref, computed } from 'vue'\n *\n * // Setup 风格定义(推荐)\n * export const useCounterStore = defineSetupStore('counter', () => {\n * // state(使用 ref)\n * const count = ref(0)\n * const step = ref(1)\n *\n * // getters(使用 computed)\n * const doubleCount = computed(() => count.value * 2)\n * const isPositive = computed(() => count.value > 0)\n *\n * // actions(普通函数)\n * function increment() {\n * count.value += step.value\n * }\n *\n * function decrement() {\n * count.value -= step.value\n * }\n *\n * async function fetchCount(id: string) {\n * const res = await fetch(`/api/count/${id}`)\n * count.value = await res.json()\n * }\n *\n * function reset() {\n * count.value = 0\n * step.value = 1\n * }\n *\n * return {\n * count, step,\n * doubleCount, isPositive,\n * increment, decrement, fetchCount, reset,\n * }\n * })\n * ```\n */\nexport function defineSetupStore<T>(\n id: string,\n setup: () => T,\n) {\n return defineStore(id, setup)\n}\n\n/**\n * createStoreState - 创建带有类型推断的 Store State\n *\n * 工具函数,帮助在 setup store 中快速创建多个 ref 状态。\n *\n * @param initial - 初始状态对象\n * @returns 以 ref 包装的响应式状态对象\n *\n * @example\n * ```typescript\n * const useUserStore = defineSetupStore('user', () => {\n * const state = createStoreState({\n * name: '张三',\n * age: 25,\n * email: '',\n * })\n *\n * // state.name 是 Ref<string>\n * // state.age 是 Ref<number>\n *\n * function updateName(name: string) {\n * state.name.value = name\n * }\n *\n * return { ...state, updateName }\n * })\n * ```\n */\nexport function createStoreState<T extends Record<string, unknown>>(\n initial: T,\n): { [K in keyof T]: Ref<T[K]> } {\n const result = {} as { [K in keyof T]: Ref<T[K]> }\n for (const key of Object.keys(initial) as Array<keyof T>) {\n result[key] = ref(initial[key]) as Ref<T[typeof key]>\n }\n return result\n}\n\n/**\n * createStoreGetters - 创建带有类型推断的 Store Getters\n *\n * 工具函数,帮助在 setup store 中快速创建多个 computed getter。\n *\n * @param getters - getter 函数映射\n * @returns 以 computed 包装的响应式 getter 对象\n *\n * @example\n * ```typescript\n * const useUserStore = defineSetupStore('user', () => {\n * const name = ref('张三')\n * const age = ref(25)\n *\n * const getters = createStoreGetters({\n * displayName: () => `${name.value} (${age.value}岁)`,\n * isAdult: () => age.value >= 18,\n * })\n *\n * return { name, age, ...getters }\n * })\n * ```\n */\nexport function createStoreGetters<T extends Record<string, () => unknown>>(\n getters: T,\n): { [K in keyof T]: ComputedRef<ReturnType<T[K]>> } {\n const result = {} as { [K in keyof T]: ComputedRef<ReturnType<T[K]>> }\n for (const key of Object.keys(getters) as Array<keyof T>) {\n result[key] = computed(getters[key]) as ComputedRef<ReturnType<T[typeof key]>>\n }\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;AAiEO,SAAS,iBACd,IACA,OACA;CACA,QAAA,GAAA,MAAA,YAAA,CAAmB,IAAI,KAAK;AAC9B;AA8BO,SAAS,iBACd,SAC+B;CAC/B,MAAM,SAAS,CAAC;CAChB,KAAA,MAAW,OAAO,OAAO,KAAK,OAAO,GACnC,OAAO,QAAA,GAAA,IAAA,IAAA,CAAW,QAAQ,IAAI;CAEhC,OAAO;AACT;AAyBO,SAAS,mBACd,SACmD;CACnD,MAAM,SAAS,CAAC;CAChB,KAAA,MAAW,OAAO,OAAO,KAAK,OAAO,GACnC,OAAO,QAAA,GAAA,IAAA,SAAA,CAAgB,QAAQ,IAAI;CAErC,OAAO;AACT"}
|