@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.
Files changed (256) hide show
  1. package/README.md +154 -0
  2. package/dist/index.js +1536 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/index.min.js +1008 -0
  5. package/dist/index.min.js.map +1 -0
  6. package/es/composables/defineSetupStore.d.ts +120 -0
  7. package/es/composables/defineSetupStore.d.ts.map +1 -0
  8. package/es/composables/defineSetupStore.js +29 -0
  9. package/es/composables/defineSetupStore.js.map +1 -0
  10. package/es/composables/index.d.ts +24 -0
  11. package/es/composables/index.d.ts.map +1 -0
  12. package/es/composables/index.js +21 -0
  13. package/es/composables/useFormStore.d.ts +131 -0
  14. package/es/composables/useFormStore.d.ts.map +1 -0
  15. package/es/composables/useFormStore.js +189 -0
  16. package/es/composables/useFormStore.js.map +1 -0
  17. package/es/composables/useOptimistic.d.ts +67 -0
  18. package/es/composables/useOptimistic.d.ts.map +1 -0
  19. package/es/composables/useOptimistic.js +64 -0
  20. package/es/composables/useOptimistic.js.map +1 -0
  21. package/es/composables/usePersist.d.ts +68 -0
  22. package/es/composables/usePersist.d.ts.map +1 -0
  23. package/es/composables/usePersist.js +96 -0
  24. package/es/composables/usePersist.js.map +1 -0
  25. package/es/composables/usePersistedState.d.ts +59 -0
  26. package/es/composables/usePersistedState.d.ts.map +1 -0
  27. package/es/composables/usePersistedState.js +121 -0
  28. package/es/composables/usePersistedState.js.map +1 -0
  29. package/es/composables/useSharedState.d.ts +62 -0
  30. package/es/composables/useSharedState.d.ts.map +1 -0
  31. package/es/composables/useSharedState.js +94 -0
  32. package/es/composables/useSharedState.js.map +1 -0
  33. package/es/composables/useStore.d.ts +99 -0
  34. package/es/composables/useStore.d.ts.map +1 -0
  35. package/es/composables/useStore.js +61 -0
  36. package/es/composables/useStore.js.map +1 -0
  37. package/es/composables/useStoreDevtools.d.ts +31 -0
  38. package/es/composables/useStoreDevtools.d.ts.map +1 -0
  39. package/es/composables/useStoreDevtools.js +112 -0
  40. package/es/composables/useStoreDevtools.js.map +1 -0
  41. package/es/composables/useStoreSnapshot.d.ts +122 -0
  42. package/es/composables/useStoreSnapshot.d.ts.map +1 -0
  43. package/es/composables/useStoreSnapshot.js +209 -0
  44. package/es/composables/useStoreSnapshot.js.map +1 -0
  45. package/es/composables/useSubscribe.d.ts +90 -0
  46. package/es/composables/useSubscribe.d.ts.map +1 -0
  47. package/es/composables/useSubscribe.js +53 -0
  48. package/es/composables/useSubscribe.js.map +1 -0
  49. package/es/context/injection-keys.d.ts +8 -0
  50. package/es/context/injection-keys.d.ts.map +1 -0
  51. package/es/context/injection-keys.js +17 -0
  52. package/es/context/injection-keys.js.map +1 -0
  53. package/es/index.d.ts +24 -0
  54. package/es/index.d.ts.map +1 -0
  55. package/es/index.js +32 -0
  56. package/es/index.js.map +1 -0
  57. package/es/plugins/engine-plugin.d.ts +36 -0
  58. package/es/plugins/engine-plugin.d.ts.map +1 -0
  59. package/es/plugins/engine-plugin.js +83 -0
  60. package/es/plugins/engine-plugin.js.map +1 -0
  61. package/es/plugins/enhanced-persist-plugin.d.ts +3 -0
  62. package/es/plugins/enhanced-persist-plugin.d.ts.map +1 -0
  63. package/es/plugins/enhanced-persist-plugin.js +12 -0
  64. package/es/plugins/index.d.ts +7 -0
  65. package/es/plugins/index.d.ts.map +1 -0
  66. package/es/plugins/index.js +15 -0
  67. package/es/plugins/pinia-plugin.d.ts +68 -0
  68. package/es/plugins/pinia-plugin.d.ts.map +1 -0
  69. package/es/plugins/pinia-plugin.js +172 -0
  70. package/es/plugins/pinia-plugin.js.map +1 -0
  71. package/es/plugins/registry-plugin.d.ts +2 -0
  72. package/es/plugins/registry-plugin.d.ts.map +1 -0
  73. package/es/plugins/registry-plugin.js +12 -0
  74. package/es/plugins/vue-plugin.d.ts +10 -0
  75. package/es/plugins/vue-plugin.d.ts.map +1 -0
  76. package/es/plugins/vue-plugin.js +24 -0
  77. package/es/plugins/vue-plugin.js.map +1 -0
  78. package/es/runtime/install-store-app.d.ts +23 -0
  79. package/es/runtime/install-store-app.d.ts.map +1 -0
  80. package/es/runtime/install-store-app.js +54 -0
  81. package/es/runtime/install-store-app.js.map +1 -0
  82. package/es/runtime/store-plugin-options.d.ts +40 -0
  83. package/es/runtime/store-plugin-options.d.ts.map +1 -0
  84. package/es/runtime/store-plugin-options.js +24 -0
  85. package/es/runtime/store-plugin-options.js.map +1 -0
  86. package/es/runtime/store-registry.d.ts +64 -0
  87. package/es/runtime/store-registry.d.ts.map +1 -0
  88. package/es/runtime/store-registry.js +102 -0
  89. package/es/runtime/store-registry.js.map +1 -0
  90. package/es/runtime/store-runtime.d.ts +46 -0
  91. package/es/runtime/store-runtime.d.ts.map +1 -0
  92. package/es/runtime/store-runtime.js +58 -0
  93. package/es/runtime/store-runtime.js.map +1 -0
  94. package/es/types.d.ts +167 -0
  95. package/es/types.d.ts.map +1 -0
  96. package/es/types.js +10 -0
  97. package/esm/composables/defineSetupStore.d.ts +120 -0
  98. package/esm/composables/defineSetupStore.d.ts.map +1 -0
  99. package/esm/composables/defineSetupStore.js +29 -0
  100. package/esm/composables/defineSetupStore.js.map +1 -0
  101. package/esm/composables/index.d.ts +24 -0
  102. package/esm/composables/index.d.ts.map +1 -0
  103. package/esm/composables/index.js +21 -0
  104. package/esm/composables/useFormStore.d.ts +131 -0
  105. package/esm/composables/useFormStore.d.ts.map +1 -0
  106. package/esm/composables/useFormStore.js +189 -0
  107. package/esm/composables/useFormStore.js.map +1 -0
  108. package/esm/composables/useOptimistic.d.ts +67 -0
  109. package/esm/composables/useOptimistic.d.ts.map +1 -0
  110. package/esm/composables/useOptimistic.js +64 -0
  111. package/esm/composables/useOptimistic.js.map +1 -0
  112. package/esm/composables/usePersist.d.ts +68 -0
  113. package/esm/composables/usePersist.d.ts.map +1 -0
  114. package/esm/composables/usePersist.js +96 -0
  115. package/esm/composables/usePersist.js.map +1 -0
  116. package/esm/composables/usePersistedState.d.ts +59 -0
  117. package/esm/composables/usePersistedState.d.ts.map +1 -0
  118. package/esm/composables/usePersistedState.js +121 -0
  119. package/esm/composables/usePersistedState.js.map +1 -0
  120. package/esm/composables/useSharedState.d.ts +62 -0
  121. package/esm/composables/useSharedState.d.ts.map +1 -0
  122. package/esm/composables/useSharedState.js +94 -0
  123. package/esm/composables/useSharedState.js.map +1 -0
  124. package/esm/composables/useStore.d.ts +99 -0
  125. package/esm/composables/useStore.d.ts.map +1 -0
  126. package/esm/composables/useStore.js +61 -0
  127. package/esm/composables/useStore.js.map +1 -0
  128. package/esm/composables/useStoreDevtools.d.ts +31 -0
  129. package/esm/composables/useStoreDevtools.d.ts.map +1 -0
  130. package/esm/composables/useStoreDevtools.js +112 -0
  131. package/esm/composables/useStoreDevtools.js.map +1 -0
  132. package/esm/composables/useStoreSnapshot.d.ts +122 -0
  133. package/esm/composables/useStoreSnapshot.d.ts.map +1 -0
  134. package/esm/composables/useStoreSnapshot.js +209 -0
  135. package/esm/composables/useStoreSnapshot.js.map +1 -0
  136. package/esm/composables/useSubscribe.d.ts +90 -0
  137. package/esm/composables/useSubscribe.d.ts.map +1 -0
  138. package/esm/composables/useSubscribe.js +53 -0
  139. package/esm/composables/useSubscribe.js.map +1 -0
  140. package/esm/context/injection-keys.d.ts +8 -0
  141. package/esm/context/injection-keys.d.ts.map +1 -0
  142. package/esm/context/injection-keys.js +17 -0
  143. package/esm/context/injection-keys.js.map +1 -0
  144. package/esm/index.d.ts +24 -0
  145. package/esm/index.d.ts.map +1 -0
  146. package/esm/index.js +34 -0
  147. package/esm/index.js.map +1 -0
  148. package/esm/plugins/engine-plugin.d.ts +36 -0
  149. package/esm/plugins/engine-plugin.d.ts.map +1 -0
  150. package/esm/plugins/engine-plugin.js +83 -0
  151. package/esm/plugins/engine-plugin.js.map +1 -0
  152. package/esm/plugins/enhanced-persist-plugin.d.ts +3 -0
  153. package/esm/plugins/enhanced-persist-plugin.d.ts.map +1 -0
  154. package/esm/plugins/enhanced-persist-plugin.js +12 -0
  155. package/esm/plugins/index.d.ts +7 -0
  156. package/esm/plugins/index.d.ts.map +1 -0
  157. package/esm/plugins/index.js +16 -0
  158. package/esm/plugins/pinia-plugin.d.ts +68 -0
  159. package/esm/plugins/pinia-plugin.d.ts.map +1 -0
  160. package/esm/plugins/pinia-plugin.js +172 -0
  161. package/esm/plugins/pinia-plugin.js.map +1 -0
  162. package/esm/plugins/registry-plugin.d.ts +2 -0
  163. package/esm/plugins/registry-plugin.d.ts.map +1 -0
  164. package/esm/plugins/registry-plugin.js +12 -0
  165. package/esm/plugins/vue-plugin.d.ts +10 -0
  166. package/esm/plugins/vue-plugin.d.ts.map +1 -0
  167. package/esm/plugins/vue-plugin.js +24 -0
  168. package/esm/plugins/vue-plugin.js.map +1 -0
  169. package/esm/runtime/install-store-app.d.ts +23 -0
  170. package/esm/runtime/install-store-app.d.ts.map +1 -0
  171. package/esm/runtime/install-store-app.js +54 -0
  172. package/esm/runtime/install-store-app.js.map +1 -0
  173. package/esm/runtime/store-plugin-options.d.ts +40 -0
  174. package/esm/runtime/store-plugin-options.d.ts.map +1 -0
  175. package/esm/runtime/store-plugin-options.js +24 -0
  176. package/esm/runtime/store-plugin-options.js.map +1 -0
  177. package/esm/runtime/store-registry.d.ts +64 -0
  178. package/esm/runtime/store-registry.d.ts.map +1 -0
  179. package/esm/runtime/store-registry.js +94 -0
  180. package/esm/runtime/store-registry.js.map +1 -0
  181. package/esm/runtime/store-runtime.d.ts +46 -0
  182. package/esm/runtime/store-runtime.d.ts.map +1 -0
  183. package/esm/runtime/store-runtime.js +58 -0
  184. package/esm/runtime/store-runtime.js.map +1 -0
  185. package/esm/types.d.ts +167 -0
  186. package/esm/types.d.ts.map +1 -0
  187. package/esm/types.js +10 -0
  188. package/lib/composables/defineSetupStore.cjs +33 -0
  189. package/lib/composables/defineSetupStore.cjs.map +1 -0
  190. package/lib/composables/defineSetupStore.d.ts +120 -0
  191. package/lib/composables/index.cjs +46 -0
  192. package/lib/composables/index.d.ts +24 -0
  193. package/lib/composables/useFormStore.cjs +197 -0
  194. package/lib/composables/useFormStore.cjs.map +1 -0
  195. package/lib/composables/useFormStore.d.ts +131 -0
  196. package/lib/composables/useOptimistic.cjs +66 -0
  197. package/lib/composables/useOptimistic.cjs.map +1 -0
  198. package/lib/composables/useOptimistic.d.ts +67 -0
  199. package/lib/composables/usePersist.cjs +100 -0
  200. package/lib/composables/usePersist.cjs.map +1 -0
  201. package/lib/composables/usePersist.d.ts +68 -0
  202. package/lib/composables/usePersistedState.cjs +123 -0
  203. package/lib/composables/usePersistedState.cjs.map +1 -0
  204. package/lib/composables/usePersistedState.d.ts +59 -0
  205. package/lib/composables/useSharedState.cjs +96 -0
  206. package/lib/composables/useSharedState.cjs.map +1 -0
  207. package/lib/composables/useSharedState.d.ts +62 -0
  208. package/lib/composables/useStore.cjs +65 -0
  209. package/lib/composables/useStore.cjs.map +1 -0
  210. package/lib/composables/useStore.d.ts +99 -0
  211. package/lib/composables/useStoreDevtools.cjs +114 -0
  212. package/lib/composables/useStoreDevtools.cjs.map +1 -0
  213. package/lib/composables/useStoreDevtools.d.ts +31 -0
  214. package/lib/composables/useStoreSnapshot.cjs +211 -0
  215. package/lib/composables/useStoreSnapshot.cjs.map +1 -0
  216. package/lib/composables/useStoreSnapshot.d.ts +122 -0
  217. package/lib/composables/useSubscribe.cjs +57 -0
  218. package/lib/composables/useSubscribe.cjs.map +1 -0
  219. package/lib/composables/useSubscribe.d.ts +90 -0
  220. package/lib/context/injection-keys.cjs +21 -0
  221. package/lib/context/injection-keys.cjs.map +1 -0
  222. package/lib/context/injection-keys.d.ts +8 -0
  223. package/lib/index.cjs +235 -0
  224. package/lib/index.cjs.map +1 -0
  225. package/lib/index.d.ts +24 -0
  226. package/lib/package.json +3 -0
  227. package/lib/plugins/engine-plugin.cjs +90 -0
  228. package/lib/plugins/engine-plugin.cjs.map +1 -0
  229. package/lib/plugins/engine-plugin.d.ts +36 -0
  230. package/lib/plugins/enhanced-persist-plugin.cjs +15 -0
  231. package/lib/plugins/enhanced-persist-plugin.d.ts +3 -0
  232. package/lib/plugins/index.cjs +28 -0
  233. package/lib/plugins/index.d.ts +7 -0
  234. package/lib/plugins/pinia-plugin.cjs +177 -0
  235. package/lib/plugins/pinia-plugin.cjs.map +1 -0
  236. package/lib/plugins/pinia-plugin.d.ts +68 -0
  237. package/lib/plugins/registry-plugin.cjs +18 -0
  238. package/lib/plugins/registry-plugin.d.ts +2 -0
  239. package/lib/plugins/vue-plugin.cjs +26 -0
  240. package/lib/plugins/vue-plugin.cjs.map +1 -0
  241. package/lib/plugins/vue-plugin.d.ts +10 -0
  242. package/lib/runtime/install-store-app.cjs +57 -0
  243. package/lib/runtime/install-store-app.cjs.map +1 -0
  244. package/lib/runtime/install-store-app.d.ts +23 -0
  245. package/lib/runtime/store-plugin-options.cjs +26 -0
  246. package/lib/runtime/store-plugin-options.cjs.map +1 -0
  247. package/lib/runtime/store-plugin-options.d.ts +40 -0
  248. package/lib/runtime/store-registry.cjs +108 -0
  249. package/lib/runtime/store-registry.cjs.map +1 -0
  250. package/lib/runtime/store-registry.d.ts +64 -0
  251. package/lib/runtime/store-runtime.cjs +62 -0
  252. package/lib/runtime/store-runtime.cjs.map +1 -0
  253. package/lib/runtime/store-runtime.d.ts +46 -0
  254. package/lib/types.cjs +12 -0
  255. package/lib/types.d.ts +167 -0
  256. package/package.json +115 -0
@@ -0,0 +1,1008 @@
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
+ (function(e, t) {
12
+ "object" == typeof exports && "undefined" != typeof module ? t(exports, require("pinia"), require("vue"), require("@composy/store-core"), require("@composy/store-core/engine")) : "function" == typeof define && define.amd ? define([
13
+ "exports",
14
+ "pinia",
15
+ "vue",
16
+ "@composy/store-core",
17
+ "@composy/store-core/engine"
18
+ ], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).LDesignStoreVue = {}, e.Pinia, e.Vue, e.ComposyStoreCore, e.ComposyStoreCore);
19
+ })(this, function(exports, e, t, n, r) {
20
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
21
+ const o = "此字段为必填项", i = /^[^\s@]+@[^@][^.@]*\.[^\s@]+$/;
22
+ function s(e, t = "格式不正确") {
23
+ return (n) => "string" != typeof n || e.test(n) ? null : t;
24
+ }
25
+ function a(e) {
26
+ const { key: n, storage: r = "localStorage", defaultValue: o, serialize: i = JSON.stringify, deserialize: s = JSON.parse } = e, a = (u = r, "undefined" == typeof window ? null : "localStorage" === u ? localStorage : sessionStorage);
27
+ var u;
28
+ const l = (0, t.ref)(o), c = () => {
29
+ if (a) try {
30
+ const e = a.getItem(n);
31
+ null !== e && (l.value = s(e));
32
+ } catch (e) {
33
+ console.error(`[usePersist] 恢复失败 (${n}):`, e);
34
+ }
35
+ }, d = () => {
36
+ if (a) try {
37
+ a.setItem(n, i(l.value));
38
+ } catch (e) {
39
+ console.error(`[usePersist] 保存失败 (${n}):`, e);
40
+ }
41
+ };
42
+ return (0, t.onMounted)(() => {
43
+ c();
44
+ }), (0, t.watch)(l, () => {
45
+ d();
46
+ }, { deep: !0 }), {
47
+ value: l,
48
+ save: d,
49
+ restore: c,
50
+ clear: () => {
51
+ if (a) try {
52
+ a.removeItem(n), l.value = o;
53
+ } catch (e) {
54
+ console.error(`[usePersist] 清除失败 (${n}):`, e);
55
+ }
56
+ }
57
+ };
58
+ }
59
+ function u(e) {
60
+ return JSON.stringify(e);
61
+ }
62
+ function l(e) {
63
+ try {
64
+ return JSON.parse(e);
65
+ } catch {
66
+ return e;
67
+ }
68
+ }
69
+ const c = /* @__PURE__ */ new Set([
70
+ "store:created",
71
+ "store:disposed",
72
+ "state:changed"
73
+ ]);
74
+ function d(e) {
75
+ const t = new Date(e);
76
+ return `${t.getHours().toString().padStart(2, "0")}:${t.getMinutes().toString().padStart(2, "0")}:${t.getSeconds().toString().padStart(2, "0")}.${t.getMilliseconds().toString().padStart(3, "0")}`;
77
+ }
78
+ function p() {
79
+ return (0, e.getActivePinia)() ?? null;
80
+ }
81
+ function f(e, t, n = "") {
82
+ const r = [];
83
+ if (typeof e != typeof t) return r.push({
84
+ path: n || ".",
85
+ oldValue: e,
86
+ newValue: t,
87
+ type: "change"
88
+ }), r;
89
+ if ("object" != typeof e || null === e || null === t) return Object.is(e, t) || r.push({
90
+ path: n || ".",
91
+ oldValue: e,
92
+ newValue: t,
93
+ type: "change"
94
+ }), r;
95
+ if (Array.isArray(e) && Array.isArray(t)) {
96
+ const o = Math.max(e.length, t.length);
97
+ for (let i = 0; i < o; i++) {
98
+ const o = n ? `${n}[${i}]` : `[${i}]`;
99
+ i >= e.length ? r.push({
100
+ path: o,
101
+ oldValue: void 0,
102
+ newValue: t[i],
103
+ type: "add"
104
+ }) : i >= t.length ? r.push({
105
+ path: o,
106
+ oldValue: e[i],
107
+ newValue: void 0,
108
+ type: "remove"
109
+ }) : r.push(...f(e[i], t[i], o));
110
+ }
111
+ return r;
112
+ }
113
+ const o = new Set(Object.keys(e)), i = new Set(Object.keys(t));
114
+ for (const e of i) o.has(e) || r.push({
115
+ path: n ? `${n}.${e}` : e,
116
+ oldValue: void 0,
117
+ newValue: t[e],
118
+ type: "add"
119
+ });
120
+ for (const t of o) i.has(t) || r.push({
121
+ path: n ? `${n}.${t}` : t,
122
+ oldValue: e[t],
123
+ newValue: void 0,
124
+ type: "remove"
125
+ });
126
+ for (const s of o) i.has(s) && r.push(...f(e[s], t[s], n ? `${n}.${s}` : s));
127
+ return r;
128
+ }
129
+ const g = Symbol("ldesign-store-api"), y = Symbol("ldesign-store-pinia"), v = Symbol("ldesign-store-runtime"), m = Symbol("ldesign-store-persist-dispose"), b = /* @__PURE__ */ new Set();
130
+ function h(e = {}) {
131
+ const { storage: t = "localStorage", keyPrefix: r = "ldesign-store:", debounce: o = 100, debug: i = !1, syncTabs: s = !1, defaultTTL: a } = e, u = /* @__PURE__ */ new Map(), l = () => {
132
+ u.forEach((e) => e()), u.clear(), b.delete(l);
133
+ };
134
+ b.add(l);
135
+ return {
136
+ plugin: (e) => {
137
+ const { store: l, options: c } = e, d = (p = c.persist) ? "boolean" == typeof p ? {} : p : null;
138
+ var p;
139
+ if (!d) return;
140
+ const f = (g = "sessionStorage" === d.storage ? "sessionStorage" : t, "undefined" == typeof window ? null : "localStorage" === g ? window.localStorage : window.sessionStorage);
141
+ var g;
142
+ const y = d.serializer?.serialize ?? JSON.stringify, v = d.serializer?.deserialize ?? JSON.parse, b = d.syncTabs ?? s, h = d.ttl ?? a, S = `${r}${d.key || l.$id}`;
143
+ if (!f) return void (i && console.warn(`[PersistPlugin] Storage 不可用 (${l.$id})`));
144
+ let P = !1;
145
+ const w = (e) => {
146
+ const t = v(e);
147
+ P = !0;
148
+ try {
149
+ d.beforeRestore?.({ store: l }), function(e, t) {
150
+ e.$patch((e) => {
151
+ (0, n.deepMerge)(e, t);
152
+ });
153
+ }(l, t), d.afterRestore?.({ store: l });
154
+ } finally {
155
+ P = !1;
156
+ }
157
+ }, $ = (0, n.debounce)(() => {
158
+ if (!P) try {
159
+ const e = function(e, t, r) {
160
+ if (t?.length) {
161
+ const r = {};
162
+ return t.forEach((t) => {
163
+ const o = (0, n.getByPath)(e, t);
164
+ void 0 !== o && (0, n.setByPath)(r, t, (0, n.deepClone)(o));
165
+ }), r;
166
+ }
167
+ if (r?.length) {
168
+ const t = (0, n.deepClone)(e);
169
+ return r.forEach((e) => {
170
+ (0, n.deleteByPath)(t, e);
171
+ }), t;
172
+ }
173
+ return (0, n.deepClone)(e);
174
+ }(l.$state, d.paths, d.excludePaths), t = {
175
+ state: y(e),
176
+ timestamp: Date.now(),
177
+ expiresAt: h ? Date.now() + h : void 0,
178
+ version: 1
179
+ };
180
+ f.setItem(S, JSON.stringify(t)), i && console.info(`[PersistPlugin] 已保存状态 (${l.$id})`);
181
+ } catch (e) {
182
+ console.error(`[PersistPlugin] 保存状态失败 (${l.$id}):`, e);
183
+ }
184
+ }, d.debounce ?? o), O = l.$subscribe(() => {
185
+ P || $();
186
+ });
187
+ let A = () => {};
188
+ if (b && "undefined" != typeof window) {
189
+ const e = (e) => {
190
+ if (e.key === S && e.newValue) try {
191
+ const t = JSON.parse(e.newValue);
192
+ if (t.expiresAt && Date.now() > t.expiresAt) return;
193
+ w(t.state);
194
+ } catch (e) {
195
+ console.error(`[PersistPlugin] 同步状态失败 (${l.$id}):`, e);
196
+ }
197
+ };
198
+ window.addEventListener("storage", e), A = () => {
199
+ window.removeEventListener("storage", e);
200
+ };
201
+ }
202
+ const j = () => {
203
+ $.cancel(), O(), A(), u.delete(S);
204
+ };
205
+ u.set(S, j), function(e, t) {
206
+ const n = e[m];
207
+ n && n();
208
+ const r = e.$dispose;
209
+ e.$dispose = function() {
210
+ return t(), r.call(this);
211
+ }, e[m] = () => {
212
+ e.$dispose = r, delete e[m];
213
+ };
214
+ }(l, j), (() => {
215
+ try {
216
+ const e = f.getItem(S);
217
+ if (!e) return;
218
+ const t = JSON.parse(e);
219
+ if (t.expiresAt && Date.now() > t.expiresAt) return void f.removeItem(S);
220
+ w(t.state), i && console.info(`[PersistPlugin] 已恢复状态 (${l.$id})`);
221
+ } catch (e) {
222
+ console.error(`[PersistPlugin] 恢复状态失败 (${l.$id}):`, e);
223
+ }
224
+ })();
225
+ },
226
+ dispose: l
227
+ };
228
+ }
229
+ const S = Symbol("ldesign-store-registry-dispose");
230
+ function P() {
231
+ const e = /* @__PURE__ */ new Map(), t = (t) => {
232
+ e.delete(t);
233
+ }, n = (n) => {
234
+ e.set(n.$id, n), function(e, t) {
235
+ const n = e[S];
236
+ n && n();
237
+ const r = e.$dispose;
238
+ e.$dispose = function() {
239
+ return t(e.$id), r.call(this);
240
+ }, e[S] = () => {
241
+ e.$dispose = r, delete e[S];
242
+ };
243
+ }(n, t);
244
+ }, r = (t) => e.has(t), o = (t) => e.get(t), i = () => Array.from(e.keys()), s = () => {
245
+ e.forEach((e) => {
246
+ if ("function" == typeof e.$reset) try {
247
+ e.$reset();
248
+ } catch {}
249
+ });
250
+ };
251
+ return {
252
+ stores: e,
253
+ register: n,
254
+ unregister: t,
255
+ hasStore: r,
256
+ getStore: o,
257
+ getStoreIds: i,
258
+ resetAll: s,
259
+ clear: () => {
260
+ e.clear();
261
+ },
262
+ registerExistingStores: (e) => {
263
+ e._s?.forEach((e) => n(e));
264
+ },
265
+ createStoreAPI: (e) => ({
266
+ pinia: e,
267
+ getStore: o,
268
+ hasStore: r,
269
+ getStoreIds: i,
270
+ resetAll: s
271
+ })
272
+ };
273
+ }
274
+ let w = P();
275
+ function $(e = w) {
276
+ return ({ store: t }) => {
277
+ e.register(t);
278
+ };
279
+ }
280
+ const O = Symbol("ldesign-store-runtime");
281
+ let A = null;
282
+ function j(e) {
283
+ A = e, w = e?.registry ?? null ?? P();
284
+ }
285
+ function C(t = {}) {
286
+ const n = function(e = {}) {
287
+ return {
288
+ pinia: e.pinia,
289
+ persist: e.persist ?? !1,
290
+ persistOptions: e.persistOptions ?? {},
291
+ globalProperties: e.globalProperties ?? !0,
292
+ provide: e.provide ?? !0,
293
+ debug: e.debug ?? !1
294
+ };
295
+ }(t), r = n.pinia ?? (0, e.createPinia)();
296
+ if (r[O]) return r[O];
297
+ const o = P(), i = n.persist ? h({
298
+ ...n.persistOptions,
299
+ debug: n.debug || n.persistOptions.debug
300
+ }) : null;
301
+ r.use($(o)), i && r.use(i.plugin), o.registerExistingStores(r);
302
+ const s = {
303
+ pinia: r,
304
+ registry: o,
305
+ storeAPI: o.createStoreAPI(r),
306
+ options: n,
307
+ dispose: () => {
308
+ r[O] === s && delete r[O], i?.dispose(), o.clear(), A === s && j(null);
309
+ }
310
+ };
311
+ return r[O] = s, s;
312
+ }
313
+ const x = Symbol("ldesign-store-app-runtime");
314
+ function M(e, t) {
315
+ const n = e, r = n[x];
316
+ return r ? (j(r), r) : (function(e, t) {
317
+ return e.config.globalProperties.$pinia === t.pinia;
318
+ }(e, t) || e.use(t.pinia), n[x] = t, t.options.provide && (e.provide(v, t), e.provide(g, t.storeAPI), e.provide(y, t.pinia), e.provide("store", t.storeAPI), e.provide("pinia", t.pinia)), t.options.globalProperties && (e.config.globalProperties.$store = t.storeAPI), function(e, t) {
319
+ const n = e.unmount;
320
+ e.unmount = function() {
321
+ return e[x] === t && (delete e[x], t.dispose()), n.call(this);
322
+ };
323
+ }(n, t), j(t), t);
324
+ }
325
+ function V(e) {
326
+ return !!e && "function" == typeof e.use;
327
+ }
328
+ function E(e, t, n) {
329
+ const r = n, o = r?.app ?? r?.framework?.app ?? e.getApp?.() ?? t.framework?.app;
330
+ return V(o) ? o : void 0;
331
+ }
332
+ Object.defineProperty(exports, "batch", {
333
+ enumerable: !0,
334
+ get: function() {
335
+ return n.batch;
336
+ }
337
+ }), exports.cleanupEnhancedPersist = function() {
338
+ Array.from(b).forEach((e) => e());
339
+ }, Object.defineProperty(exports, "combineSelectors", {
340
+ enumerable: !0,
341
+ get: function() {
342
+ return n.combineSelectors;
343
+ }
344
+ }), Object.defineProperty(exports, "composePlugins", {
345
+ enumerable: !0,
346
+ get: function() {
347
+ return n.composePlugins;
348
+ }
349
+ }), Object.defineProperty(exports, "createAsyncAction", {
350
+ enumerable: !0,
351
+ get: function() {
352
+ return n.createAsyncAction;
353
+ }
354
+ }), Object.defineProperty(exports, "createBatchManager", {
355
+ enumerable: !0,
356
+ get: function() {
357
+ return n.createBatchManager;
358
+ }
359
+ }), exports.createEnhancedPersistPlugin = function(e = {}) {
360
+ return h(e).plugin;
361
+ }, Object.defineProperty(exports, "createEventBus", {
362
+ enumerable: !0,
363
+ get: function() {
364
+ return n.createEventBus;
365
+ }
366
+ }), Object.defineProperty(exports, "createLoggerMiddleware", {
367
+ enumerable: !0,
368
+ get: function() {
369
+ return n.createLoggerMiddleware;
370
+ }
371
+ }), Object.defineProperty(exports, "createMiddlewareManager", {
372
+ enumerable: !0,
373
+ get: function() {
374
+ return n.createMiddlewareManager;
375
+ }
376
+ }), exports.createPersistPlugin = function(e = {}) {
377
+ return h(e).plugin;
378
+ }, Object.defineProperty(exports, "createPinia", {
379
+ enumerable: !0,
380
+ get: function() {
381
+ return e.createPinia;
382
+ }
383
+ }), Object.defineProperty(exports, "createPluginFactory", {
384
+ enumerable: !0,
385
+ get: function() {
386
+ return n.createPluginFactory;
387
+ }
388
+ }), Object.defineProperty(exports, "createPubSub", {
389
+ enumerable: !0,
390
+ get: function() {
391
+ return n.createPubSub;
392
+ }
393
+ }), Object.defineProperty(exports, "createPubSubWithValue", {
394
+ enumerable: !0,
395
+ get: function() {
396
+ return n.createPubSubWithValue;
397
+ }
398
+ }), Object.defineProperty(exports, "createSelector", {
399
+ enumerable: !0,
400
+ get: function() {
401
+ return n.createSelector;
402
+ }
403
+ }), exports.createStoreEnginePlugin = function(e = {}) {
404
+ const { debug: t = !1, dependencies: n = [], name: o = "store", version: i = "1.0.0" } = e;
405
+ let s = null;
406
+ const a = () => {
407
+ const n = (0, r.createStoreEngineBridge)({
408
+ name: o,
409
+ version: i,
410
+ debug: t,
411
+ createRuntime: () => C({
412
+ pinia: e.pinia,
413
+ persist: e.persist,
414
+ persistOptions: e.persistOptions,
415
+ globalProperties: e.globalProperties,
416
+ provide: e.provide,
417
+ debug: t
418
+ }),
419
+ attachToApp: (e, t) => M(e, t),
420
+ getRuntimeFromApp: (e) => function(e) {
421
+ return e[x];
422
+ }(e),
423
+ isApp: V,
424
+ resolveApp: E
425
+ });
426
+ return s = n, n;
427
+ }, u = () => s ?? a();
428
+ return {
429
+ name: o,
430
+ version: i,
431
+ dependencies: n,
432
+ async install(e) {
433
+ await u().install(e);
434
+ },
435
+ installVue(e) {
436
+ u().attachToApp(e);
437
+ },
438
+ async uninstall(e) {
439
+ await u().uninstall(e), s = null;
440
+ }
441
+ };
442
+ }, exports.createStoreGetters = function(e) {
443
+ const n = {};
444
+ for (const r of Object.keys(e)) n[r] = (0, t.computed)(e[r]);
445
+ return n;
446
+ }, exports.createStoreRegistryPlugin = $, exports.createStoreState = function(e) {
447
+ const n = {};
448
+ for (const r of Object.keys(e)) n[r] = (0, t.ref)(e[r]);
449
+ return n;
450
+ }, exports.createStoreVuePlugin = function(e = {}) {
451
+ return { install(t) {
452
+ const n = C(e), r = M(t, n);
453
+ r !== n && n.dispose(), r.options.debug && console.info("[StoreVuePlugin] installed");
454
+ } };
455
+ }, Object.defineProperty(exports, "createTimeTravelMiddleware", {
456
+ enumerable: !0,
457
+ get: function() {
458
+ return n.createTimeTravelMiddleware;
459
+ }
460
+ }), Object.defineProperty(exports, "createValidationMiddleware", {
461
+ enumerable: !0,
462
+ get: function() {
463
+ return n.createValidationMiddleware;
464
+ }
465
+ }), Object.defineProperty(exports, "deepClone", {
466
+ enumerable: !0,
467
+ get: function() {
468
+ return n.deepClone;
469
+ }
470
+ }), Object.defineProperty(exports, "deepMerge", {
471
+ enumerable: !0,
472
+ get: function() {
473
+ return n.deepMerge;
474
+ }
475
+ }), exports.defineSetupStore = function(t, n) {
476
+ return (0, e.defineStore)(t, n);
477
+ }, Object.defineProperty(exports, "defineStore", {
478
+ enumerable: !0,
479
+ get: function() {
480
+ return e.defineStore;
481
+ }
482
+ }), Object.defineProperty(exports, "defineStorePlugin", {
483
+ enumerable: !0,
484
+ get: function() {
485
+ return n.defineStorePlugin;
486
+ }
487
+ }), exports.email = function(e = "邮箱格式不正确") {
488
+ return s(i, e);
489
+ }, Object.defineProperty(exports, "getActivePinia", {
490
+ enumerable: !0,
491
+ get: function() {
492
+ return e.getActivePinia;
493
+ }
494
+ }), exports.getGlobalStoreAPI = function() {
495
+ return A?.storeAPI ?? null;
496
+ }, exports.getStoreRegistry = function() {
497
+ return w.stores;
498
+ }, Object.defineProperty(exports, "globalEventBus", {
499
+ enumerable: !0,
500
+ get: function() {
501
+ return n.globalEventBus;
502
+ }
503
+ }), Object.defineProperty(exports, "mapActions", {
504
+ enumerable: !0,
505
+ get: function() {
506
+ return e.mapActions;
507
+ }
508
+ }), Object.defineProperty(exports, "mapGetters", {
509
+ enumerable: !0,
510
+ get: function() {
511
+ return e.mapGetters;
512
+ }
513
+ }), Object.defineProperty(exports, "mapState", {
514
+ enumerable: !0,
515
+ get: function() {
516
+ return e.mapState;
517
+ }
518
+ }), Object.defineProperty(exports, "mapStores", {
519
+ enumerable: !0,
520
+ get: function() {
521
+ return e.mapStores;
522
+ }
523
+ }), Object.defineProperty(exports, "mapWritableState", {
524
+ enumerable: !0,
525
+ get: function() {
526
+ return e.mapWritableState;
527
+ }
528
+ }), exports.maxLength = function(e, t) {
529
+ return (n) => "string" == typeof n && n.length > e ? t ?? `不能超过 ${e} 个字符` : null;
530
+ }, exports.minLength = function(e, t) {
531
+ return (n) => "string" == typeof n && n.length < e ? t ?? `至少需要 ${e} 个字符` : null;
532
+ }, exports.name = "@composy/store-vue", exports.numberRange = function(e, t, n) {
533
+ return (r) => "number" == typeof r && (r < e || r > t) ? n ?? `必须在 ${e} 到 ${t} 之间` : null;
534
+ }, exports.pattern = s, exports.required = function(e = o) {
535
+ return (t) => "" === t || null == t ? e : null;
536
+ }, Object.defineProperty(exports, "setActivePinia", {
537
+ enumerable: !0,
538
+ get: function() {
539
+ return e.setActivePinia;
540
+ }
541
+ }), Object.defineProperty(exports, "storeToRefs", {
542
+ enumerable: !0,
543
+ get: function() {
544
+ return e.storeToRefs;
545
+ }
546
+ }), exports.useComputedStore = function(e, n) {
547
+ const r = {};
548
+ for (const o in n) r[o] = (0, t.computed)(() => n[o](e.value));
549
+ return {
550
+ state: e,
551
+ getters: r
552
+ };
553
+ }, exports.useFormStore = function(e) {
554
+ const { initialValues: r, validationRules: i = {}, onSubmit: s, validateOn: a = "blur" } = e, u = (0, n.deepClone)(r), l = (0, t.reactive)((0, n.deepClone)(u)), c = l, d = u, p = Object.keys(u), f = (0, t.reactive)(p.reduce((e, t) => (e[t] = {
555
+ touched: !1,
556
+ dirty: !1,
557
+ error: null,
558
+ validating: !1
559
+ }, e), {})), g = (0, t.ref)(!1), y = (0, t.ref)(null), v = (0, t.ref)(0), m = (0, t.computed)(() => p.some((e) => f[e]?.dirty)), b = (0, t.computed)(() => p.every((e) => !f[e]?.error)), h = (0, t.computed)(() => p.reduce((e, t) => (e[t] = f[t]?.error ?? null, e), {}));
560
+ async function S(e) {
561
+ const t = f[e];
562
+ if (!t) return null;
563
+ const n = i[e];
564
+ if (!n) return t.error = null, null;
565
+ t.validating = !0;
566
+ try {
567
+ const r = l[e];
568
+ if (n.required && ("" === r || null == r)) {
569
+ const e = "string" == typeof n.required ? n.required : o;
570
+ return t.error = e, e;
571
+ }
572
+ if (n.rules) for (const e of n.rules) {
573
+ const n = await e(r, c);
574
+ if (n) return t.error = n, n;
575
+ }
576
+ return t.error = null, null;
577
+ } finally {
578
+ t.validating = !1;
579
+ }
580
+ }
581
+ async function P() {
582
+ return (await Promise.all(p.map((e) => S(e)))).every((e) => null === e);
583
+ }
584
+ return p.forEach((e) => {
585
+ (0, t.watch)(() => c[e], (t) => {
586
+ const r = f[e];
587
+ r && (r.dirty = !(0, n.deepEqual)(t, d[e])), "change" === a && r?.touched && S(e);
588
+ }, { deep: !0 });
589
+ }), {
590
+ values: l,
591
+ fields: f,
592
+ isDirty: m,
593
+ isValid: b,
594
+ isSubmitting: g,
595
+ submitError: y,
596
+ submitCount: v,
597
+ setFieldValue: function(e, t) {
598
+ c[e] = t;
599
+ },
600
+ setFieldTouched: function(e) {
601
+ const t = f[e];
602
+ t && (t.touched = !0), "blur" === a && S(e);
603
+ },
604
+ validateField: S,
605
+ validateAll: P,
606
+ handleSubmit: async function() {
607
+ if (v.value++, y.value = null, p.forEach((e) => {
608
+ const t = f[e];
609
+ t && (t.touched = !0);
610
+ }), await P() && s) {
611
+ g.value = !0;
612
+ try {
613
+ await s((0, n.deepClone)(l));
614
+ } catch (e) {
615
+ y.value = e instanceof Error ? e.message : String(e);
616
+ } finally {
617
+ g.value = !1;
618
+ }
619
+ }
620
+ },
621
+ reset: function(e) {
622
+ e && Object.assign(d, (0, n.deepClone)(e)), p.forEach((e) => {
623
+ c[e] = (0, n.deepClone)(d[e]);
624
+ const t = f[e];
625
+ t && (t.touched = !1, t.dirty = !1, t.error = null, t.validating = !1);
626
+ }), y.value = null;
627
+ },
628
+ resetField: function(e) {
629
+ const t = e;
630
+ c[t] = (0, n.deepClone)(d[t]);
631
+ const r = f[t];
632
+ r && (r.touched = !1, r.dirty = !1, r.error = null, r.validating = !1);
633
+ },
634
+ errors: h
635
+ };
636
+ }, exports.useOnAction = function(e, n, r = !1) {
637
+ if (e.$onAction) {
638
+ const o = e.$onAction(n, r);
639
+ return r || (0, t.onUnmounted)(() => {
640
+ o();
641
+ }), o;
642
+ }
643
+ return console.warn("[useOnAction] Store 不支持 $onAction 方法"), () => {};
644
+ }, exports.useOptimistic = function(e) {
645
+ const r = (0, t.ref)((0, n.deepClone)(e)), o = (0, t.ref)(!1), i = (0, t.ref)(!1), s = (0, t.ref)([]);
646
+ return {
647
+ state: r,
648
+ isPending: o,
649
+ pending: s,
650
+ isRolledBack: i,
651
+ mutate: async function(e, t) {
652
+ const a = (0, n.deepClone)(r.value), u = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, l = {
653
+ id: u,
654
+ snapshot: a,
655
+ status: "pending",
656
+ timestamp: Date.now()
657
+ };
658
+ s.value = [...s.value, l], o.value = !0, i.value = !1, r.value = "function" == typeof e ? e(r.value) : e;
659
+ try {
660
+ await t(), s.value = s.value.map((e) => e.id === u ? {
661
+ ...e,
662
+ status: "confirmed"
663
+ } : e);
664
+ } catch {
665
+ r.value = a, i.value = !0, s.value = s.value.map((e) => e.id === u ? {
666
+ ...e,
667
+ status: "rolled-back"
668
+ } : e);
669
+ } finally {
670
+ s.value = s.value.filter((e) => "pending" === e.status), o.value = s.value.length > 0;
671
+ }
672
+ }
673
+ };
674
+ }, exports.usePersist = a, exports.usePersistedRef = function(e, t, n = "localStorage") {
675
+ const { value: r } = a({
676
+ key: e,
677
+ storage: n,
678
+ defaultValue: t
679
+ });
680
+ return r;
681
+ }, exports.usePersistedRefWithTTL = function(e, n, r) {
682
+ const { value: o } = a({
683
+ key: e,
684
+ defaultValue: {
685
+ value: n,
686
+ expiry: 0
687
+ },
688
+ serialize: (e) => JSON.stringify(e),
689
+ deserialize: (e) => {
690
+ const t = JSON.parse(e);
691
+ return t.expiry && Date.now() > t.expiry ? {
692
+ value: n,
693
+ expiry: 0
694
+ } : t;
695
+ }
696
+ }), i = (0, t.ref)(o.value.value);
697
+ return (0, t.watch)(i, (e) => {
698
+ o.value = {
699
+ value: e,
700
+ expiry: Date.now() + r
701
+ };
702
+ }), i;
703
+ }, exports.usePersistedState = function(e) {
704
+ const { key: r, storage: o = "localStorage", defaultValue: i, serializer: s = u, deserializer: a = l, syncTabs: c = !1, debounce: d = 0, deep: p = !0, ttl: f, onBeforeChange: g, onAfterChange: y, onBeforeRestore: v, onAfterRestore: m } = e, b = (h = o, "undefined" == typeof window ? null : "localStorage" === h ? window.localStorage : window.sessionStorage);
705
+ var h;
706
+ const S = () => (0, n.deepClone)(i), P = (0, t.ref)(S()), w = () => {
707
+ if (b) try {
708
+ const e = {
709
+ value: s(P.value),
710
+ timestamp: Date.now(),
711
+ expiresAt: f ? Date.now() + f : void 0
712
+ };
713
+ b.setItem(r, JSON.stringify(e));
714
+ } catch (e) {
715
+ console.error(`[usePersistedState] 保存失败 (${r}):`, e);
716
+ }
717
+ }, $ = () => {
718
+ if (b) try {
719
+ b.removeItem(r);
720
+ } catch (e) {
721
+ console.error(`[usePersistedState] 清除失败 (${r}):`, e);
722
+ }
723
+ }, O = () => {
724
+ if (b) try {
725
+ const e = b.getItem(r);
726
+ if (!e) return;
727
+ const t = JSON.parse(e);
728
+ if (t.expiresAt && Date.now() > t.expiresAt) return void $();
729
+ let n = a(t.value);
730
+ v && (n = v(n)), P.value = n, m?.(n);
731
+ } catch (e) {
732
+ console.error(`[usePersistedState] 恢复失败 (${r}):`, e);
733
+ }
734
+ }, A = d > 0 ? (0, n.debounce)(w, d) : null, j = (e) => {
735
+ if (e.key === r && e.newValue) try {
736
+ const t = JSON.parse(e.newValue);
737
+ if (t.expiresAt && Date.now() > t.expiresAt) return;
738
+ P.value = a(t.value);
739
+ } catch (e) {
740
+ console.error(`[usePersistedState] 同步失败 (${r}):`, e);
741
+ }
742
+ };
743
+ return O(), (0, t.watch)(P, (e, t) => {
744
+ !1 !== g?.(e, t) && (A ? A() : w(), y?.(e, t));
745
+ }, { deep: p }), c && "undefined" != typeof window && window.addEventListener("storage", j), (0, t.onUnmounted)(() => {
746
+ A?.cancel(), c && "undefined" != typeof window && window.removeEventListener("storage", j);
747
+ }), {
748
+ state: P,
749
+ save: w,
750
+ restore: O,
751
+ clear: $,
752
+ reset: () => {
753
+ P.value = S(), $();
754
+ },
755
+ isExpired: () => {
756
+ if (!b || !f) return !1;
757
+ try {
758
+ const e = b.getItem(r);
759
+ if (!e) return !1;
760
+ const t = JSON.parse(e);
761
+ return !!t.expiresAt && Date.now() > t.expiresAt;
762
+ } catch {
763
+ return !1;
764
+ }
765
+ }
766
+ };
767
+ }, exports.useSharedState = function(e, r, o = {}) {
768
+ const { immediate: i = !1, deep: s = !0 } = o, a = (0, t.ref)((0, n.deepClone)(r)), u = (0, t.ref)(0), l = (0, t.ref)(null);
769
+ let c = null, d = !1;
770
+ if ("undefined" == typeof BroadcastChannel) return console.warn("[useSharedState] BroadcastChannel not supported in this environment"), {
771
+ state: a,
772
+ broadcast: () => {},
773
+ peerCount: u,
774
+ lastSyncAt: l,
775
+ close: () => {}
776
+ };
777
+ try {
778
+ c = new BroadcastChannel(e);
779
+ } catch (e) {
780
+ return console.warn("[useSharedState] Failed to create BroadcastChannel:", e), {
781
+ state: a,
782
+ broadcast: () => {},
783
+ peerCount: u,
784
+ lastSyncAt: l,
785
+ close: () => {}
786
+ };
787
+ }
788
+ function p() {
789
+ if (c) try {
790
+ c.postMessage({
791
+ type: "state-update",
792
+ payload: (0, n.deepClone)(a.value),
793
+ timestamp: Date.now()
794
+ });
795
+ } catch (e) {
796
+ console.warn("[useSharedState] broadcast error:", e);
797
+ }
798
+ }
799
+ c.onmessage = (e) => {
800
+ const t = e.data;
801
+ t && "object" == typeof t && ("state-update" === t.type ? (d = !0, a.value = t.payload, l.value = t.timestamp || Date.now(), d = !1) : "ping" === t.type ? c?.postMessage({ type: "pong" }) : "pong" === t.type && u.value++);
802
+ };
803
+ const f = (0, t.watch)(a, () => {
804
+ d || p();
805
+ }, { deep: s });
806
+ function g() {
807
+ f(), c && (c.close(), c = null);
808
+ }
809
+ return i && p(), c && (u.value = 0, c.postMessage({ type: "ping" })), (0, t.onUnmounted)(() => {
810
+ g();
811
+ }), {
812
+ state: a,
813
+ broadcast: p,
814
+ peerCount: u,
815
+ lastSyncAt: l,
816
+ close: g
817
+ };
818
+ }, exports.useSimpleStore = function(e) {
819
+ const n = (0, t.ref)({ ...e }), r = { ...e };
820
+ return {
821
+ state: n,
822
+ setState: (e) => {
823
+ Object.assign(n.value, e);
824
+ },
825
+ reset: () => {
826
+ n.value = { ...r };
827
+ },
828
+ subscribe: (e) => (0, t.watch)(n, (t) => {
829
+ e(t);
830
+ }, { deep: !0 })
831
+ };
832
+ }, exports.useStore = function(t, n = {}) {
833
+ const r = t();
834
+ return {
835
+ store: r,
836
+ refs: (0, e.storeToRefs)(r),
837
+ reset: () => {
838
+ "$reset" in r && "function" == typeof r.$reset && r.$reset();
839
+ },
840
+ patch: (e) => {
841
+ "$patch" in r && "function" == typeof r.$patch && r.$patch(e);
842
+ }
843
+ };
844
+ }, exports.useStoreDevtools = function(e = {}) {
845
+ const { maxLogEntries: r = 200 } = e, o = (0, t.ref)([]), i = (0, t.ref)([]), s = (0, t.ref)(!1);
846
+ let a = null;
847
+ function u() {
848
+ const e = p();
849
+ o.value = e?._s ? Array.from(e._s.entries(), ([e, t]) => function(e, t) {
850
+ return {
851
+ id: t,
852
+ state: (0, n.deepClone)(e.$state),
853
+ active: !0
854
+ };
855
+ }(t, e)) : [];
856
+ }
857
+ function l() {
858
+ s.value && (s.value = !1, a?.(), a = null);
859
+ }
860
+ return u(), (0, t.onUnmounted)(() => {
861
+ l();
862
+ }), {
863
+ stores: o,
864
+ eventLog: i,
865
+ isMonitoring: s,
866
+ startMonitoring: function() {
867
+ s.value || (s.value = !0, a = n.globalEventBus.on("*", (e) => {
868
+ const t = {
869
+ type: e.type,
870
+ payload: e.payload,
871
+ timestamp: e.timestamp,
872
+ time: d(e.timestamp)
873
+ };
874
+ i.value = [t, ...i.value].slice(0, r), c.has(e.type) && u();
875
+ }));
876
+ },
877
+ stopMonitoring: l,
878
+ clearLog: function() {
879
+ i.value = [];
880
+ },
881
+ refreshStores: u,
882
+ getStoreState: function(e) {
883
+ const t = p()?._s?.get(e);
884
+ return t ? (0, n.deepClone)(t.$state) : null;
885
+ },
886
+ resetStore: function(e) {
887
+ const t = p()?._s?.get(e);
888
+ return !(!t || "function" != typeof t.$reset) && (t.$reset(), u(), !0);
889
+ },
890
+ resetAllStores: function() {
891
+ const e = p();
892
+ e?._s && (e._s.forEach((e) => {
893
+ "function" == typeof e.$reset && e.$reset();
894
+ }), u());
895
+ }
896
+ };
897
+ }, exports.useStoreSnapshot = function(e, r, o = {}) {
898
+ const { maxSnapshots: i = 100, autoRecord: s = !1, debounceMs: a = 300, deep: u = !0 } = o, l = (0, t.ref)([]), c = (0, t.ref)(-1);
899
+ let d, p = !1;
900
+ const g = (0, t.computed)(() => c.value > 0), y = (0, t.computed)(() => c.value < l.value.length - 1), v = (t, r) => {
901
+ const o = "function" == typeof e ? e() : e.value, s = {
902
+ id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
903
+ state: (0, n.deepClone)(o),
904
+ timestamp: Date.now(),
905
+ label: t,
906
+ meta: r
907
+ };
908
+ return c.value < l.value.length - 1 && (l.value = l.value.slice(0, c.value + 1)), l.value.push(s), c.value = l.value.length - 1, l.value.length > i && (l.value.shift(), c.value--), s;
909
+ };
910
+ if (s) {
911
+ const r = (0, t.watch)("function" == typeof e ? (0, t.computed)(e) : e, (e, t) => {
912
+ p || (0, n.deepEqual)(e, t) || (d && clearTimeout(d), d = setTimeout(() => {
913
+ v();
914
+ }, a));
915
+ }, { deep: u });
916
+ v("初始状态"), (0, t.onUnmounted)(() => {
917
+ r(), d && clearTimeout(d);
918
+ });
919
+ }
920
+ return {
921
+ snapshots: l,
922
+ currentIndex: c,
923
+ canUndo: g,
924
+ canRedo: y,
925
+ takeSnapshot: v,
926
+ restoreSnapshot: (e) => {
927
+ const t = l.value.findIndex((t) => t.id === e);
928
+ if (-1 === t) return !1;
929
+ p = !0;
930
+ try {
931
+ return r((0, n.deepClone)(l.value[t].state)), c.value = t, !0;
932
+ } finally {
933
+ p = !1;
934
+ }
935
+ },
936
+ undo: () => {
937
+ if (!g.value) return !1;
938
+ c.value--, p = !0;
939
+ try {
940
+ return r((0, n.deepClone)(l.value[c.value].state)), !0;
941
+ } finally {
942
+ p = !1;
943
+ }
944
+ },
945
+ redo: () => {
946
+ if (!y.value) return !1;
947
+ c.value++, p = !0;
948
+ try {
949
+ return r((0, n.deepClone)(l.value[c.value].state)), !0;
950
+ } finally {
951
+ p = !1;
952
+ }
953
+ },
954
+ goto: (e) => {
955
+ if (e < 0 || e >= l.value.length) return !1;
956
+ c.value = e, p = !0;
957
+ try {
958
+ return r((0, n.deepClone)(l.value[e].state)), !0;
959
+ } finally {
960
+ p = !1;
961
+ }
962
+ },
963
+ clearSnapshots: () => {
964
+ l.value = [], c.value = -1;
965
+ },
966
+ getDiff: (e, t) => {
967
+ const n = l.value.find((t) => t.id === e), r = l.value.find((e) => e.id === t);
968
+ return n && r ? f(n.state, r.state) : [];
969
+ },
970
+ exportSnapshots: () => JSON.stringify({
971
+ snapshots: l.value,
972
+ currentIndex: c.value,
973
+ exportedAt: Date.now()
974
+ }),
975
+ importSnapshots: (e) => {
976
+ try {
977
+ const t = JSON.parse(e);
978
+ return !!Array.isArray(t.snapshots) && (l.value = t.snapshots, c.value = Math.min(t.currentIndex ?? l.value.length - 1, l.value.length - 1), !0);
979
+ } catch {
980
+ return !1;
981
+ }
982
+ }
983
+ };
984
+ }, exports.useSubscribe = function(e, n, r = {}) {
985
+ const { detached: o = !1 } = r;
986
+ if (e.$subscribe) {
987
+ const r = e.$subscribe((e, t) => {
988
+ n(e, t);
989
+ }, { detached: o });
990
+ return o || (0, t.onUnmounted)(() => {
991
+ r();
992
+ }), r;
993
+ }
994
+ return console.warn("[useSubscribe] Store 不支持 $subscribe 方法"), () => {};
995
+ }, exports.useWatch = function(e, n, r = {}) {
996
+ const { immediate: o = !1, deep: i = !1, detached: s = !1 } = r, a = (0, t.watch)(e, (e, t) => {
997
+ n(e, t);
998
+ }, {
999
+ immediate: o,
1000
+ deep: i
1001
+ });
1002
+ return s || (0, t.onUnmounted)(() => {
1003
+ a();
1004
+ }), a;
1005
+ }, exports.version = "1.0.0";
1006
+ });
1007
+ /*! End of @composy/store-vue | Powered by @composy/builder */
1008
+ //# sourceMappingURL=index.min.js.map