cogsbox-state 0.5.464 → 0.5.466

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/dist/store.js CHANGED
@@ -1,353 +1,359 @@
1
1
  import { create as M } from "zustand";
2
- import { ulid as b } from "ulid";
3
- import { startTransition as g } from "react";
4
- const j = M((f, c) => ({
2
+ import { ulid as y } from "ulid";
3
+ const I = M((f, s) => ({
5
4
  formRefs: /* @__PURE__ */ new Map(),
6
- registerFormRef: (e, s) => f((o) => {
7
- const t = new Map(o.formRefs);
8
- return t.set(e, s), { formRefs: t };
5
+ registerFormRef: (t, e) => f((n) => {
6
+ const o = new Map(n.formRefs);
7
+ return o.set(t, e), { formRefs: o };
9
8
  }),
10
- getFormRef: (e) => c().formRefs.get(e),
11
- removeFormRef: (e) => f((s) => {
12
- const o = new Map(s.formRefs);
13
- return o.delete(e), { formRefs: o };
9
+ getFormRef: (t) => s().formRefs.get(t),
10
+ removeFormRef: (t) => f((e) => {
11
+ const n = new Map(e.formRefs);
12
+ return n.delete(t), { formRefs: n };
14
13
  }),
15
- // Get all refs that start with the stateKey prefix
16
- getFormRefsByStateKey: (e) => {
17
- const s = c().formRefs, o = e + ".", t = /* @__PURE__ */ new Map();
18
- return s.forEach((n, a) => {
19
- (a.startsWith(o) || a === e) && t.set(a, n);
20
- }), t;
14
+ getFormRefsByStateKey: (t) => {
15
+ const e = s().formRefs, n = t + ".", o = /* @__PURE__ */ new Map();
16
+ return e.forEach((r, a) => {
17
+ (a.startsWith(n) || a === t) && o.set(a, r);
18
+ }), o;
21
19
  }
22
- })), m = (f) => f === null || typeof f != "object" ? !1 : !!(Array.isArray(f) || f.constructor === Object), E = M((f, c) => ({
23
- updateQueue: /* @__PURE__ */ new Set(),
24
- // A flag to ensure we only schedule the flush once per event-loop tick.
25
- isFlushScheduled: !1,
26
- // This function is called by queueMicrotask to execute all queued updates.
27
- flushUpdates: () => {
28
- const { updateQueue: e } = c();
29
- e.size > 0 && g(() => {
30
- e.forEach((s) => s());
31
- }), f({ updateQueue: /* @__PURE__ */ new Set(), isFlushScheduled: !1 });
32
- },
33
- addPathComponent: (e, s, o) => {
34
- f((t) => {
35
- const n = new Map(t.shadowStateStore), a = [e, ...s].join("."), r = n.get(a) || {}, S = new Set(r.pathComponents);
36
- S.add(o), n.set(a, { ...r, pathComponents: S });
37
- const i = n.get(e) || {}, p = i.components?.get(o);
38
- if (p) {
39
- const d = new Set(p.paths);
40
- d.add(a);
41
- const h = { ...p, paths: d }, l = new Map(i.components);
42
- l.set(o, h), n.set(e, {
43
- ...i,
44
- components: l
45
- });
46
- }
47
- return { shadowStateStore: n };
48
- });
49
- },
50
- registerComponent: (e, s, o) => {
51
- f((t) => {
52
- const n = new Map(t.shadowStateStore), a = n.get(e) || {}, r = new Map(a.components);
53
- return r.set(s, o), n.set(e, { ...a, components: r }), { shadowStateStore: n };
20
+ }));
21
+ function w(f) {
22
+ if (f === null || typeof f != "object")
23
+ return { value: f };
24
+ if (Array.isArray(f)) {
25
+ const s = { _meta: { arrayKeys: [] } }, t = [];
26
+ return f.forEach((e) => {
27
+ const n = `id:${y()}`;
28
+ s[n] = w(e), t.push(n);
29
+ }), s._meta.arrayKeys = t, s;
30
+ }
31
+ if (f.constructor === Object) {
32
+ const s = { _meta: {} };
33
+ for (const t in f)
34
+ Object.prototype.hasOwnProperty.call(f, t) && (s[t] = w(f[t]));
35
+ return s;
36
+ }
37
+ return { value: f };
38
+ }
39
+ const N = M((f, s) => ({
40
+ shadowStateStore: /* @__PURE__ */ new Map(),
41
+ setTransformCache: (t, e, n, o) => {
42
+ const r = s().getShadowMetadata(t, e) || {};
43
+ r.transformCaches || (r.transformCaches = /* @__PURE__ */ new Map()), r.transformCaches.set(n, o), s().setShadowMetadata(t, e, {
44
+ transformCaches: r.transformCaches
54
45
  });
55
46
  },
56
- unregisterComponent: (e, s) => {
57
- f((o) => {
58
- const t = new Map(o.shadowStateStore), n = t.get(e);
59
- if (!n?.components)
60
- return o;
61
- const a = new Map(n.components);
62
- return a.delete(s) ? (t.set(e, { ...n, components: a }), { shadowStateStore: t }) : o;
47
+ initializeShadowState: (t, e) => {
48
+ f((n) => {
49
+ const o = new Map(n.shadowStateStore), r = o.get(t) || o.get(`[${t}`);
50
+ let a = {};
51
+ if (r?._meta) {
52
+ const {
53
+ components: c,
54
+ features: i,
55
+ lastServerSync: S,
56
+ stateSource: l,
57
+ baseServerState: u
58
+ } = r._meta;
59
+ c && (a.components = c), i && (a.features = i), S && (a.lastServerSync = S), l && (a.stateSource = l), u && (a.baseServerState = u);
60
+ }
61
+ o.delete(t), o.delete(`[${t}`);
62
+ const d = w(e);
63
+ d._meta || (d._meta = {}), Object.assign(d._meta, a);
64
+ const p = Array.isArray(e) ? `[${t}` : t;
65
+ return o.set(p, d), { shadowStateStore: o };
63
66
  });
64
67
  },
65
- markAsDirty: (e, s, o = { bubble: !0 }) => {
66
- const { shadowStateStore: t } = c(), n = /* @__PURE__ */ new Map(), a = (r) => {
67
- const S = [e, ...r].join("."), i = t.get(S) || {};
68
- return i.isDirty === !0 ? !0 : (n.set(S, { ...i, isDirty: !0 }), !1);
69
- };
70
- if (a(s), o.bubble) {
71
- let r = [...s];
72
- for (; r.length > 0 && (r.pop(), !a(r)); )
73
- ;
68
+ getShadowNode: (t, e) => {
69
+ const n = s().shadowStateStore;
70
+ let o = n.get(t) || n.get(`[${t}`);
71
+ if (o) {
72
+ if (e.length === 0) return o;
73
+ for (const r of e)
74
+ if (typeof o != "object" || o === null || (o = o[r], o === void 0)) return;
75
+ return o;
74
76
  }
75
- n.size > 0 && f((r) => (n.forEach((S, i) => {
76
- r.shadowStateStore.set(i, S);
77
- }), r));
78
77
  },
79
- serverStateUpdates: /* @__PURE__ */ new Map(),
80
- setServerStateUpdate: (e, s) => {
78
+ getShadowMetadata: (t, e) => s().getShadowNode(t, e)?._meta,
79
+ setShadowMetadata: (t, e, n) => {
81
80
  f((o) => {
82
- const t = new Map(o.serverStateUpdates);
83
- return t.set(e, s), { serverStateUpdates: t };
84
- }), c().notifyPathSubscribers(e, {
85
- type: "SERVER_STATE_UPDATE",
86
- serverState: s
81
+ const r = new Map(o.shadowStateStore), a = r.has(`[${t}`) ? `[${t}` : t;
82
+ let d = r.get(a);
83
+ d || (d = {}, r.set(a, d));
84
+ const p = { ...d };
85
+ r.set(a, p);
86
+ let c = p;
87
+ for (const i of e) {
88
+ const S = c[i] || {};
89
+ c[i] = { ...S }, c = c[i];
90
+ }
91
+ return c._meta = { ...c._meta || {}, ...n }, { shadowStateStore: r };
87
92
  });
88
93
  },
89
- shadowStateStore: /* @__PURE__ */ new Map(),
90
- getShadowNode: (e) => c().shadowStateStore.get(e),
91
- pathSubscribers: /* @__PURE__ */ new Map(),
92
- subscribeToPath: (e, s) => {
93
- const o = c().pathSubscribers, t = o.get(e) || /* @__PURE__ */ new Set();
94
- return t.add(s), o.set(e, t), () => {
95
- const n = c().pathSubscribers.get(e);
96
- n && (n.delete(s), n.size === 0 && c().pathSubscribers.delete(e));
97
- };
98
- },
99
- notifyPathSubscribers: (e, s) => {
100
- const t = c().pathSubscribers.get(e);
101
- t && t.forEach((n) => n(s));
94
+ getShadowValue: (t, e, n, o) => {
95
+ const r = s().getShadowNode(t, e);
96
+ if (r == null) return;
97
+ const a = Object.keys(r);
98
+ if (Object.prototype.hasOwnProperty.call(r, "value") && a.every((i) => i === "value" || i === "_meta"))
99
+ return r.value;
100
+ if (r._meta && Object.prototype.hasOwnProperty.call(r._meta, "arrayKeys"))
101
+ return (n !== void 0 && n.length > 0 ? n : r._meta.arrayKeys).map(
102
+ (S) => s().getShadowValue(t, [...e, S])
103
+ );
104
+ const c = {};
105
+ for (const i of a)
106
+ i !== "_meta" && !i.startsWith("id:") && (c[i] = s().getShadowValue(t, [...e, i]));
107
+ return c;
102
108
  },
103
- initializeShadowState: (e, s) => {
109
+ updateShadowAtPath: (t, e, n) => {
104
110
  f((o) => {
105
- const t = new Map(o.shadowStateStore);
106
- console.log("initializeShadowState");
107
- const a = t.get(e)?.components, r = e + ".";
108
- for (const i of Array.from(t.keys()))
109
- (i === e || i.startsWith(r)) && t.delete(i);
110
- const S = (i, p) => {
111
- const d = [e, ...p].join(".");
112
- if (Array.isArray(i)) {
113
- const h = [];
114
- i.forEach(() => {
115
- const l = `id:${b()}`;
116
- h.push(d + "." + l);
117
- }), t.set(d, { arrayKeys: h }), i.forEach((l, u) => {
118
- const w = h[u].split(".").pop();
119
- S(l, [...p, w]);
120
- });
121
- } else if (m(i)) {
122
- const h = Object.fromEntries(
123
- Object.keys(i).map((l) => [l, d + "." + l])
124
- );
125
- t.set(d, { fields: h }), Object.keys(i).forEach((l) => {
126
- S(i[l], [...p, l]);
127
- });
128
- } else
129
- t.set(d, { value: i });
130
- };
131
- if (S(s, []), a) {
132
- const i = t.get(e) || {};
133
- t.set(e, {
134
- ...i,
135
- components: a
136
- });
111
+ const r = new Map(o.shadowStateStore), a = r.has(`[${t}`) ? `[${t}` : t;
112
+ let d = r.get(a);
113
+ if (!d) return o;
114
+ const p = { ...d };
115
+ r.set(a, p);
116
+ let c = p;
117
+ for (let l = 0; l < e.length - 1; l++)
118
+ c[e[l]] = { ...c[e[l]] || {} }, c = c[e[l]];
119
+ const i = e.length === 0 ? c : c[e[e.length - 1]];
120
+ if (!i)
121
+ return c[e[e.length - 1]] = w(n), { shadowStateStore: r };
122
+ function S(l, u) {
123
+ if (typeof u != "object" || u === null || Array.isArray(u)) {
124
+ const h = l._meta, m = w(u);
125
+ h && (m._meta = { ...h, ...m._meta || {} }), Object.keys(l).forEach((g) => delete l[g]), Object.assign(l, m);
126
+ return;
127
+ }
128
+ const b = new Set(Object.keys(u));
129
+ for (const h of b) {
130
+ const m = u[h];
131
+ l[h] ? S(l[h], m) : l[h] = w(m);
132
+ }
133
+ for (const h in l)
134
+ h === "_meta" || !Object.prototype.hasOwnProperty.call(l, h) || b.has(h) || delete l[h];
137
135
  }
138
- return { shadowStateStore: t };
136
+ return S(i, n), s().notifyPathSubscribers([t, ...e].join("."), {
137
+ type: "UPDATE",
138
+ newValue: n
139
+ }), { shadowStateStore: r };
139
140
  });
140
141
  },
141
- getShadowValue: (e, s) => {
142
- const o = /* @__PURE__ */ new Map(), t = (n, a) => {
143
- if (o.has(n))
144
- return o.get(n);
145
- const r = c().shadowStateStore.get(n);
146
- if (!r)
147
- return;
148
- if (r.value !== void 0)
149
- return r.value;
150
- let S;
151
- if (r.arrayKeys) {
152
- const i = a ?? r.arrayKeys;
153
- S = [], o.set(n, S), i.forEach((p) => {
154
- S.push(t(p));
155
- });
156
- } else r.fields ? (S = {}, o.set(n, S), Object.entries(r.fields).forEach(([i, p]) => {
157
- S[i] = t(p);
158
- })) : S = void 0;
159
- return S;
160
- };
161
- return t(e, s);
162
- },
163
- getShadowMetadata: (e, s) => {
164
- const o = [e, ...s].join(".");
165
- return c().shadowStateStore.get(o);
166
- },
167
- setShadowMetadata: (e, s, o) => {
168
- const t = [e, ...s].join("."), n = c().shadowStateStore.get(t);
169
- n?.components && !o.components && (console.group(
170
- "%c🚨 RACE CONDITION DETECTED! 🚨",
171
- "color: red; font-size: 18px; font-weight: bold;"
172
- ), console.error(
173
- `An overwrite is about to happen on stateKey: "${e}" at path: [${s.join(", ")}]`
174
- ), console.log(
175
- "The EXISTING metadata had a components map:",
176
- n.components
177
- ), console.log(
178
- "The NEW metadata is trying to save WITHOUT a components map:",
179
- o
180
- ), console.log(
181
- "%cStack trace to the function that caused this overwrite:",
182
- "font-weight: bold;"
183
- ), console.trace(), console.groupEnd());
184
- const a = new Map(c().shadowStateStore), r = { ...n || {}, ...o };
185
- a.set(t, r), f({ shadowStateStore: a });
186
- },
187
- setTransformCache: (e, s, o, t) => {
188
- const n = [e, ...s].join("."), a = new Map(c().shadowStateStore), r = a.get(n) || {};
189
- r.transformCaches || (r.transformCaches = /* @__PURE__ */ new Map()), r.transformCaches.set(o, t), a.set(n, r), f({ shadowStateStore: a });
142
+ addItemsToArrayNode: (t, e, n, o) => {
143
+ f((r) => {
144
+ const a = new Map(r.shadowStateStore), d = a.has(`[${t}`) ? `[${t}` : t;
145
+ let p = a.get(d);
146
+ if (!p)
147
+ return console.error("Root not found for state key:", t), r;
148
+ const c = { ...p };
149
+ a.set(d, c);
150
+ let i = c;
151
+ for (const S of e) {
152
+ const l = i[S] || {};
153
+ i[S] = { ...l }, i = i[S];
154
+ }
155
+ return Object.assign(i, n), i._meta = { ...i._meta || {}, arrayKeys: o }, { shadowStateStore: a };
156
+ });
190
157
  },
191
- insertShadowArrayElement: (e, s, o) => {
192
- const t = new Map(c().shadowStateStore), n = [e, ...s].join("."), a = t.get(n);
193
- if (!a || !a.arrayKeys) return;
194
- const r = `id:${b()}`, S = n + "." + r, i = [...a.arrayKeys];
195
- i.push(S), t.set(n, { ...a, arrayKeys: i });
196
- const p = (d, h) => {
197
- const l = [e, ...h].join(".");
198
- if (!Array.isArray(d)) if (typeof d == "object" && d !== null) {
199
- const u = Object.fromEntries(
200
- Object.keys(d).map((w) => [w, l + "." + w])
201
- );
202
- t.set(l, { fields: u }), Object.entries(d).forEach(([w, y]) => {
203
- p(y, [...h, w]);
204
- });
205
- } else
206
- t.set(l, { value: d });
207
- };
208
- p(o, [...s, r]), f({ shadowStateStore: t }), c().notifyPathSubscribers(n, {
158
+ insertShadowArrayElement: (t, e, n, o) => {
159
+ const r = s().getShadowNode(t, e);
160
+ if (!r?._meta?.arrayKeys) {
161
+ console.error(
162
+ `Array not found at path: ${[t, ...e].join(".")}`
163
+ );
164
+ return;
165
+ }
166
+ const a = `id:${y()}`, d = { [a]: w(n) }, c = [...r._meta.arrayKeys], i = o !== void 0 && o >= 0 && o <= c.length ? o : c.length;
167
+ c.splice(i, 0, a), s().addItemsToArrayNode(t, e, d, c);
168
+ const S = [t, ...e].join(".");
169
+ s().notifyPathSubscribers(S, {
209
170
  type: "INSERT",
210
- path: n,
211
- itemKey: S
171
+ path: S,
172
+ itemKey: `${S}.${a}`,
173
+ index: i
212
174
  });
213
175
  },
214
- removeShadowArrayElement: (e, s) => {
215
- const o = new Map(c().shadowStateStore), t = [e, ...s].join("."), n = s.slice(0, -1), a = [e, ...n].join("."), r = o.get(a);
216
- if (r && r.arrayKeys && r.arrayKeys.findIndex(
217
- (i) => i === t
218
- ) !== -1) {
219
- const i = r.arrayKeys.filter(
220
- (d) => d !== t
176
+ insertManyShadowArrayElements: (t, e, n, o) => {
177
+ if (!n || n.length === 0)
178
+ return;
179
+ const r = s().getShadowNode(t, e);
180
+ if (!r?._meta?.arrayKeys) {
181
+ console.error(
182
+ `Array not found at path: ${[t, ...e].join(".")}`
221
183
  );
222
- o.set(a, {
223
- ...r,
224
- arrayKeys: i
225
- });
226
- const p = t + ".";
227
- for (const d of Array.from(o.keys()))
228
- (d === t || d.startsWith(p)) && o.delete(d);
184
+ return;
229
185
  }
230
- f({ shadowStateStore: o }), c().notifyPathSubscribers(a, {
186
+ const a = {}, d = [];
187
+ n.forEach((l) => {
188
+ const u = `id:${y()}`;
189
+ d.push(u), a[u] = w(l);
190
+ });
191
+ const c = [...r._meta.arrayKeys], i = o !== void 0 && o >= 0 && o <= c.length ? o : c.length;
192
+ c.splice(i, 0, ...d), s().addItemsToArrayNode(t, e, a, c);
193
+ const S = [t, ...e].join(".");
194
+ s().notifyPathSubscribers(S, {
195
+ type: "INSERT_MANY",
196
+ path: S,
197
+ count: n.length,
198
+ index: i
199
+ });
200
+ },
201
+ removeShadowArrayElement: (t, e) => {
202
+ if (e.length === 0) return;
203
+ const n = e.slice(0, -1), o = e[e.length - 1];
204
+ if (!o?.startsWith("id:")) return;
205
+ const r = s().getShadowNode(t, n);
206
+ if (!r?._meta?.arrayKeys) return;
207
+ const a = r._meta.arrayKeys.filter((p) => p !== o);
208
+ delete r[o], s().setShadowMetadata(t, n, { arrayKeys: a });
209
+ const d = [t, ...n].join(".");
210
+ s().notifyPathSubscribers(d, {
231
211
  type: "REMOVE",
232
- path: a,
233
- itemKey: t
234
- // The exact ID of the removed item
212
+ path: d,
213
+ itemKey: `${d}.${o}`
235
214
  });
236
215
  },
237
- updateShadowAtPath: (e, s, o) => {
238
- const t = [e, ...s].join(".");
239
- c().shadowStateStore.get(t)?.value === o && !m(o) || f((a) => {
240
- const r = a.shadowStateStore;
241
- if (m(o)) {
242
- const S = (i, p) => {
243
- const d = [e, ...i].join("."), h = r.get(d);
244
- if (h && h.fields) {
245
- for (const l in p)
246
- if (Object.prototype.hasOwnProperty.call(p, l)) {
247
- const u = p[l], w = h.fields[l];
248
- if (w)
249
- if (m(u))
250
- S(
251
- w.split(".").slice(1),
252
- u
253
- );
254
- else {
255
- const y = r.get(w) || {};
256
- r.set(w, {
257
- ...y,
258
- value: u
259
- });
260
- }
261
- }
262
- }
263
- };
264
- S(s, o);
265
- } else {
266
- const S = r.get(t) || {};
267
- r.set(t, { ...S, value: o });
216
+ addPathComponent: (t, e, n) => {
217
+ const o = s().getShadowMetadata(t, e) || {}, r = new Set(o.pathComponents);
218
+ r.add(n), s().setShadowMetadata(t, e, {
219
+ pathComponents: r
220
+ });
221
+ const a = s().getShadowMetadata(t, []);
222
+ if (a?.components) {
223
+ const d = a.components.get(n);
224
+ if (d) {
225
+ const p = [t, ...e].join("."), c = new Set(d.paths);
226
+ c.add(p);
227
+ const i = { ...d, paths: c }, S = new Map(a.components);
228
+ S.set(n, i), s().setShadowMetadata(t, [], { components: S });
268
229
  }
269
- return c().notifyPathSubscribers(t, { type: "UPDATE", newValue: o }), a;
230
+ }
231
+ },
232
+ registerComponent: (t, e, n) => {
233
+ const o = s().getShadowMetadata(t, []) || {}, r = new Map(o.components);
234
+ r.set(e, n), s().setShadowMetadata(t, [], { components: r });
235
+ },
236
+ unregisterComponent: (t, e) => {
237
+ const n = s().getShadowMetadata(t, []);
238
+ if (!n?.components) return;
239
+ const o = new Map(n.components);
240
+ o.delete(e) && s().setShadowMetadata(t, [], { components: o });
241
+ },
242
+ markAsDirty: (t, e, n = { bubble: !0 }) => {
243
+ const o = (r) => s().getShadowNode(t, r)?._meta?.isDirty ? !0 : (s().setShadowMetadata(t, r, { isDirty: !0 }), !1);
244
+ if (o(e), n.bubble) {
245
+ let r = [...e];
246
+ for (; r.length > 0 && (r.pop(), !o(r)); )
247
+ ;
248
+ }
249
+ },
250
+ serverStateUpdates: /* @__PURE__ */ new Map(),
251
+ setServerStateUpdate: (t, e) => {
252
+ f((n) => ({
253
+ serverStateUpdates: new Map(n.serverStateUpdates).set(
254
+ t,
255
+ e
256
+ )
257
+ })), s().notifyPathSubscribers(t, {
258
+ type: "SERVER_STATE_UPDATE",
259
+ serverState: e
270
260
  });
271
261
  },
262
+ pathSubscribers: /* @__PURE__ */ new Map(),
263
+ subscribeToPath: (t, e) => {
264
+ const n = s().pathSubscribers, o = n.get(t) || /* @__PURE__ */ new Set();
265
+ return o.add(e), n.set(t, o), () => {
266
+ const r = s().pathSubscribers.get(t);
267
+ r && (r.delete(e), r.size === 0 && s().pathSubscribers.delete(t));
268
+ };
269
+ },
270
+ notifyPathSubscribers: (t, e) => {
271
+ const o = s().pathSubscribers.get(t);
272
+ o && o.forEach((r) => r(e));
273
+ },
272
274
  selectedIndicesMap: /* @__PURE__ */ new Map(),
273
- getSelectedIndex: (e, s) => {
274
- const o = c().selectedIndicesMap.get(e);
275
- if (!o) return -1;
276
- const t = s || E.getState().getShadowMetadata(e, [])?.arrayKeys;
277
- return t ? t.indexOf(o) : -1;
275
+ getSelectedIndex: (t, e) => {
276
+ const n = s().selectedIndicesMap.get(t);
277
+ if (!n) return -1;
278
+ const o = s().getShadowMetadata(
279
+ t.split(".")[0],
280
+ t.split(".").slice(1)
281
+ ), r = e || o?.arrayKeys;
282
+ return r ? r.indexOf(n) : -1;
278
283
  },
279
- setSelectedIndex: (e, s) => {
280
- f((o) => {
281
- const t = o.selectedIndicesMap;
282
- return s === void 0 ? t.delete(e) : (t.has(e) && c().notifyPathSubscribers(t.get(e), {
284
+ setSelectedIndex: (t, e) => {
285
+ f((n) => {
286
+ const o = new Map(n.selectedIndicesMap);
287
+ return e === void 0 ? o.delete(t) : (o.has(t) && s().notifyPathSubscribers(o.get(t), {
283
288
  type: "THIS_UNSELECTED"
284
- }), t.set(e, s), c().notifyPathSubscribers(s, {
285
- type: "THIS_SELECTED"
286
- })), c().notifyPathSubscribers(e, {
287
- type: "GET_SELECTED"
288
- }), {
289
- ...o,
290
- selectedIndicesMap: t
289
+ }), o.set(t, e), s().notifyPathSubscribers(e, { type: "THIS_SELECTED" })), s().notifyPathSubscribers(t, { type: "GET_SELECTED" }), {
290
+ ...n,
291
+ selectedIndicesMap: o
291
292
  };
292
293
  });
293
294
  },
294
- clearSelectedIndex: ({ arrayKey: e }) => {
295
- f((s) => {
296
- const o = s.selectedIndicesMap, t = o.get(e);
297
- return t && c().notifyPathSubscribers(t, {
295
+ clearSelectedIndex: ({ arrayKey: t }) => {
296
+ f((e) => {
297
+ const n = new Map(e.selectedIndicesMap), o = n.get(t);
298
+ return o && s().notifyPathSubscribers(o, {
298
299
  type: "CLEAR_SELECTION"
299
- }), o.delete(e), c().notifyPathSubscribers(e, {
300
+ }), n.delete(t), s().notifyPathSubscribers(t, {
300
301
  type: "CLEAR_SELECTION"
301
302
  }), {
302
- ...s,
303
- selectedIndicesMap: o
303
+ ...e,
304
+ selectedIndicesMap: n
304
305
  };
305
306
  });
306
307
  },
307
- clearSelectedIndexesForState: (e) => {
308
- f((s) => {
309
- const o = new Map(s.selectedIndicesMap);
310
- return o.delete(e) ? { selectedIndicesMap: o } : {};
308
+ clearSelectedIndexesForState: (t) => {
309
+ f((e) => {
310
+ const n = new Map(e.selectedIndicesMap);
311
+ let o = !1;
312
+ for (const r of n.keys())
313
+ (r === t || r.startsWith(t + ".")) && (n.delete(r), o = !0);
314
+ return o ? { selectedIndicesMap: n } : {};
311
315
  });
312
316
  },
313
317
  initialStateOptions: {},
314
- stateTimeline: {},
315
- cogsStateStore: {},
316
318
  stateLog: /* @__PURE__ */ new Map(),
317
319
  initialStateGlobal: {},
318
- validationErrors: /* @__PURE__ */ new Map(),
319
- addStateLog: (e, s) => {
320
- f((o) => {
321
- const t = new Map(o.stateLog), n = new Map(t.get(e)), a = JSON.stringify(s.path), r = n.get(a);
322
- return r ? (r.newValue = s.newValue, r.timeStamp = s.timeStamp) : n.set(a, { ...s }), t.set(e, n), { stateLog: t };
320
+ addStateLog: (t) => {
321
+ !t || t.length === 0 || f((e) => {
322
+ const n = new Map(e.stateLog), o = /* @__PURE__ */ new Map();
323
+ for (const r of t) {
324
+ const a = o.get(r.stateKey) || [];
325
+ a.push(r), o.set(r.stateKey, a);
326
+ }
327
+ for (const [r, a] of o.entries()) {
328
+ const d = new Map(n.get(r));
329
+ for (const p of a)
330
+ d.set(JSON.stringify(p.path), { ...p });
331
+ n.set(r, d);
332
+ }
333
+ return { stateLog: n };
323
334
  });
324
335
  },
325
- getInitialOptions: (e) => c().initialStateOptions[e],
326
- setInitialStateOptions: (e, s) => {
327
- f((o) => ({
328
- initialStateOptions: {
329
- ...o.initialStateOptions,
330
- [e]: s
331
- }
336
+ getInitialOptions: (t) => s().initialStateOptions[t],
337
+ setInitialStateOptions: (t, e) => {
338
+ f((n) => ({
339
+ initialStateOptions: { ...n.initialStateOptions, [t]: e }
332
340
  }));
333
341
  },
334
- updateInitialStateGlobal: (e, s) => {
335
- f((o) => ({
336
- initialStateGlobal: {
337
- ...o.initialStateGlobal,
338
- [e]: s
339
- }
342
+ updateInitialStateGlobal: (t, e) => {
343
+ f((n) => ({
344
+ initialStateGlobal: { ...n.initialStateGlobal, [t]: e }
340
345
  }));
341
346
  },
342
347
  syncInfoStore: /* @__PURE__ */ new Map(),
343
- setSyncInfo: (e, s) => f((o) => {
344
- const t = new Map(o.syncInfoStore);
345
- return t.set(e, s), { ...o, syncInfoStore: t };
348
+ setSyncInfo: (t, e) => f((n) => {
349
+ const o = new Map(n.syncInfoStore);
350
+ return o.set(t, e), { syncInfoStore: o };
346
351
  }),
347
- getSyncInfo: (e) => c().syncInfoStore.get(e) || null
352
+ getSyncInfo: (t) => s().syncInfoStore.get(t) || null
348
353
  }));
349
354
  export {
350
- j as formRefStore,
351
- E as getGlobalStore
355
+ w as buildShadowNode,
356
+ I as formRefStore,
357
+ N as getGlobalStore
352
358
  };
353
359
  //# sourceMappingURL=store.js.map