@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,36 @@
|
|
|
1
|
+
import type { /* 引擎抽象接口 */ StoreEnginePluginContextLike } from '@composy/store-core/engine';
|
|
2
|
+
import type { App } from 'vue';
|
|
3
|
+
import type { GlobalStoreAPI, StoreEnginePluginOptions } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* Store Engine 插件接口
|
|
6
|
+
* 定义了插件的元信息和生命周期方法
|
|
7
|
+
* @property dependencies - 依赖的其他插件名称列表
|
|
8
|
+
* @property name - 插件名称
|
|
9
|
+
* @property version - 插件版本
|
|
10
|
+
* @property install - 安装到引擎上下文(异步)
|
|
11
|
+
* @property installVue - 安装到 Vue App
|
|
12
|
+
* @property uninstall - 从引擎上下文卸载(异步)
|
|
13
|
+
*/
|
|
14
|
+
export interface StoreEnginePlugin {
|
|
15
|
+
dependencies: string[];
|
|
16
|
+
name: string;
|
|
17
|
+
version: string;
|
|
18
|
+
install: (context: StoreEnginePluginContextLike) => Promise<void>;
|
|
19
|
+
installVue: (app: App) => void;
|
|
20
|
+
uninstall: (context?: StoreEnginePluginContextLike) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 获取当前活跃的全局 Store API
|
|
24
|
+
* @returns 全局 Store API 或 null(未初始化时)
|
|
25
|
+
*/
|
|
26
|
+
export declare function getGlobalStoreAPI(): GlobalStoreAPI | null;
|
|
27
|
+
/**
|
|
28
|
+
* 创建 Store Engine 插件工厂函数
|
|
29
|
+
* 这是 Vue 适配层的核心入口,将 core 层的 bridge 包装为 Vue 友好的插件
|
|
30
|
+
*
|
|
31
|
+
* @param options - 插件配置选项
|
|
32
|
+
* @returns StoreEnginePlugin 插件实例
|
|
33
|
+
*/
|
|
34
|
+
export declare function createStoreEnginePlugin(options?: StoreEnginePluginOptions): StoreEnginePlugin;
|
|
35
|
+
export default createStoreEnginePlugin;
|
|
36
|
+
//# sourceMappingURL=engine-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine-plugin.d.ts","sourceRoot":"","sources":["../../src/plugins/engine-plugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAE4B,YAAY,CAClD,4BAA4B,EAC7B,MAAM,4BAA4B,CAAA;AAEnC,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;AAI9B,OAAO,KAAK,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAA;AAQxE;;;;;;;;;GASG;AACH,MAAM,WAAW,iBAAiB;IAChC,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,CAAC,OAAO,EAAE,4BAA4B,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACjE,UAAU,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAA;IAC9B,SAAS,EAAE,CAAC,OAAO,CAAC,EAAE,4BAA4B,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CACrE;AAoCD;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,IAAI,CAEzD;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,GAAE,wBAA6B,GAAG,iBAAiB,CAwFjG;AAGD,eAAe,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,83 @@
|
|
|
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, getGlobalStoreAPI as getGlobalStoreAPI$1 } from "../runtime/store-runtime.js";
|
|
11
|
+
import { getInstalledStoreRuntime, installStoreRuntime } from "../runtime/install-store-app.js";
|
|
12
|
+
import { createStoreEngineBridge } from "@composy/store-core/engine";
|
|
13
|
+
//#region src/plugins/engine-plugin.ts
|
|
14
|
+
function isVueApp(value) {
|
|
15
|
+
return !!value && typeof value.use === "function";
|
|
16
|
+
}
|
|
17
|
+
function resolveVueApp(engine, context, payload) {
|
|
18
|
+
const appPayload = payload;
|
|
19
|
+
const app = appPayload?.app ?? appPayload?.framework?.app ?? engine.getApp?.() ?? context.framework?.app;
|
|
20
|
+
return isVueApp(app) ? app : void 0;
|
|
21
|
+
}
|
|
22
|
+
function getGlobalStoreAPI() {
|
|
23
|
+
return getGlobalStoreAPI$1();
|
|
24
|
+
}
|
|
25
|
+
function createStoreEnginePlugin(options = {}) {
|
|
26
|
+
const { debug = false, dependencies = [], name = "store", version = "1.0.0" } = options;
|
|
27
|
+
let bridge = null;
|
|
28
|
+
const createBridge = () => {
|
|
29
|
+
const activeBridge = createStoreEngineBridge({
|
|
30
|
+
name,
|
|
31
|
+
version,
|
|
32
|
+
debug,
|
|
33
|
+
createRuntime: () => createStoreRuntime({
|
|
34
|
+
pinia: options.pinia,
|
|
35
|
+
persist: options.persist,
|
|
36
|
+
persistOptions: options.persistOptions,
|
|
37
|
+
globalProperties: options.globalProperties,
|
|
38
|
+
provide: options.provide,
|
|
39
|
+
debug
|
|
40
|
+
}),
|
|
41
|
+
attachToApp: (app, runtime) => installStoreRuntime(app, runtime),
|
|
42
|
+
getRuntimeFromApp: (app) => getInstalledStoreRuntime(app),
|
|
43
|
+
isApp: isVueApp,
|
|
44
|
+
resolveApp: resolveVueApp
|
|
45
|
+
});
|
|
46
|
+
bridge = activeBridge;
|
|
47
|
+
return activeBridge;
|
|
48
|
+
};
|
|
49
|
+
const getBridge = () => {
|
|
50
|
+
return bridge ?? createBridge();
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
name,
|
|
54
|
+
version,
|
|
55
|
+
dependencies,
|
|
56
|
+
/**
|
|
57
|
+
* 安装到引擎上下文(异步)
|
|
58
|
+
* 通过 bridge 将 store 能力注册到引擎
|
|
59
|
+
*/
|
|
60
|
+
async install(context) {
|
|
61
|
+
await getBridge().install(context);
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* 安装到 Vue App
|
|
65
|
+
* 将 store 运行时连接到 Vue 应用实例
|
|
66
|
+
*/
|
|
67
|
+
installVue(app) {
|
|
68
|
+
getBridge().attachToApp(app);
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* 从引擎上下文卸载(异步)
|
|
72
|
+
* 清理 bridge 引用
|
|
73
|
+
*/
|
|
74
|
+
async uninstall(context) {
|
|
75
|
+
await getBridge().uninstall(context);
|
|
76
|
+
bridge = null;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { createStoreEnginePlugin, createStoreEnginePlugin as default, getGlobalStoreAPI };
|
|
82
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
83
|
+
//# sourceMappingURL=engine-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine-plugin.js","names":[],"sources":["../../src/plugins/engine-plugin.ts"],"sourcesContent":["/* ============================================================\n * engine-plugin.ts\n * Store Engine 插件(Vue 适配层)\n * 将 core 层的 createStoreEngineBridge 包装为 Vue 友好的插件接口\n * 负责创建 bridge、管理运行时生命周期、连接 Vue App\n * ============================================================ */\n\n/* 从 core/engine 导入 bridge 相关类型 */\nimport type {\n StoreEngineBridge, /* 引擎桥接实例接口 */\n StoreEngineLike, /* 引擎抽象接口 */\n StoreEnginePluginContextLike, /* 插件上下文接口 */\n} from '@composy/store-core/engine'\n/* Vue App 类型 */\nimport type { App } from 'vue'\n/* 本地 store 运行时类型 */\nimport type { StoreRuntime } from '../runtime/store-runtime'\n/* 全局 Store API 和插件选项类型 */\nimport type { GlobalStoreAPI, StoreEnginePluginOptions } from '../types'\n/* 从 core/engine 导入 bridge 创建函数 */\nimport { createStoreEngineBridge } from '@composy/store-core/engine'\n/* 导入 Vue App 运行时安装相关函数 */\nimport { getInstalledStoreRuntime, installStoreRuntime } from '../runtime/install-store-app'\n/* 导入运行时创建函数和全局 API 获取函数 */\nimport { createStoreRuntime, getGlobalStoreAPI as getActiveStoreAPI } from '../runtime/store-runtime'\n\n/**\n * Store Engine 插件接口\n * 定义了插件的元信息和生命周期方法\n * @property dependencies - 依赖的其他插件名称列表\n * @property name - 插件名称\n * @property version - 插件版本\n * @property install - 安装到引擎上下文(异步)\n * @property installVue - 安装到 Vue App\n * @property uninstall - 从引擎上下文卸载(异步)\n */\nexport interface StoreEnginePlugin {\n dependencies: string[]\n name: string\n version: string\n install: (context: StoreEnginePluginContextLike) => Promise<void>\n installVue: (app: App) => void\n uninstall: (context?: StoreEnginePluginContextLike) => Promise<void>\n}\n\n/**\n * 类型守卫:判断值是否为 Vue App 实例\n * 通过检查是否有 use 方法来识别\n */\nfunction isVueApp(value: unknown): value is App {\n return !!value && typeof (value as App).use === 'function'\n}\n\n/**\n * 从各种可能的来源解析 Vue App 实例\n * 按优先级尝试:payload.app → payload.framework.app → engine.getApp() → context.framework.app\n *\n * @param engine - Store 引擎实例\n * @param context - 插件上下文\n * @param payload - 安装时传入的额外数据\n * @returns 解析到的 Vue App 或 undefined\n */\nfunction resolveVueApp(\n engine: StoreEngineLike,\n context: StoreEnginePluginContextLike,\n payload?: unknown,\n): App | undefined {\n /* 将 payload 宽松转为可能包含 app 字段的对象 */\n const appPayload = payload as { app?: unknown; framework?: { app?: unknown } } | undefined\n /* 按优先级尝试多个来源获取 app */\n const app = appPayload?.app\n ?? appPayload?.framework?.app\n ?? engine.getApp?.()\n ?? context.framework?.app\n\n /* 使用类型守卫验证是否为 Vue App */\n return isVueApp(app) ? app : undefined\n}\n\n/**\n * 获取当前活跃的全局 Store API\n * @returns 全局 Store API 或 null(未初始化时)\n */\nexport function getGlobalStoreAPI(): GlobalStoreAPI | null {\n return getActiveStoreAPI()\n}\n\n/**\n * 创建 Store Engine 插件工厂函数\n * 这是 Vue 适配层的核心入口,将 core 层的 bridge 包装为 Vue 友好的插件\n *\n * @param options - 插件配置选项\n * @returns StoreEnginePlugin 插件实例\n */\nexport function createStoreEnginePlugin(options: StoreEnginePluginOptions = {}): StoreEnginePlugin {\n /* 解构配置选项,设置默认值 */\n const {\n debug = false, /* 是否开启调试日志 */\n dependencies = [], /* 依赖的插件列表 */\n name = 'store', /* 插件名称 */\n version = '1.0.0', /* 插件版本 */\n } = options\n\n /* 延迟创建的 bridge 实例引用 */\n let bridge: StoreEngineBridge | null = null\n\n /**\n * 创建 bridge 的工厂函数\n * 内部调用 core 层的 createStoreEngineBridge,并传入 Vue 特定的实现\n */\n const createBridge = (): StoreEngineBridge => {\n const activeBridge = createStoreEngineBridge<StoreRuntime>({\n name, /* 插件名称 */\n version, /* 插件版本 */\n debug, /* 调试模式 */\n\n /* 创建运行时:传入所有 Vue 相关配置 */\n createRuntime: () => createStoreRuntime({\n pinia: options.pinia, /* 可选的外部 Pinia 实例 */\n persist: options.persist, /* 是否启用持久化 */\n persistOptions: options.persistOptions, /* 持久化选项 */\n globalProperties: options.globalProperties, /* 是否挂载到全局属性 */\n provide: options.provide, /* 是否通过 provide 注入 */\n debug,\n }),\n\n /* 将运行时安装到 Vue App */\n attachToApp: (app, runtime) => installStoreRuntime(app as App, runtime),\n\n /* 从 Vue App 获取已安装的运行时 */\n getRuntimeFromApp: app => getInstalledStoreRuntime(app as App),\n\n /* Vue App 类型守卫 */\n isApp: isVueApp,\n\n /* Vue App 解析函数 */\n resolveApp: resolveVueApp,\n })\n\n bridge = activeBridge\n return activeBridge\n }\n\n /**\n * 获取 bridge 实例(懒创建)\n * 首次调用时自动创建\n */\n const getBridge = (): StoreEngineBridge => {\n return bridge ?? createBridge()\n }\n\n /* ── 返回插件对象 ── */\n return {\n name, /* 插件名称 */\n version, /* 插件版本 */\n dependencies, /* 插件依赖 */\n\n /**\n * 安装到引擎上下文(异步)\n * 通过 bridge 将 store 能力注册到引擎\n */\n async install(context: StoreEnginePluginContextLike): Promise<void> {\n await getBridge().install(context)\n },\n\n /**\n * 安装到 Vue App\n * 将 store 运行时连接到 Vue 应用实例\n */\n installVue(app: App): void {\n getBridge().attachToApp(app)\n },\n\n /**\n * 从引擎上下文卸载(异步)\n * 清理 bridge 引用\n */\n async uninstall(context?: StoreEnginePluginContextLike): Promise<void> {\n await getBridge().uninstall(context)\n bridge = null /* 释放 bridge 引用,允许垃圾回收 */\n },\n }\n}\n\n/* 默认导出工厂函数 */\nexport default createStoreEnginePlugin\n"],"mappings":";;;;;;;;;;;;;AAiDA,SAAS,SAAS,OAA8B;CAC9C,OAAO,CAAC,CAAC,SAAS,OAAQ,MAAc,QAAQ;AAClD;AAWA,SAAS,cACP,QACA,SACA,SACiB;CAEjB,MAAM,aAAa;CAEnB,MAAM,MAAM,YAAY,OACnB,YAAY,WAAW,OACvB,OAAO,SAAS,KAChB,QAAQ,WAAW;CAGxB,OAAO,SAAS,GAAG,IAAI,MAAM,KAAA;AAC/B;AAMO,SAAS,oBAA2C;CACzD,OAAO,oBAAkB;AAC3B;AASO,SAAS,wBAAwB,UAAoC,CAAC,GAAsB;CAEjG,MAAM,EACJ,QAAQ,OACR,eAAe,CAAC,GAChB,OAAO,SACP,UAAU,YACR;CAGJ,IAAI,SAAmC;CAMvC,MAAM,qBAAwC;EAC5C,MAAM,eAAe,wBAAsC;GACzD;GACA;GACA;GAGA,qBAAqB,mBAAmB;IACtC,OAAO,QAAQ;IACf,SAAS,QAAQ;IACjB,gBAAgB,QAAQ;IACxB,kBAAkB,QAAQ;IAC1B,SAAS,QAAQ;IACjB;GACF,CAAC;GAGD,cAAc,KAAK,YAAY,oBAAoB,KAAY,OAAO;GAGtE,oBAAmB,QAAO,yBAAyB,GAAU;GAG7D,OAAO;GAGP,YAAY;EACd,CAAC;EAED,SAAS;EACT,OAAO;CACT;CAMA,MAAM,kBAAqC;EACzC,OAAO,UAAU,aAAa;CAChC;CAGA,OAAO;EACL;EACA;EACA;;;;;EAMA,MAAM,QAAQ,SAAsD;GAClE,MAAM,UAAU,CAAA,CAAE,QAAQ,OAAO;EACnC;;;;;EAMA,WAAW,KAAgB;GACzB,UAAU,CAAA,CAAE,YAAY,GAAG;EAC7B;;;;;EAMA,MAAM,UAAU,SAAuD;GACrE,MAAM,UAAU,CAAA,CAAE,UAAU,OAAO;GACnC,SAAS;EACX;CACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enhanced-persist-plugin.d.ts","sourceRoot":"","sources":["../../src/plugins/enhanced-persist-plugin.ts"],"names":[],"mappings":"AACA,OAAO,EACL,sBAAsB,EAEtB,2BAA2B,GAC5B,MAAM,gBAAgB,CAAA;AAGvB,YAAY,EAEV,sBAAsB,EAEtB,4BAA4B,GAC7B,MAAM,gBAAgB,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 { cleanupEnhancedPersist, createEnhancedPersistPlugin } from "./pinia-plugin.js";
|
|
11
|
+
export { cleanupEnhancedPersist, createEnhancedPersistPlugin };
|
|
12
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createStoreEnginePlugin, getGlobalStoreAPI, } from './engine-plugin';
|
|
2
|
+
export { cleanupEnhancedPersist, createEnhancedPersistPlugin, createPersistPlugin, createPersistPluginController, } from './pinia-plugin';
|
|
3
|
+
export type { EnhancedPersistOptions, EnhancedPersistPluginOptions, PersistPluginOptions, } from './pinia-plugin';
|
|
4
|
+
export { createRegistryMethods, createStoreRegistry, createStoreRegistryPlugin, getStoreRegistry, setGlobalStoreRegistry, } from './registry-plugin';
|
|
5
|
+
export { createStoreVuePlugin, } from './vue-plugin';
|
|
6
|
+
export type { StoreVuePluginOptions } from './vue-plugin';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,uBAAuB,EAEvB,iBAAiB,GAClB,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EAEL,sBAAsB,EAEtB,2BAA2B,EAE3B,mBAAmB,EAEnB,6BAA6B,GAC9B,MAAM,gBAAgB,CAAA;AAGvB,YAAY,EAEV,sBAAsB,EAEtB,4BAA4B,EAE5B,oBAAoB,GACrB,MAAM,gBAAgB,CAAA;AAGvB,OAAO,EAEL,qBAAqB,EAErB,mBAAmB,EAEnB,yBAAyB,EAEzB,gBAAgB,EAEhB,sBAAsB,GACvB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAEL,oBAAoB,GACrB,MAAM,cAAc,CAAA;AAGrB,YAAY,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
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 { cleanupEnhancedPersist, createEnhancedPersistPlugin, createPersistPlugin } from "./pinia-plugin.js";
|
|
11
|
+
import { createStoreRegistryPlugin, getStoreRegistry } from "../runtime/store-registry.js";
|
|
12
|
+
import { createStoreEnginePlugin, getGlobalStoreAPI } from "./engine-plugin.js";
|
|
13
|
+
import "./registry-plugin.js";
|
|
14
|
+
import { createStoreVuePlugin } from "./vue-plugin.js";
|
|
15
|
+
export { cleanupEnhancedPersist, createEnhancedPersistPlugin, createPersistPlugin, createStoreEnginePlugin, createStoreRegistryPlugin, createStoreVuePlugin, getGlobalStoreAPI, getStoreRegistry };
|
|
16
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { PiniaPlugin } from 'pinia';
|
|
2
|
+
import type { PersistOptions } from '../types';
|
|
3
|
+
type BrowserStorage = 'localStorage' | 'sessionStorage';
|
|
4
|
+
/**
|
|
5
|
+
* 持久化插件控制器接口
|
|
6
|
+
* @property plugin - Pinia 插件函数
|
|
7
|
+
* @property dispose - 清理函数,用于移除所有持久化订阅
|
|
8
|
+
*/
|
|
9
|
+
interface PersistPluginController {
|
|
10
|
+
plugin: PiniaPlugin;
|
|
11
|
+
dispose: () => void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 持久化插件全局配置选项
|
|
15
|
+
* @property storage - 默认存储类型
|
|
16
|
+
* @property keyPrefix - 存储键名前缀
|
|
17
|
+
* @property debounce - 防抖延迟(毫秒)
|
|
18
|
+
* @property debug - 是否开启调试日志
|
|
19
|
+
* @property syncTabs - 是否默认启用跨标签页同步
|
|
20
|
+
* @property defaultTTL - 默认过期时间(毫秒)
|
|
21
|
+
*/
|
|
22
|
+
export interface PersistPluginOptions {
|
|
23
|
+
storage?: BrowserStorage;
|
|
24
|
+
keyPrefix?: string;
|
|
25
|
+
debounce?: number;
|
|
26
|
+
debug?: boolean;
|
|
27
|
+
syncTabs?: boolean;
|
|
28
|
+
defaultTTL?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface EnhancedPersistPluginOptions extends PersistPluginOptions {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 增强版持久化选项(Store 级别配置)
|
|
34
|
+
* 在基础 PersistOptions 上增加 excludePaths、syncTabs、ttl
|
|
35
|
+
*/
|
|
36
|
+
export interface EnhancedPersistOptions extends PersistOptions {
|
|
37
|
+
syncTabs?: boolean;
|
|
38
|
+
ttl?: number;
|
|
39
|
+
excludePaths?: string[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 创建持久化插件控制器
|
|
43
|
+
* 返回一个 Pinia 插件函数和对应的 dispose 清理函数
|
|
44
|
+
*
|
|
45
|
+
* @param options - 插件全局配置
|
|
46
|
+
* @returns 控制器对象(包含 plugin 和 dispose)
|
|
47
|
+
*/
|
|
48
|
+
export declare function createPersistPluginController(options?: PersistPluginOptions): PersistPluginController;
|
|
49
|
+
/**
|
|
50
|
+
* 创建基础版持久化插件
|
|
51
|
+
* @param options - 插件配置
|
|
52
|
+
* @returns Pinia 插件函数
|
|
53
|
+
*/
|
|
54
|
+
export declare function createPersistPlugin(options?: PersistPluginOptions): PiniaPlugin;
|
|
55
|
+
/**
|
|
56
|
+
* 创建增强版持久化插件
|
|
57
|
+
* 与基础版功能相同,类型签名保留扩展空间
|
|
58
|
+
* @param options - 插件配置
|
|
59
|
+
* @returns Pinia 插件函数
|
|
60
|
+
*/
|
|
61
|
+
export declare function createEnhancedPersistPlugin(options?: EnhancedPersistPluginOptions): PiniaPlugin;
|
|
62
|
+
/**
|
|
63
|
+
* 全局清理所有活跃的持久化控制器
|
|
64
|
+
* 通常在应用卸载或测试中调用
|
|
65
|
+
*/
|
|
66
|
+
export declare function cleanupEnhancedPersist(): void;
|
|
67
|
+
export {};
|
|
68
|
+
//# sourceMappingURL=pinia-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pinia-plugin.d.ts","sourceRoot":"","sources":["../../src/plugins/pinia-plugin.ts"],"names":[],"mappings":"AASA,OAAO,KA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+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { debounce, deepClone, deepMerge, deleteByPath, getByPath, setByPath } from "@composy/store-core";
|
|
11
|
+
//#region src/plugins/pinia-plugin.ts
|
|
12
|
+
const STORE_PERSIST_DISPOSE_KEY = /* @__PURE__ */ Symbol("ldesign-store-persist-dispose");
|
|
13
|
+
const activePersistControllers = /* @__PURE__ */ new Set();
|
|
14
|
+
function getStorage(type) {
|
|
15
|
+
if (typeof window === "undefined") return null;
|
|
16
|
+
return type === "localStorage" ? window.localStorage : window.sessionStorage;
|
|
17
|
+
}
|
|
18
|
+
function normalizePersistConfig(value) {
|
|
19
|
+
if (!value) return null;
|
|
20
|
+
return typeof value === "boolean" ? {} : value;
|
|
21
|
+
}
|
|
22
|
+
function filterState(state, includePaths, excludePaths) {
|
|
23
|
+
if (includePaths?.length) {
|
|
24
|
+
const pickedState = {};
|
|
25
|
+
includePaths.forEach((path) => {
|
|
26
|
+
const value = getByPath(state, path);
|
|
27
|
+
if (value !== void 0) setByPath(pickedState, path, deepClone(value));
|
|
28
|
+
});
|
|
29
|
+
return pickedState;
|
|
30
|
+
}
|
|
31
|
+
if (excludePaths?.length) {
|
|
32
|
+
const nextState = deepClone(state);
|
|
33
|
+
excludePaths.forEach((path) => {
|
|
34
|
+
deleteByPath(nextState, path);
|
|
35
|
+
});
|
|
36
|
+
return nextState;
|
|
37
|
+
}
|
|
38
|
+
return deepClone(state);
|
|
39
|
+
}
|
|
40
|
+
function restoreStoreState(store, nextState) {
|
|
41
|
+
store.$patch((state) => {
|
|
42
|
+
deepMerge(state, nextState);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function patchPersistDispose(store, cleanup) {
|
|
46
|
+
const restorePatchedDispose = store[STORE_PERSIST_DISPOSE_KEY];
|
|
47
|
+
if (restorePatchedDispose) restorePatchedDispose();
|
|
48
|
+
const originalDispose = store.$dispose;
|
|
49
|
+
store.$dispose = function patchedDispose() {
|
|
50
|
+
cleanup();
|
|
51
|
+
return originalDispose.call(this);
|
|
52
|
+
};
|
|
53
|
+
store[STORE_PERSIST_DISPOSE_KEY] = () => {
|
|
54
|
+
store.$dispose = originalDispose;
|
|
55
|
+
delete store[STORE_PERSIST_DISPOSE_KEY];
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function createPersistPluginController(options = {}) {
|
|
59
|
+
const { storage: defaultStorage = "localStorage", keyPrefix = "ldesign-store:", debounce: debounceDelay = 100, debug = false, syncTabs: defaultSyncTabs = false, defaultTTL } = options;
|
|
60
|
+
const cleanupMap = /* @__PURE__ */ new Map();
|
|
61
|
+
const dispose = () => {
|
|
62
|
+
cleanupMap.forEach((cleanup) => cleanup());
|
|
63
|
+
cleanupMap.clear();
|
|
64
|
+
activePersistControllers.delete(dispose);
|
|
65
|
+
};
|
|
66
|
+
activePersistControllers.add(dispose);
|
|
67
|
+
const plugin = (context) => {
|
|
68
|
+
const { store, options: storeOptions } = context;
|
|
69
|
+
const config = normalizePersistConfig(storeOptions.persist);
|
|
70
|
+
if (!config) return;
|
|
71
|
+
const storage = getStorage(config.storage === "sessionStorage" ? "sessionStorage" : defaultStorage);
|
|
72
|
+
const serializer = config.serializer?.serialize ?? JSON.stringify;
|
|
73
|
+
const deserialize = config.serializer?.deserialize ?? JSON.parse;
|
|
74
|
+
const syncTabs = config.syncTabs ?? defaultSyncTabs;
|
|
75
|
+
const ttl = config.ttl ?? defaultTTL;
|
|
76
|
+
const key = `${keyPrefix}${config.key || store.$id}`;
|
|
77
|
+
if (!storage) {
|
|
78
|
+
if (debug) console.warn(`[PersistPlugin] Storage \u4E0D\u53EF\u7528 (${store.$id})`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
let isRestoring = false;
|
|
82
|
+
const applyPersistedValue = (rawState) => {
|
|
83
|
+
const nextState = deserialize(rawState);
|
|
84
|
+
isRestoring = true;
|
|
85
|
+
try {
|
|
86
|
+
config.beforeRestore?.({ store });
|
|
87
|
+
restoreStoreState(store, nextState);
|
|
88
|
+
config.afterRestore?.({ store });
|
|
89
|
+
} finally {
|
|
90
|
+
isRestoring = false;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const restoreState = () => {
|
|
94
|
+
try {
|
|
95
|
+
const stored = storage.getItem(key);
|
|
96
|
+
if (!stored) return;
|
|
97
|
+
const storageItem = JSON.parse(stored);
|
|
98
|
+
if (storageItem.expiresAt && Date.now() > storageItem.expiresAt) {
|
|
99
|
+
storage.removeItem(key);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
applyPersistedValue(storageItem.state);
|
|
103
|
+
if (debug) console.info(`[PersistPlugin] \u5DF2\u6062\u590D\u72B6\u6001 (${store.$id})`);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error(`[PersistPlugin] \u6062\u590D\u72B6\u6001\u5931\u8D25 (${store.$id}):`, error);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const persistState = () => {
|
|
109
|
+
if (isRestoring) return;
|
|
110
|
+
try {
|
|
111
|
+
const filteredState = filterState(store.$state, config.paths, config.excludePaths);
|
|
112
|
+
const payload = {
|
|
113
|
+
state: serializer(filteredState),
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
expiresAt: ttl ? Date.now() + ttl : void 0,
|
|
116
|
+
version: 1
|
|
117
|
+
};
|
|
118
|
+
storage.setItem(key, JSON.stringify(payload));
|
|
119
|
+
if (debug) console.info(`[PersistPlugin] \u5DF2\u4FDD\u5B58\u72B6\u6001 (${store.$id})`);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error(`[PersistPlugin] \u4FDD\u5B58\u72B6\u6001\u5931\u8D25 (${store.$id}):`, error);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const saveState = debounce(persistState, config.debounce ?? debounceDelay);
|
|
125
|
+
const stopSubscribe = store.$subscribe(() => {
|
|
126
|
+
if (!isRestoring) saveState();
|
|
127
|
+
});
|
|
128
|
+
let stopStorageSync = () => {};
|
|
129
|
+
if (syncTabs && typeof window !== "undefined") {
|
|
130
|
+
const handleStorage = (event) => {
|
|
131
|
+
if (event.key !== key || !event.newValue) return;
|
|
132
|
+
try {
|
|
133
|
+
const storageItem = JSON.parse(event.newValue);
|
|
134
|
+
if (storageItem.expiresAt && Date.now() > storageItem.expiresAt) return;
|
|
135
|
+
applyPersistedValue(storageItem.state);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error(`[PersistPlugin] \u540C\u6B65\u72B6\u6001\u5931\u8D25 (${store.$id}):`, error);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
window.addEventListener("storage", handleStorage);
|
|
141
|
+
stopStorageSync = () => {
|
|
142
|
+
window.removeEventListener("storage", handleStorage);
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const cleanup = () => {
|
|
146
|
+
saveState.cancel();
|
|
147
|
+
stopSubscribe();
|
|
148
|
+
stopStorageSync();
|
|
149
|
+
cleanupMap.delete(key);
|
|
150
|
+
};
|
|
151
|
+
cleanupMap.set(key, cleanup);
|
|
152
|
+
patchPersistDispose(store, cleanup);
|
|
153
|
+
restoreState();
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
plugin,
|
|
157
|
+
dispose
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function createPersistPlugin(options = {}) {
|
|
161
|
+
return createPersistPluginController(options).plugin;
|
|
162
|
+
}
|
|
163
|
+
function createEnhancedPersistPlugin(options = {}) {
|
|
164
|
+
return createPersistPluginController(options).plugin;
|
|
165
|
+
}
|
|
166
|
+
function cleanupEnhancedPersist() {
|
|
167
|
+
Array.from(activePersistControllers).forEach((dispose) => dispose());
|
|
168
|
+
}
|
|
169
|
+
//#endregion
|
|
170
|
+
export { cleanupEnhancedPersist, createEnhancedPersistPlugin, createPersistPlugin, createPersistPluginController };
|
|
171
|
+
/*! End of @composy/store-vue | Powered by @composy/builder */
|
|
172
|
+
//# sourceMappingURL=pinia-plugin.js.map
|
|
@@ -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 cleanupEnh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,OAA0B,OAAO;EAErD,aAAa;CACf;CAEA,OAAO;EACL;EACA;CACF;AACF;AAOO,SAAS,oBAAoB,UAAgC,CAAC,GAAgB;CACnF,OAAO,8BAA8B,OAAO,CAAA,CAAE;AAChD;AAQO,SAAS,4BACd,UAAwC,CAAC,GAC5B;CACb,OAAO,8BAA8B,OAAO,CAAA,CAAE;AAChD;AAMO,SAAS,yBAA+B;CAC7C,MAAM,KAAK,wBAAwB,CAAA,CAAE,SAAQ,YAAW,QAAQ,CAAC;AACnE"}
|
|
@@ -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 { createStoreRegistryPlugin, getStoreRegistry } from "../runtime/store-registry.js";
|
|
11
|
+
export { createStoreRegistryPlugin, getStoreRegistry };
|
|
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
|