cogsbox-state 0.5.29 → 0.5.31

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.
@@ -2,10 +2,10 @@
2
2
  import { jsx as it } from "react/jsx-runtime";
3
3
  import { useState as Y, useRef as J, useEffect as K, useLayoutEffect as It, useMemo as Et, createElement as Q, useSyncExternalStore as _t, startTransition as B } from "react";
4
4
  import { transformStateFunc as pt, isFunction as ft, getNestedValue as G, isDeepEqual as W, debounce as wt } from "./utility.js";
5
- import { pushFunc as st, updateFn as z, cutFunc as ct, ValidationWrapper as $t, FormControlComponent as Vt } from "./Functions.jsx";
5
+ import { pushFunc as st, updateFn as z, cutFunc as ct, 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
- import { useCogsConfig as Nt } from "./CogsStateClient.jsx";
8
+ import { useCogsConfig as $t } from "./CogsStateClient.jsx";
9
9
  import tt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function dt(t, i) {
11
11
  const f = r.getState().getInitialOptions, g = r.getState().setInitialStateOptions, d = f(t) || {};
@@ -48,7 +48,7 @@ const zt = (t, i) => {
48
48
  options: S,
49
49
  initialOptionsPart: d
50
50
  });
51
- const u = r.getState().cogsStateStore[v] || g[v], e = S?.modifyState ? S.modifyState(u) : u, [I, N] = Ot(
51
+ const u = r.getState().cogsStateStore[v] || g[v], e = S?.modifyState ? S.modifyState(u) : u, [I, $] = Ot(
52
52
  e,
53
53
  {
54
54
  stateKey: v,
@@ -63,7 +63,7 @@ const zt = (t, i) => {
63
63
  localStorageKey: S?.localStorageKey
64
64
  }
65
65
  );
66
- return N;
66
+ return $;
67
67
  };
68
68
  function _(v, S) {
69
69
  ut({ stateKey: v, options: S, initialOptionsPart: d });
@@ -71,15 +71,15 @@ const zt = (t, i) => {
71
71
  return { useCogsState: E, setCogsOptions: _ };
72
72
  }, {
73
73
  setUpdaterState: Z,
74
- setState: D,
74
+ setState: k,
75
75
  getInitialOptions: et,
76
76
  getKeyState: gt,
77
77
  getValidationErrors: ht,
78
- setStateLog: At,
78
+ setStateLog: Tt,
79
79
  updateInitialStateGlobal: nt,
80
- addValidationError: Ct,
80
+ addValidationError: At,
81
81
  removeValidationError: P,
82
- setServerSyncActions: Tt
82
+ setServerSyncActions: Ct
83
83
  } = r.getState(), St = (t) => {
84
84
  if (!t) return null;
85
85
  try {
@@ -89,13 +89,13 @@ const zt = (t, i) => {
89
89
  return console.error("Error loading from localStorage:", i), null;
90
90
  }
91
91
  }, Ft = (t, i, f, g) => {
92
- if (f.localStorageKey) {
92
+ if (console.log("saveToLocalStorage", t, i, f), f.localStorage?.key) {
93
93
  const d = {
94
94
  state: t,
95
95
  lastUpdated: Date.now(),
96
96
  lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
97
97
  baseServerState: r.getState().serverState[i]
98
- }, E = f.initState ? `${g}-${i}-${f.localStorageKey}` : i;
98
+ }, E = f.initState ? `${g}-${i}-${f.localStorage?.key}` : i;
99
99
  window.localStorage.setItem(E, JSON.stringify(d));
100
100
  }
101
101
  }, jt = (t, i, f, g, d, E) => {
@@ -110,7 +110,7 @@ const zt = (t, i) => {
110
110
  state: f
111
111
  };
112
112
  B(() => {
113
- nt(t, _.initialState), Z(t, _.updaterState), D(t, _.state);
113
+ nt(t, _.initialState), Z(t, _.updaterState), k(t, _.state);
114
114
  });
115
115
  }, mt = (t) => {
116
116
  const i = r.getState().stateComponents.get(t);
@@ -143,12 +143,12 @@ function Ot(t, {
143
143
  initState: u,
144
144
  syncUpdate: e
145
145
  } = {}) {
146
- const [I, N] = Y({}), { sessionId: F } = Nt();
146
+ const [I, $] = Y({}), { sessionId: F } = $t();
147
147
  let R = !i;
148
- const [l] = Y(i ?? tt()), x = r.getState().stateLog[l], s = J(/* @__PURE__ */ new Set()), M = J(S ?? tt()), C = J(null);
149
- C.current = et(l), K(() => {
148
+ const [l] = Y(i ?? tt()), x = r.getState().stateLog[l], s = J(/* @__PURE__ */ new Set()), b = J(S ?? tt()), A = J(null);
149
+ A.current = et(l), K(() => {
150
150
  if (e && e.stateKey === l && e.path?.[0]) {
151
- D(l, (o) => ({
151
+ k(l, (o) => ({
152
152
  ...o,
153
153
  [e.path[0]]: e.newValue
154
154
  }));
@@ -172,9 +172,9 @@ function Ot(t, {
172
172
  u?.initialState,
173
173
  o,
174
174
  L,
175
- M.current,
175
+ b.current,
176
176
  F
177
- ), console.log("newState222", o), mt(l), N({}));
177
+ ), mt(l), $({}));
178
178
  }, [w, ...u?.dependencies || []]), It(() => {
179
179
  R && dt(l, {
180
180
  serverSync: f,
@@ -183,17 +183,17 @@ function Ot(t, {
183
183
  localStorage: g,
184
184
  middleware: E
185
185
  });
186
- const a = `${l}////${M.current}`, o = r.getState().stateComponents.get(l) || {
186
+ const a = `${l}////${b.current}`, o = r.getState().stateComponents.get(l) || {
187
187
  components: /* @__PURE__ */ new Map()
188
188
  };
189
189
  return o.components.set(a, {
190
- forceUpdate: () => N({}),
190
+ forceUpdate: () => $({}),
191
191
  paths: /* @__PURE__ */ new Set(),
192
192
  deps: [],
193
193
  depsFunction: _ || void 0,
194
194
  reactiveType: v ?? ["component", "deps"]
195
- }), r.getState().stateComponents.set(l, o), N({}), () => {
196
- const c = `${l}////${M.current}`;
195
+ }), r.getState().stateComponents.set(l, o), $({}), () => {
196
+ const c = `${l}////${b.current}`;
197
197
  o && (o.components.delete(c), o.components.size === 0 && r.getState().stateComponents.delete(l));
198
198
  };
199
199
  }, []);
@@ -202,62 +202,62 @@ function Ot(t, {
202
202
  const p = `${l}-${o.join(".")}`;
203
203
  s.current.add(p);
204
204
  }
205
- D(l, (p) => {
205
+ k(l, (p) => {
206
206
  const h = ft(a) ? a(p) : a, j = `${l}-${o.join(".")}`;
207
207
  if (j) {
208
208
  let U = !1, y = r.getState().signalDomElements.get(j);
209
209
  if ((!y || y.size === 0) && (c.updateType === "insert" || c.updateType === "cut")) {
210
- const V = o.slice(0, -1), A = G(h, V);
211
- if (Array.isArray(A)) {
210
+ const V = o.slice(0, -1), T = G(h, V);
211
+ if (Array.isArray(T)) {
212
212
  U = !0;
213
- const $ = `${l}-${V.join(".")}`;
214
- y = r.getState().signalDomElements.get($);
213
+ const N = `${l}-${V.join(".")}`;
214
+ y = r.getState().signalDomElements.get(N);
215
215
  }
216
216
  }
217
217
  if (y) {
218
218
  const V = U ? G(h, o.slice(0, -1)) : G(h, o);
219
- y.forEach(({ parentId: A, position: $, effect: k }) => {
220
- const b = document.querySelector(
221
- `[data-parent-id="${A}"]`
219
+ y.forEach(({ parentId: T, position: N, effect: D }) => {
220
+ const O = document.querySelector(
221
+ `[data-parent-id="${T}"]`
222
222
  );
223
- if (b) {
224
- const ot = Array.from(b.childNodes);
225
- if (ot[$]) {
226
- const vt = k ? new Function("state", `return (${k})(state)`)(V) : V;
227
- ot[$].textContent = String(vt);
223
+ if (O) {
224
+ const ot = Array.from(O.childNodes);
225
+ if (ot[N]) {
226
+ const vt = D ? new Function("state", `return (${D})(state)`)(V) : V;
227
+ ot[N].textContent = String(vt);
228
228
  }
229
229
  }
230
230
  });
231
231
  }
232
232
  }
233
- c.updateType === "update" && (m || C.current?.validationKey) && o && P(
234
- (m || C.current?.validationKey) + "." + o.join(".")
233
+ c.updateType === "update" && (m || A.current?.validationKey) && o && P(
234
+ (m || A.current?.validationKey) + "." + o.join(".")
235
235
  );
236
- const T = o.slice(0, o.length - 1);
237
- c.updateType === "cut" && C.current?.validationKey && P(
238
- C.current?.validationKey + "." + T.join(".")
239
- ), c.updateType === "insert" && C.current?.validationKey && ht(
240
- C.current?.validationKey + "." + T.join(".")
236
+ const C = o.slice(0, o.length - 1);
237
+ c.updateType === "cut" && A.current?.validationKey && P(
238
+ A.current?.validationKey + "." + C.join(".")
239
+ ), c.updateType === "insert" && A.current?.validationKey && ht(
240
+ A.current?.validationKey + "." + C.join(".")
241
241
  ).filter(([y, V]) => {
242
- let A = y?.split(".").length;
243
- if (y == T.join(".") && A == T.length - 1) {
244
- let $ = y + "." + T;
245
- P(y), Ct($, V);
242
+ let T = y?.split(".").length;
243
+ if (y == C.join(".") && T == C.length - 1) {
244
+ let N = y + "." + C;
245
+ P(y), At(N, V);
246
246
  }
247
247
  });
248
- const O = G(p, o), q = G(h, o), yt = c.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), rt = r.getState().stateComponents.get(l);
248
+ const M = G(p, o), q = G(h, o), yt = c.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), rt = r.getState().stateComponents.get(l);
249
249
  if (rt)
250
250
  for (const [U, y] of rt.components.entries()) {
251
251
  let V = !1;
252
- const A = Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"];
253
- if (!A.includes("none")) {
254
- if (A.includes("all")) {
252
+ const T = Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"];
253
+ if (!T.includes("none")) {
254
+ if (T.includes("all")) {
255
255
  y.forceUpdate();
256
256
  continue;
257
257
  }
258
- if (A.includes("component") && y.paths && (y.paths.has(yt) || y.paths.has("")) && (V = !0), !V && A.includes("deps") && y.depsFunction) {
259
- const $ = y.depsFunction(h);
260
- typeof $ == "boolean" ? $ && (V = !0) : W(y.deps, $) || (y.deps = $, V = !0);
258
+ if (T.includes("component") && y.paths && (y.paths.has(yt) || y.paths.has("")) && (V = !0), !V && T.includes("deps") && y.depsFunction) {
259
+ const N = y.depsFunction(h);
260
+ typeof N == "boolean" ? N && (V = !0) : W(y.deps, N) || (y.deps = N, V = !0);
261
261
  }
262
262
  V && y.forceUpdate();
263
263
  }
@@ -268,26 +268,26 @@ function Ot(t, {
268
268
  path: o,
269
269
  updateType: c.updateType,
270
270
  status: "new",
271
- oldValue: O,
271
+ oldValue: M,
272
272
  newValue: q
273
273
  };
274
- if (At(l, (U) => {
275
- const V = [...U ?? [], at].reduce((A, $) => {
276
- const k = `${$.stateKey}:${JSON.stringify($.path)}`, b = A.get(k);
277
- return b ? (b.timeStamp = Math.max(b.timeStamp, $.timeStamp), b.newValue = $.newValue, b.oldValue = b.oldValue ?? $.oldValue, b.updateType = $.updateType) : A.set(k, { ...$ }), A;
274
+ if (Tt(l, (U) => {
275
+ const V = [...U ?? [], at].reduce((T, N) => {
276
+ const D = `${N.stateKey}:${JSON.stringify(N.path)}`, O = T.get(D);
277
+ return O ? (O.timeStamp = Math.max(O.timeStamp, N.timeStamp), O.newValue = N.newValue, O.oldValue = O.oldValue ?? N.oldValue, O.updateType = N.updateType) : T.set(D, { ...N }), T;
278
278
  }, /* @__PURE__ */ new Map());
279
279
  return Array.from(V.values());
280
280
  }), Ft(
281
281
  h,
282
282
  l,
283
- C.current,
283
+ A.current,
284
284
  F
285
285
  ), E && E({
286
286
  updateLog: x,
287
287
  update: at
288
- }), C.current?.serverSync) {
289
- const U = r.getState().serverState[l], y = C.current?.serverSync;
290
- Tt(l, {
288
+ }), A.current?.serverSync) {
289
+ const U = r.getState().serverState[l], y = A.current?.serverSync;
290
+ Ct(l, {
291
291
  syncKey: typeof y.syncKey == "string" ? y.syncKey : y.syncKey({ state: h }),
292
292
  rollBackState: U,
293
293
  actionTimeStamp: Date.now() + (y.debounce ?? 3e3),
@@ -302,14 +302,14 @@ function Ot(t, {
302
302
  H(
303
303
  l,
304
304
  L,
305
- M.current,
305
+ b.current,
306
306
  F
307
307
  )
308
- ), r.getState().cogsStateStore[l] || D(l, t), r.getState().initialStateGlobal[l] || nt(l, t));
308
+ ), r.getState().cogsStateStore[l] || k(l, t), r.getState().initialStateGlobal[l] || nt(l, t));
309
309
  const n = Et(() => H(
310
310
  l,
311
311
  L,
312
- M.current,
312
+ b.current,
313
313
  F
314
314
  ), [l]);
315
315
  return [gt(l), n];
@@ -331,9 +331,9 @@ function H(t, i, f, g) {
331
331
  e?.key && P(e?.key), u?.validationKey && P(u.validationKey);
332
332
  const I = r.getState().initialStateGlobal[t];
333
333
  d.clear(), E++;
334
- const N = w(I, []);
334
+ const $ = w(I, []);
335
335
  B(() => {
336
- Z(t, N), D(t, I);
336
+ Z(t, $), k(t, I);
337
337
  const F = r.getState().stateComponents.get(t);
338
338
  F && F.components.forEach((l) => {
339
339
  l.forceUpdate();
@@ -353,10 +353,10 @@ function H(t, i, f, g) {
353
353
  g
354
354
  );
355
355
  return B(() => {
356
- nt(t, u), Z(t, e), D(t, u);
356
+ nt(t, u), Z(t, e), k(t, u);
357
357
  const I = r.getState().stateComponents.get(t);
358
- I && I.components.forEach((N) => {
359
- N.forceUpdate();
358
+ I && I.components.forEach(($) => {
359
+ $.forceUpdate();
360
360
  }), localStorage.removeItem(t);
361
361
  }), {
362
362
  fetchId: (I) => e.get()[I]
@@ -371,8 +371,8 @@ function H(t, i, f, g) {
371
371
  }
372
372
  };
373
373
  function w(u, e = [], I) {
374
- const N = e.map(String).join(".");
375
- d.get(N);
374
+ const $ = e.map(String).join(".");
375
+ d.get($);
376
376
  const F = function() {
377
377
  return r().getNestedState(t, e);
378
378
  };
@@ -380,7 +380,7 @@ function H(t, i, f, g) {
380
380
  F[x] = S[x];
381
381
  });
382
382
  const R = {
383
- apply(x, s, M) {
383
+ apply(x, s, b) {
384
384
  return r().getNestedState(t, e);
385
385
  },
386
386
  get(x, s) {
@@ -479,20 +479,20 @@ function H(t, i, f, g) {
479
479
  let p = null;
480
480
  if (!c.some((j) => {
481
481
  if (a) {
482
- const O = a.every(
482
+ const M = a.every(
483
483
  (q) => W(j[q], m[q])
484
484
  );
485
- return O && (p = j), O;
485
+ return M && (p = j), M;
486
486
  }
487
- const T = W(j, m);
488
- return T && (p = j), T;
487
+ const C = W(j, m);
488
+ return C && (p = j), C;
489
489
  }))
490
490
  _(e), st(i, m, e, t);
491
491
  else if (o && p) {
492
- const j = o(p), T = c.map(
493
- (O) => W(O, p) ? j : O
492
+ const j = o(p), C = c.map(
493
+ (M) => W(M, p) ? j : M
494
494
  );
495
- _(e), z(i, T, e);
495
+ _(e), z(i, C, e);
496
496
  }
497
497
  };
498
498
  if (s === "cut")
@@ -510,19 +510,19 @@ function H(t, i, f, g) {
510
510
  return d.clear(), E++, w(c, e, {
511
511
  filtered: [...I?.filtered || [], e],
512
512
  validIndices: o
513
- // Pass through the meta
513
+ // Always pass validIndices, even if empty
514
514
  });
515
515
  };
516
516
  }
517
- const M = e[e.length - 1];
518
- if (!isNaN(Number(M))) {
517
+ const b = e[e.length - 1];
518
+ if (!isNaN(Number(b))) {
519
519
  const n = e.slice(0, -1), a = r.getState().getNestedState(t, n);
520
520
  if (Array.isArray(a) && s === "cut")
521
521
  return () => ct(
522
522
  i,
523
523
  n,
524
524
  t,
525
- Number(M)
525
+ Number(b)
526
526
  );
527
527
  }
528
528
  if (s === "get")
@@ -578,10 +578,8 @@ function H(t, i, f, g) {
578
578
  });
579
579
  const m = n.zodSchema.safeParse(o);
580
580
  return m.success ? !0 : (m.error.errors.forEach((h) => {
581
- const j = h.path, T = h.message, O = [n.key, ...j].join(".");
582
- a(O, T), console.log(
583
- `Validation error at ${O}: ${T}`
584
- );
581
+ const j = h.path, C = h.message, M = [n.key, ...j].join(".");
582
+ a(M, C);
585
583
  }), mt(t), !1);
586
584
  } catch (c) {
587
585
  return console.error("Zod schema validation failed", c), !1;
@@ -610,7 +608,7 @@ function H(t, i, f, g) {
610
608
  children: n,
611
609
  hideMessage: a
612
610
  }) => /* @__PURE__ */ it(
613
- $t,
611
+ Nt,
614
612
  {
615
613
  formOpts: a ? { validation: { message: "" } } : void 0,
616
614
  path: e,
@@ -649,11 +647,11 @@ function H(t, i, f, g) {
649
647
  formOpts: a
650
648
  }
651
649
  );
652
- const C = [...e, s], L = r.getState().getNestedState(t, C);
653
- return w(L, C, I);
650
+ const A = [...e, s], L = r.getState().getNestedState(t, A);
651
+ return w(L, A, I);
654
652
  }
655
653
  }, l = new Proxy(F, R);
656
- return d.set(N, {
654
+ return d.set($, {
657
655
  proxy: l,
658
656
  stateVersion: E
659
657
  }), l;
@@ -670,7 +668,7 @@ function bt({
670
668
  rebuildStateShape: i
671
669
  }) {
672
670
  const f = r().getNestedState(t._stateKey, t._path);
673
- return console.log("value", f), Array.isArray(f) ? i(
671
+ return Array.isArray(f) ? i(
674
672
  f,
675
673
  t._path
676
674
  ).stateMapNoRender(
@@ -702,8 +700,8 @@ function Mt({
702
700
  "state",
703
701
  `return (${t._effect})(state)`
704
702
  )(u);
705
- } catch (N) {
706
- console.error("Error evaluating effect function during mount:", N), e = u;
703
+ } catch ($) {
704
+ console.error("Error evaluating effect function during mount:", $), e = u;
707
705
  }
708
706
  else
709
707
  e = u;