cogsbox-state 0.5.71 → 0.5.72

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.
@@ -1,15 +1,15 @@
1
1
  "use client";
2
2
  import { jsx as ct } from "react/jsx-runtime";
3
3
  import { useState as et, useRef as Z, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Q, useSyncExternalStore as Et, startTransition as H } from "react";
4
- import { transformStateFunc as wt, isFunction as G, getNestedValue as U, isDeepEqual as M, debounce as Nt } from "./utility.js";
5
- import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Vt, FormControlComponent as $t } from "./Functions.jsx";
4
+ import { transformStateFunc as $t, isFunction as G, getNestedValue as U, isDeepEqual as M, debounce as wt } from "./utility.js";
5
+ import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Nt, FormControlComponent as Vt } from "./Functions.jsx";
6
6
  import "zod";
7
7
  import { getGlobalStore as r, formRefStore as lt } from "./store.js";
8
8
  import { useCogsConfig as At } from "./CogsStateClient.jsx";
9
9
  import rt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function dt(t, i) {
11
- const S = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, g = S(t) || {};
12
- y(t, {
11
+ const S = r.getState().getInitialOptions, m = r.getState().setInitialStateOptions, g = S(t) || {};
12
+ m(t, {
13
13
  ...g,
14
14
  ...i
15
15
  });
@@ -19,19 +19,19 @@ function ut({
19
19
  options: i,
20
20
  initialOptionsPart: S
21
21
  }) {
22
- const y = q(t) || {}, g = S[t] || {}, w = r.getState().setInitialStateOptions, _ = { ...g, ...y };
22
+ const m = q(t) || {}, g = S[t] || {}, $ = r.getState().setInitialStateOptions, _ = { ...g, ...m };
23
23
  let v = !1;
24
24
  if (i)
25
- for (const l in i)
26
- _.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (v = !0, _[l] = i[l]) : (v = !0, _[l] = i[l]);
27
- v && w(t, _);
25
+ for (const d in i)
26
+ _.hasOwnProperty(d) ? d == "localStorage" && i[d] && _[d].key !== i[d]?.key && (v = !0, _[d] = i[d]) : (v = !0, _[d] = i[d]);
27
+ v && $(t, _);
28
28
  }
29
29
  function qt(t, { formElements: i, validation: S }) {
30
30
  return { initialState: t, formElements: i, validation: S };
31
31
  }
32
32
  const zt = (t, i) => {
33
33
  let S = t;
34
- const [y, g] = wt(S);
34
+ const [m, g] = $t(S);
35
35
  (i?.formElements || i?.validation) && Object.keys(g).forEach((v) => {
36
36
  g[v] = g[v] || {}, g[v].formElements = {
37
37
  ...i.formElements,
@@ -40,35 +40,35 @@ const zt = (t, i) => {
40
40
  ...g[v].formElements || {}
41
41
  // State-specific overrides
42
42
  };
43
- }), r.getState().setInitialStates(y);
44
- const w = (v, l) => {
45
- const [f] = et(l?.componentId ?? rt());
43
+ }), r.getState().setInitialStates(m);
44
+ const $ = (v, d) => {
45
+ const [f] = et(d?.componentId ?? rt());
46
46
  ut({
47
47
  stateKey: v,
48
- options: l,
48
+ options: d,
49
49
  initialOptionsPart: g
50
50
  });
51
- const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [h, V] = Ft(
52
- m,
51
+ const e = r.getState().cogsStateStore[v] || m[v], y = d?.modifyState ? d.modifyState(e) : e, [b, N] = Ft(
52
+ y,
53
53
  {
54
54
  stateKey: v,
55
- syncUpdate: l?.syncUpdate,
55
+ syncUpdate: d?.syncUpdate,
56
56
  componentId: f,
57
- localStorage: l?.localStorage,
58
- middleware: l?.middleware,
59
- enabledSync: l?.enabledSync,
60
- reactiveType: l?.reactiveType,
61
- reactiveDeps: l?.reactiveDeps,
62
- initialState: l?.initialState,
63
- dependencies: l?.dependencies
57
+ localStorage: d?.localStorage,
58
+ middleware: d?.middleware,
59
+ enabledSync: d?.enabledSync,
60
+ reactiveType: d?.reactiveType,
61
+ reactiveDeps: d?.reactiveDeps,
62
+ initialState: d?.initialState,
63
+ dependencies: d?.dependencies
64
64
  }
65
65
  );
66
- return V;
66
+ return N;
67
67
  };
68
- function _(v, l) {
69
- ut({ stateKey: v, options: l, initialOptionsPart: g });
68
+ function _(v, d) {
69
+ ut({ stateKey: v, options: d, initialOptionsPart: g });
70
70
  }
71
- return { useCogsState: w, setCogsOptions: _ };
71
+ return { useCogsState: $, setCogsOptions: _ };
72
72
  }, {
73
73
  setUpdaterState: Y,
74
74
  setState: L,
@@ -88,31 +88,31 @@ const zt = (t, i) => {
88
88
  } catch (i) {
89
89
  return console.error("Error loading from localStorage:", i), null;
90
90
  }
91
- }, ht = (t, i, S, y) => {
91
+ }, ht = (t, i, S, m) => {
92
92
  S.log && console.log(
93
93
  "saving to localstorage",
94
94
  i,
95
95
  S.localStorage?.key,
96
- y
96
+ m
97
97
  );
98
98
  const g = G(S.localStorage?.key) ? S.localStorage?.key(t) : S.localStorage?.key;
99
- if (g && y) {
100
- const w = {
99
+ if (g && m) {
100
+ const $ = {
101
101
  state: t,
102
102
  lastUpdated: Date.now(),
103
103
  lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
104
104
  baseServerState: r.getState().serverState[i]
105
- }, _ = `${y}-${i}-${g}`;
106
- window.localStorage.setItem(_, JSON.stringify(w));
105
+ }, _ = `${m}-${i}-${g}`;
106
+ window.localStorage.setItem(_, JSON.stringify($));
107
107
  }
108
- }, bt = (t, i, S, y, g, w) => {
108
+ }, bt = (t, i, S, m, g, $) => {
109
109
  const _ = {
110
110
  initialState: i,
111
111
  updaterState: X(
112
112
  t,
113
- y,
113
+ m,
114
114
  g,
115
- w
115
+ $
116
116
  ),
117
117
  state: S
118
118
  };
@@ -123,39 +123,39 @@ const zt = (t, i) => {
123
123
  const i = r.getState().stateComponents.get(t);
124
124
  if (!i) return;
125
125
  const S = /* @__PURE__ */ new Set();
126
- i.components.forEach((y) => {
127
- S.add(() => y.forceUpdate());
126
+ i.components.forEach((m) => {
127
+ S.add(() => m.forceUpdate());
128
128
  }), queueMicrotask(() => {
129
129
  H(() => {
130
- S.forEach((y) => y());
130
+ S.forEach((m) => m());
131
131
  });
132
132
  });
133
133
  }, Bt = (t, i) => {
134
134
  const S = r.getState().stateComponents.get(t);
135
135
  if (S) {
136
- const y = `${t}////${i}`, g = S.components.get(y);
136
+ const m = `${t}////${i}`, g = S.components.get(m);
137
137
  g && g.forceUpdate();
138
138
  }
139
139
  };
140
140
  function Ft(t, {
141
141
  stateKey: i,
142
142
  serverSync: S,
143
- localStorage: y,
143
+ localStorage: m,
144
144
  formElements: g,
145
- middleware: w,
145
+ middleware: $,
146
146
  reactiveDeps: _,
147
147
  reactiveType: v,
148
- componentId: l,
148
+ componentId: d,
149
149
  initialState: f,
150
150
  syncUpdate: e,
151
- dependencies: m
151
+ dependencies: y
152
152
  } = {}) {
153
- const [h, V] = et({}), { sessionId: A } = At();
154
- let j = !i;
155
- const [u] = et(i ?? rt()), s = r.getState().stateLog[u], D = Z(/* @__PURE__ */ new Set()), x = Z(l ?? rt()), C = Z(null);
156
- C.current = q(u), nt(() => {
157
- if (e && e.stateKey === u && e.path?.[0]) {
158
- L(u, (o) => ({
153
+ const [b, N] = et({}), { sessionId: p } = At();
154
+ let h = !i;
155
+ const [l] = et(i ?? rt()), s = r.getState().stateLog[l], D = Z(/* @__PURE__ */ new Set()), x = Z(d ?? rt()), C = Z(null);
156
+ C.current = q(l), nt(() => {
157
+ if (e && e.stateKey === l && e.path?.[0]) {
158
+ L(l, (o) => ({
159
159
  ...o,
160
160
  [e.path[0]]: e.newValue
161
161
  }));
@@ -166,172 +166,172 @@ function Ft(t, {
166
166
  });
167
167
  }
168
168
  }, [e]), nt(() => {
169
- dt(u, {
169
+ dt(l, {
170
170
  initialState: f
171
171
  });
172
172
  const c = C.current;
173
173
  let o = null;
174
174
  c.log && console.log("newoptions", c);
175
- const d = G(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
176
- d && A && (o = ft(
177
- A + "-" + u + "-" + d
175
+ const u = G(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
176
+ u && p && (o = ft(
177
+ p + "-" + l + "-" + u
178
178
  ));
179
179
  let E = null;
180
180
  f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), bt(
181
- u,
181
+ l,
182
182
  f,
183
183
  E,
184
184
  n,
185
185
  x.current,
186
- A
187
- ), St(u), V({}));
188
- }, [f, ...m || []]), It(() => {
189
- j && dt(u, {
186
+ p
187
+ ), St(l), N({}));
188
+ }, [f, ...y || []]), It(() => {
189
+ h && dt(l, {
190
190
  serverSync: S,
191
191
  formElements: g,
192
192
  initialState: f,
193
- localStorage: y,
194
- middleware: w
193
+ localStorage: m,
194
+ middleware: $
195
195
  });
196
- const c = `${u}////${x.current}`, o = r.getState().stateComponents.get(u) || {
196
+ const c = `${l}////${x.current}`, o = r.getState().stateComponents.get(l) || {
197
197
  components: /* @__PURE__ */ new Map()
198
198
  };
199
199
  return o.components.set(c, {
200
- forceUpdate: () => V({}),
200
+ forceUpdate: () => N({}),
201
201
  paths: /* @__PURE__ */ new Set(),
202
202
  deps: [],
203
203
  depsFunction: _ || void 0,
204
204
  reactiveType: v ?? ["component", "deps"]
205
- }), r.getState().stateComponents.set(u, o), V({}), () => {
206
- const d = `${u}////${x.current}`;
207
- o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(u));
205
+ }), r.getState().stateComponents.set(l, o), N({}), () => {
206
+ const u = `${l}////${x.current}`;
207
+ o && (o.components.delete(u), o.components.size === 0 && r.getState().stateComponents.delete(l));
208
208
  };
209
209
  }, []);
210
- const n = (c, o, d, E) => {
210
+ const n = (c, o, u, E) => {
211
211
  if (Array.isArray(o)) {
212
- const b = `${u}-${o.join(".")}`;
213
- D.current.add(b);
212
+ const F = `${l}-${o.join(".")}`;
213
+ D.current.add(F);
214
214
  }
215
- L(u, (b) => {
216
- const $ = G(c) ? c(b) : c, O = `${u}-${o.join(".")}`;
215
+ L(l, (F) => {
216
+ const V = G(c) ? c(F) : c, O = `${l}-${o.join(".")}`;
217
217
  if (O) {
218
218
  let P = !1, I = r.getState().signalDomElements.get(O);
219
- if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
220
- const k = o.slice(0, -1), p = U($, k);
221
- if (Array.isArray(p)) {
219
+ if ((!I || I.size === 0) && (u.updateType === "insert" || u.updateType === "cut")) {
220
+ const A = o.slice(0, -1), k = U(V, A);
221
+ if (Array.isArray(k)) {
222
222
  P = !0;
223
- const N = `${u}-${k.join(".")}`;
224
- I = r.getState().signalDomElements.get(N);
223
+ const w = `${l}-${A.join(".")}`;
224
+ I = r.getState().signalDomElements.get(w);
225
225
  }
226
226
  }
227
227
  if (I) {
228
- const k = P ? U($, o.slice(0, -1)) : U($, o);
229
- I.forEach(({ parentId: p, position: N, effect: W }) => {
230
- const F = document.querySelector(
231
- `[data-parent-id="${p}"]`
228
+ const A = P ? U(V, o.slice(0, -1)) : U(V, o);
229
+ I.forEach(({ parentId: k, position: w, effect: W }) => {
230
+ const j = document.querySelector(
231
+ `[data-parent-id="${k}"]`
232
232
  );
233
- if (F) {
234
- const st = Array.from(F.childNodes);
235
- if (st[N]) {
236
- const vt = W ? new Function("state", `return (${W})(state)`)(k) : k;
237
- st[N].textContent = String(vt);
233
+ if (j) {
234
+ const st = Array.from(j.childNodes);
235
+ if (st[w]) {
236
+ const vt = W ? new Function("state", `return (${W})(state)`)(A) : A;
237
+ st[w].textContent = String(vt);
238
238
  }
239
239
  }
240
240
  });
241
241
  }
242
242
  }
243
- d.updateType === "update" && (E || C.current?.validationKey) && o && R(
243
+ u.updateType === "update" && (E || C.current?.validationKey) && o && R(
244
244
  (E || C.current?.validationKey) + "." + o.join(".")
245
245
  );
246
246
  const T = o.slice(0, o.length - 1);
247
- d.updateType === "cut" && C.current?.validationKey && R(
247
+ u.updateType === "cut" && C.current?.validationKey && R(
248
248
  C.current?.validationKey + "." + T.join(".")
249
- ), d.updateType === "insert" && C.current?.validationKey && kt(
249
+ ), u.updateType === "insert" && C.current?.validationKey && kt(
250
250
  C.current?.validationKey + "." + T.join(".")
251
- ).filter(([I, k]) => {
252
- let p = I?.split(".").length;
253
- if (I == T.join(".") && p == T.length - 1) {
254
- let N = I + "." + T;
255
- R(I), Ct(N, k);
251
+ ).filter(([I, A]) => {
252
+ let k = I?.split(".").length;
253
+ if (I == T.join(".") && k == T.length - 1) {
254
+ let w = I + "." + T;
255
+ R(I), Ct(w, A);
256
256
  }
257
257
  });
258
- const z = U(b, o), mt = U($, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
258
+ const z = U(F, o), mt = U(V, o), yt = u.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(l);
259
259
  if (at)
260
260
  for (const [P, I] of at.components.entries()) {
261
- let k = !1;
262
- const p = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
263
- if (!p.includes("none")) {
264
- if (p.includes("all")) {
261
+ let A = !1;
262
+ const k = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
263
+ if (!k.includes("none")) {
264
+ if (k.includes("all")) {
265
265
  I.forceUpdate();
266
266
  continue;
267
267
  }
268
- if (p.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (k = !0), !k && p.includes("deps") && I.depsFunction) {
269
- const N = I.depsFunction($);
270
- typeof N == "boolean" ? N && (k = !0) : M(I.deps, N) || (I.deps = N, k = !0);
268
+ if (k.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (A = !0), !A && k.includes("deps") && I.depsFunction) {
269
+ const w = I.depsFunction(V);
270
+ typeof w == "boolean" ? w && (A = !0) : M(I.deps, w) || (I.deps = w, A = !0);
271
271
  }
272
- k && I.forceUpdate();
272
+ A && I.forceUpdate();
273
273
  }
274
274
  }
275
275
  const it = {
276
276
  timeStamp: Date.now(),
277
- stateKey: u,
277
+ stateKey: l,
278
278
  path: o,
279
- updateType: d.updateType,
279
+ updateType: u.updateType,
280
280
  status: "new",
281
281
  oldValue: z,
282
282
  newValue: mt
283
283
  };
284
- if (pt(u, (P) => {
285
- const k = [...P ?? [], it].reduce((p, N) => {
286
- const W = `${N.stateKey}:${JSON.stringify(N.path)}`, F = p.get(W);
287
- return F ? (F.timeStamp = Math.max(F.timeStamp, N.timeStamp), F.newValue = N.newValue, F.oldValue = F.oldValue ?? N.oldValue, F.updateType = N.updateType) : p.set(W, { ...N }), p;
284
+ if (pt(l, (P) => {
285
+ const A = [...P ?? [], it].reduce((k, w) => {
286
+ const W = `${w.stateKey}:${JSON.stringify(w.path)}`, j = k.get(W);
287
+ return j ? (j.timeStamp = Math.max(j.timeStamp, w.timeStamp), j.newValue = w.newValue, j.oldValue = j.oldValue ?? w.oldValue, j.updateType = w.updateType) : k.set(W, { ...w }), k;
288
288
  }, /* @__PURE__ */ new Map());
289
- return Array.from(k.values());
289
+ return Array.from(A.values());
290
290
  }), ht(
291
- $,
292
- u,
291
+ V,
292
+ l,
293
293
  C.current,
294
- A
295
- ), w && w({
294
+ p
295
+ ), $ && $({
296
296
  updateLog: s,
297
297
  update: it
298
298
  }), C.current?.serverSync) {
299
- const P = r.getState().serverState[u], I = C.current?.serverSync;
300
- Tt(u, {
301
- syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: $ }),
299
+ const P = r.getState().serverState[l], I = C.current?.serverSync;
300
+ Tt(l, {
301
+ syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: V }),
302
302
  rollBackState: P,
303
303
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
304
304
  status: "waiting"
305
305
  });
306
306
  }
307
- return $;
307
+ return V;
308
308
  });
309
309
  };
310
- r.getState().updaterState[u] || (Y(
311
- u,
310
+ r.getState().updaterState[l] || (Y(
311
+ l,
312
312
  X(
313
- u,
313
+ l,
314
314
  n,
315
315
  x.current,
316
- A
316
+ p
317
317
  )
318
- ), r.getState().cogsStateStore[u] || L(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
318
+ ), r.getState().cogsStateStore[l] || L(l, t), r.getState().initialStateGlobal[l] || ot(l, t));
319
319
  const a = _t(() => X(
320
- u,
320
+ l,
321
321
  n,
322
322
  x.current,
323
- A
324
- ), [u]);
325
- return [gt(u), a];
323
+ p
324
+ ), [l]);
325
+ return [gt(l), a];
326
326
  }
327
- function X(t, i, S, y) {
327
+ function X(t, i, S, m) {
328
328
  const g = /* @__PURE__ */ new Map();
329
- let w = 0;
329
+ let $ = 0;
330
330
  const _ = (f) => {
331
331
  const e = f.join(".");
332
- for (const [m] of g)
333
- (m === e || m.startsWith(e + ".")) && g.delete(m);
334
- w++;
332
+ for (const [y] of g)
333
+ (y === e || y.startsWith(e + ".")) && g.delete(y);
334
+ $++;
335
335
  }, v = {
336
336
  removeValidation: (f) => {
337
337
  f?.validationKey && R(f.validationKey);
@@ -339,35 +339,35 @@ function X(t, i, S, y) {
339
339
  revertToInitialState: (f) => {
340
340
  const e = r.getState().getInitialOptions(t)?.validation;
341
341
  e?.key && R(e?.key), f?.validationKey && R(f.validationKey);
342
- const m = r.getState().initialStateGlobal[t];
343
- g.clear(), w++;
344
- const h = l(m, []), V = q(t), A = G(V?.localStorage?.key) ? V?.localStorage?.key(m) : V?.localStorage?.key;
345
- return A && localStorage.removeItem(A), H(() => {
346
- Y(t, h), L(t, m);
347
- const j = r.getState().stateComponents.get(t);
348
- j && j.components.forEach((u) => {
349
- u.forceUpdate();
342
+ const y = r.getState().initialStateGlobal[t];
343
+ g.clear(), $++;
344
+ const b = d(y, []), N = q(t), p = G(N?.localStorage?.key) ? N?.localStorage?.key(y) : N?.localStorage?.key, h = `${m}-${t}-${p}`;
345
+ return h && localStorage.removeItem(h), H(() => {
346
+ Y(t, b), L(t, y);
347
+ const l = r.getState().stateComponents.get(t);
348
+ l && l.components.forEach((s) => {
349
+ s.forceUpdate();
350
350
  });
351
- }), m;
351
+ }), y;
352
352
  },
353
353
  updateInitialState: (f) => {
354
- g.clear(), w++;
355
- const e = r.getState().initialStateGlobal[t], m = q(t), h = G(m?.localStorage?.key) ? m?.localStorage?.key(e) : m?.localStorage?.key;
356
- h && localStorage.removeItem(h);
357
- const V = X(
354
+ g.clear(), $++;
355
+ const e = r.getState().initialStateGlobal[t], y = q(t), b = G(y?.localStorage?.key) ? y?.localStorage?.key(e) : y?.localStorage?.key, N = `${m}-${t}-${b}`;
356
+ N && localStorage.removeItem(N);
357
+ const p = X(
358
358
  t,
359
359
  i,
360
360
  S,
361
- y
361
+ m
362
362
  );
363
363
  return H(() => {
364
- ot(t, f), Y(t, V), L(t, f);
365
- const A = r.getState().stateComponents.get(t);
366
- A && A.components.forEach((j) => {
367
- j.forceUpdate();
364
+ ot(t, f), Y(t, p), L(t, f);
365
+ const h = r.getState().stateComponents.get(t);
366
+ h && h.components.forEach((l) => {
367
+ l.forceUpdate();
368
368
  });
369
369
  }), {
370
- fetchId: (A) => V.get()[A]
370
+ fetchId: (h) => p.get()[h]
371
371
  };
372
372
  },
373
373
  _initialState: r.getState().initialStateGlobal[t],
@@ -378,22 +378,22 @@ function X(t, i, S, y) {
378
378
  return !!(f && M(f, gt(t)));
379
379
  }
380
380
  };
381
- function l(f, e = [], m) {
382
- const h = e.map(String).join(".");
383
- g.get(h);
384
- const V = function() {
381
+ function d(f, e = [], y) {
382
+ const b = e.map(String).join(".");
383
+ g.get(b);
384
+ const N = function() {
385
385
  return r().getNestedState(t, e);
386
386
  };
387
- Object.keys(v).forEach((u) => {
388
- V[u] = v[u];
387
+ Object.keys(v).forEach((l) => {
388
+ N[l] = v[l];
389
389
  });
390
- const A = {
391
- apply(u, s, D) {
390
+ const p = {
391
+ apply(l, s, D) {
392
392
  return console.log(
393
393
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${e.join(".")}`
394
394
  ), console.trace("Apply trap stack trace"), r().getNestedState(t, e);
395
395
  },
396
- get(u, s) {
396
+ get(l, s) {
397
397
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
398
398
  const n = e.join("."), a = `${t}////${S}`, c = r.getState().stateComponents.get(t);
399
399
  if (c) {
@@ -415,8 +415,8 @@ function X(t, i, S, y) {
415
415
  };
416
416
  if (s === "removeStorage")
417
417
  return () => {
418
- const n = r.getState().initialStateGlobal[t], a = q(t), c = G(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key;
419
- c && localStorage.removeItem(c);
418
+ const n = r.getState().initialStateGlobal[t], a = q(t), c = G(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key, o = `${m}-${t}-${c}`;
419
+ o && localStorage.removeItem(o);
420
420
  };
421
421
  if (s === "showValidationErrors")
422
422
  return () => {
@@ -430,42 +430,42 @@ function X(t, i, S, y) {
430
430
  return () => {
431
431
  const n = r.getState().getSelectedIndex(t, e.join("."));
432
432
  if (n !== void 0)
433
- return l(
433
+ return d(
434
434
  f[n],
435
435
  [...e, n.toString()],
436
- m
436
+ y
437
437
  );
438
438
  };
439
439
  if (s === "stateSort")
440
440
  return (n) => {
441
- const o = [...r.getState().getNestedState(t, e).map((d, E) => ({
442
- ...d,
441
+ const o = [...r.getState().getNestedState(t, e).map((u, E) => ({
442
+ ...u,
443
443
  __origIndex: E.toString()
444
444
  }))].sort(n);
445
- return g.clear(), w++, l(o, e, {
446
- filtered: [...m?.filtered || [], e],
445
+ return g.clear(), $++, d(o, e, {
446
+ filtered: [...y?.filtered || [], e],
447
447
  validIndices: o.map(
448
- (d) => parseInt(d.__origIndex)
448
+ (u) => parseInt(u.__origIndex)
449
449
  )
450
450
  });
451
451
  };
452
452
  if (s === "stateMap" || s === "stateMapNoRender")
453
453
  return (n) => {
454
- const a = m?.filtered?.some(
454
+ const a = y?.filtered?.some(
455
455
  (o) => o.join(".") === e.join(".")
456
456
  ), c = a ? f : r.getState().getNestedState(t, e);
457
- return s !== "stateMapNoRender" && (g.clear(), w++), c.map((o, d) => {
458
- const E = a && o.__origIndex ? o.__origIndex : d, b = l(
457
+ return s !== "stateMapNoRender" && (g.clear(), $++), c.map((o, u) => {
458
+ const E = a && o.__origIndex ? o.__origIndex : u, F = d(
459
459
  o,
460
460
  [...e, E.toString()],
461
- m
461
+ y
462
462
  );
463
463
  return n(
464
464
  o,
465
- b,
466
- d,
465
+ F,
466
+ u,
467
467
  f,
468
- l(f, e, m)
468
+ d(f, e, y)
469
469
  );
470
470
  });
471
471
  };
@@ -477,21 +477,21 @@ function X(t, i, S, y) {
477
477
  _mapFn: n
478
478
  // Pass the actual function, not string
479
479
  },
480
- rebuildStateShape: l
480
+ rebuildStateShape: d
481
481
  });
482
482
  if (s === "stateFlattenOn")
483
483
  return (n) => {
484
- const c = m?.filtered?.some(
485
- (d) => d.join(".") === e.join(".")
484
+ const c = y?.filtered?.some(
485
+ (u) => u.join(".") === e.join(".")
486
486
  ) ? f : r.getState().getNestedState(t, e);
487
- g.clear(), w++;
487
+ g.clear(), $++;
488
488
  const o = c.flatMap(
489
- (d, E) => d[n] ?? []
489
+ (u, E) => u[n] ?? []
490
490
  );
491
- return l(
491
+ return d(
492
492
  o,
493
493
  [...e, "[*]", n],
494
- m
494
+ y
495
495
  );
496
496
  };
497
497
  if (s === "findWith")
@@ -500,37 +500,37 @@ function X(t, i, S, y) {
500
500
  (E) => E[n] === a
501
501
  );
502
502
  if (c === -1) return;
503
- const o = f[c], d = [...e, c.toString()];
504
- return g.clear(), w++, g.clear(), w++, l(o, d);
503
+ const o = f[c], u = [...e, c.toString()];
504
+ return g.clear(), $++, g.clear(), $++, d(o, u);
505
505
  };
506
506
  if (s === "index")
507
507
  return (n) => {
508
508
  const a = f[n];
509
- return l(a, [...e, n.toString()]);
509
+ return d(a, [...e, n.toString()]);
510
510
  };
511
511
  if (s === "insert")
512
- return (n) => (_(e), K(i, n, e, t), l(
512
+ return (n) => (_(e), K(i, n, e, t), d(
513
513
  r.getState().cogsStateStore[t],
514
514
  []
515
515
  ));
516
516
  if (s === "uniqueInsert")
517
517
  return (n, a, c) => {
518
- const o = r.getState().getNestedState(t, e), d = G(n) ? n(o) : n;
518
+ const o = r.getState().getNestedState(t, e), u = G(n) ? n(o) : n;
519
519
  let E = null;
520
- if (!o.some(($) => {
520
+ if (!o.some((V) => {
521
521
  if (a) {
522
522
  const T = a.every(
523
- (z) => M($[z], d[z])
523
+ (z) => M(V[z], u[z])
524
524
  );
525
- return T && (E = $), T;
525
+ return T && (E = V), T;
526
526
  }
527
- const O = M($, d);
528
- return O && (E = $), O;
527
+ const O = M(V, u);
528
+ return O && (E = V), O;
529
529
  }))
530
- _(e), K(i, d, e, t);
530
+ _(e), K(i, u, e, t);
531
531
  else if (c && E) {
532
- const $ = c(E), O = o.map(
533
- (T) => M(T, E) ? $ : T
532
+ const V = c(E), O = o.map(
533
+ (T) => M(T, E) ? V : T
534
534
  );
535
535
  _(e), B(i, O, e);
536
536
  }
@@ -551,14 +551,14 @@ function X(t, i, S, y) {
551
551
  };
552
552
  if (s === "stateFilter")
553
553
  return (n) => {
554
- const a = f.map((d, E) => ({
555
- ...d,
554
+ const a = f.map((u, E) => ({
555
+ ...u,
556
556
  __origIndex: E.toString()
557
557
  })), c = [], o = [];
558
- for (let d = 0; d < a.length; d++)
559
- n(a[d], d) && (c.push(d), o.push(a[d]));
560
- return g.clear(), w++, l(o, e, {
561
- filtered: [...m?.filtered || [], e],
558
+ for (let u = 0; u < a.length; u++)
559
+ n(a[u], u) && (c.push(u), o.push(a[u]));
560
+ return g.clear(), $++, d(o, e, {
561
+ filtered: [...y?.filtered || [], e],
562
562
  validIndices: c
563
563
  // Always pass validIndices, even if empty
564
564
  });
@@ -599,7 +599,7 @@ function X(t, i, S, y) {
599
599
  return r.getState().getSyncInfo(n);
600
600
  }
601
601
  if (s == "getLocalStorage")
602
- return (n) => ft(y + "-" + t + "-" + n);
602
+ return (n) => ft(m + "-" + t + "-" + n);
603
603
  if (s === "_selected") {
604
604
  const n = e.slice(0, -1), a = n.join("."), c = r.getState().getNestedState(t, n);
605
605
  return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, a) : void 0;
@@ -608,8 +608,8 @@ function X(t, i, S, y) {
608
608
  return (n) => {
609
609
  const a = e.slice(0, -1), c = Number(e[e.length - 1]), o = a.join(".");
610
610
  n ? r.getState().setSelectedIndex(t, o, c) : r.getState().setSelectedIndex(t, o, void 0);
611
- const d = r.getState().getNestedState(t, [...a]);
612
- B(i, d, a), _(a);
611
+ const u = r.getState().getNestedState(t, [...a]);
612
+ B(i, u, a), _(a);
613
613
  };
614
614
  if (e.length == 0) {
615
615
  if (s === "validateZodSchema")
@@ -626,9 +626,9 @@ function X(t, i, S, y) {
626
626
  o && o.length > 0 && o.forEach(([E]) => {
627
627
  E && E.startsWith(n.key) && R(E);
628
628
  });
629
- const d = n.zodSchema.safeParse(c);
630
- return d.success ? !0 : (d.error.errors.forEach((b) => {
631
- const $ = b.path, O = b.message, T = [n.key, ...$].join(".");
629
+ const u = n.zodSchema.safeParse(c);
630
+ return u.success ? !0 : (u.error.errors.forEach((F) => {
631
+ const V = F.path, O = F.message, T = [n.key, ...V].join(".");
632
632
  a(T, O);
633
633
  }), St(t), !1);
634
634
  } catch (o) {
@@ -658,13 +658,13 @@ function X(t, i, S, y) {
658
658
  children: n,
659
659
  hideMessage: a
660
660
  }) => /* @__PURE__ */ ct(
661
- Vt,
661
+ Nt,
662
662
  {
663
663
  formOpts: a ? { validation: { message: "" } } : void 0,
664
664
  path: e,
665
665
  validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
666
666
  stateKey: t,
667
- validIndices: m?.validIndices,
667
+ validIndices: y?.validIndices,
668
668
  children: n
669
669
  }
670
670
  );
@@ -674,7 +674,7 @@ function X(t, i, S, y) {
674
674
  if (s === "update")
675
675
  return (n, a) => {
676
676
  if (a?.debounce)
677
- Nt(() => {
677
+ wt(() => {
678
678
  B(i, n, e, "");
679
679
  const c = r.getState().getNestedState(t, e);
680
680
  a?.afterUpdate && a.afterUpdate(c);
@@ -688,7 +688,7 @@ function X(t, i, S, y) {
688
688
  };
689
689
  if (s === "formElement")
690
690
  return (n, a) => /* @__PURE__ */ ct(
691
- $t,
691
+ Vt,
692
692
  {
693
693
  setState: i,
694
694
  stateKey: t,
@@ -698,15 +698,15 @@ function X(t, i, S, y) {
698
698
  }
699
699
  );
700
700
  const x = [...e, s], C = r.getState().getNestedState(t, x);
701
- return l(C, x, m);
701
+ return d(C, x, y);
702
702
  }
703
- }, j = new Proxy(V, A);
704
- return g.set(h, {
705
- proxy: j,
706
- stateVersion: w
707
- }), j;
703
+ }, h = new Proxy(N, p);
704
+ return g.set(b, {
705
+ proxy: h,
706
+ stateVersion: $
707
+ }), h;
708
708
  }
709
- return l(
709
+ return d(
710
710
  r.getState().getNestedState(t, [])
711
711
  );
712
712
  }
@@ -722,7 +722,7 @@ function jt({
722
722
  S,
723
723
  t._path
724
724
  ).stateMapNoRender(
725
- (g, w, _, v, l) => t._mapFn(g, w, _, v, l)
725
+ (g, $, _, v, d) => t._mapFn(g, $, _, v, d)
726
726
  ) : null;
727
727
  }
728
728
  function Ot({
@@ -730,9 +730,9 @@ function Ot({
730
730
  }) {
731
731
  const i = Z(null), S = `${t._stateKey}-${t._path.join(".")}`;
732
732
  return nt(() => {
733
- const y = i.current;
734
- if (!y || !y.parentElement) return;
735
- const g = y.parentElement, _ = Array.from(g.childNodes).indexOf(y);
733
+ const m = i.current;
734
+ if (!m || !m.parentElement) return;
735
+ const g = m.parentElement, _ = Array.from(g.childNodes).indexOf(m);
736
736
  let v = g.getAttribute("data-parent-id");
737
737
  v || (v = `parent-${crypto.randomUUID()}`, g.setAttribute("data-parent-id", v));
738
738
  const f = {
@@ -743,21 +743,21 @@ function Ot({
743
743
  };
744
744
  r.getState().addSignalElement(S, f);
745
745
  const e = r.getState().getNestedState(t._stateKey, t._path);
746
- let m;
746
+ let y;
747
747
  if (t._effect)
748
748
  try {
749
- m = new Function(
749
+ y = new Function(
750
750
  "state",
751
751
  `return (${t._effect})(state)`
752
752
  )(e);
753
- } catch (V) {
754
- console.error("Error evaluating effect function during mount:", V), m = e;
753
+ } catch (N) {
754
+ console.error("Error evaluating effect function during mount:", N), y = e;
755
755
  }
756
756
  else
757
- m = e;
758
- m !== null && typeof m == "object" && (m = JSON.stringify(m));
759
- const h = document.createTextNode(String(m));
760
- y.replaceWith(h);
757
+ y = e;
758
+ y !== null && typeof y == "object" && (y = JSON.stringify(y));
759
+ const b = document.createTextNode(String(y));
760
+ m.replaceWith(b);
761
761
  }, [t._stateKey, t._path.join("."), t._effect]), Q("span", {
762
762
  ref: i,
763
763
  style: { display: "none" },
@@ -767,13 +767,13 @@ function Ot({
767
767
  function Jt(t) {
768
768
  const i = Et(
769
769
  (S) => {
770
- const y = r.getState().stateComponents.get(t._stateKey) || {
770
+ const m = r.getState().stateComponents.get(t._stateKey) || {
771
771
  components: /* @__PURE__ */ new Map()
772
772
  };
773
- return y.components.set(t._stateKey, {
773
+ return m.components.set(t._stateKey, {
774
774
  forceUpdate: S,
775
775
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
776
- }), () => y.components.delete(t._stateKey);
776
+ }), () => m.components.delete(t._stateKey);
777
777
  },
778
778
  () => r.getState().getNestedState(t._stateKey, t._path)
779
779
  );