cogsbox-state 0.5.365 → 0.5.366

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,7 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as ve } from "react/jsx-runtime";
3
- import { useState as Z, useRef as Q, useEffect as re, useLayoutEffect as ie, useMemo as ye, createElement as ce, useSyncExternalStore as xe, startTransition as Ge, useCallback as Ee } from "react";
4
- import { transformStateFunc as Pe, isDeepEqual as Y, isFunction as X, getNestedValue as z, getDifferences as Ae, debounce as Ue } from "./utility.js";
3
+ import { useState as Z, useRef as Q, useEffect as oe, useLayoutEffect as ie, useMemo as ye, createElement as ce, useSyncExternalStore as xe, startTransition as Pe, useCallback as Ee } from "react";
4
+ import { transformStateFunc as Ge, isDeepEqual as Y, isFunction as X, getNestedValue as z, getDifferences as Ae, debounce as Ue } from "./utility.js";
5
5
  import { pushFunc as Te, updateFn as se, cutFunc as ge, ValidationWrapper as je, FormControlComponent as Fe } from "./Functions.jsx";
6
6
  import De from "superjson";
7
7
  import { v4 as pe } from "uuid";
@@ -9,7 +9,7 @@ import "zod";
9
9
  import { getGlobalStore as r, formRefStore as Oe } from "./store.js";
10
10
  import { useCogsConfig as Me } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as He } from "fast-json-patch";
12
- import We from "react-use-measure";
12
+ import Le from "react-use-measure";
13
13
  function Ne(e, i) {
14
14
  const m = r.getState().getInitialOptions, g = r.getState().setInitialStateOptions, T = m(e) || {};
15
15
  g(e, {
@@ -22,7 +22,7 @@ function Ce({
22
22
  options: i,
23
23
  initialOptionsPart: m
24
24
  }) {
25
- const g = ne(e) || {}, T = m[e] || {}, N = r.getState().setInitialStateOptions, A = { ...T, ...g };
25
+ const g = re(e) || {}, T = m[e] || {}, N = r.getState().setInitialStateOptions, A = { ...T, ...g };
26
26
  let v = !1;
27
27
  if (i)
28
28
  for (const a in i)
@@ -35,7 +35,7 @@ function ut(e, { formElements: i, validation: m }) {
35
35
  }
36
36
  const gt = (e, i) => {
37
37
  let m = e;
38
- const [g, T] = Pe(m);
38
+ const [g, T] = Ge(m);
39
39
  (Object.keys(T).length > 0 || i && Object.keys(i).length > 0) && Object.keys(T).forEach((v) => {
40
40
  T[v] = T[v] || {}, T[v].formElements = {
41
41
  ...i?.formElements,
@@ -43,7 +43,7 @@ const gt = (e, i) => {
43
43
  ...i?.validation,
44
44
  ...T[v].formElements || {}
45
45
  // State-specific overrides
46
- }, ne(v) || r.getState().setInitialStateOptions(v, T[v]);
46
+ }, re(v) || r.getState().setInitialStateOptions(v, T[v]);
47
47
  }), r.getState().setInitialStates(g), r.getState().setCreatedState(g);
48
48
  const N = (v, a) => {
49
49
  const [h] = Z(a?.componentId ?? pe());
@@ -52,7 +52,7 @@ const gt = (e, i) => {
52
52
  options: a,
53
53
  initialOptionsPart: T
54
54
  });
55
- const n = r.getState().cogsStateStore[v] || g[v], S = a?.modifyState ? a.modifyState(n) : n, [H, P] = Ze(
55
+ const n = r.getState().cogsStateStore[v] || g[v], S = a?.modifyState ? a.modifyState(n) : n, [L, G] = Ze(
56
56
  S,
57
57
  {
58
58
  stateKey: v,
@@ -68,7 +68,7 @@ const gt = (e, i) => {
68
68
  serverState: a?.serverState
69
69
  }
70
70
  );
71
- return P;
71
+ return G;
72
72
  };
73
73
  function A(v, a) {
74
74
  Ce({ stateKey: v, options: a, initialOptionsPart: T }), a.localStorage && qe(v, a), Ie(v);
@@ -77,9 +77,9 @@ const gt = (e, i) => {
77
77
  }, {
78
78
  setUpdaterState: fe,
79
79
  setState: te,
80
- getInitialOptions: ne,
80
+ getInitialOptions: re,
81
81
  getKeyState: Re,
82
- getValidationErrors: Le,
82
+ getValidationErrors: We,
83
83
  setStateLog: Be,
84
84
  updateInitialStateGlobal: _e,
85
85
  addValidationError: Ye,
@@ -193,12 +193,12 @@ function Ze(e, {
193
193
  dependencies: n,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [H, P] = Z({}), { sessionId: U } = Me();
196
+ const [L, G] = Z({}), { sessionId: U } = Me();
197
197
  let W = !i;
198
198
  const [I] = Z(i ?? pe()), l = r.getState().stateLog[I], le = Q(/* @__PURE__ */ new Set()), K = Q(v ?? pe()), V = Q(
199
199
  null
200
200
  );
201
- V.current = ne(I) ?? null, re(() => {
201
+ V.current = re(I) ?? null, oe(() => {
202
202
  if (h && h.stateKey === I && h.path?.[0]) {
203
203
  te(I, (o) => ({
204
204
  ...o,
@@ -210,7 +210,7 @@ function Ze(e, {
210
210
  userId: h.userId
211
211
  });
212
212
  }
213
- }, [h]), re(() => {
213
+ }, [h]), oe(() => {
214
214
  if (a) {
215
215
  Ne(I, {
216
216
  initialState: a
@@ -227,10 +227,10 @@ function Ze(e, {
227
227
  I,
228
228
  a,
229
229
  _,
230
- oe,
230
+ ae,
231
231
  K.current,
232
232
  U
233
- ), w && p && U && $e(_, I, t, U, Date.now()), Ie(I), (Array.isArray(A) ? A : [A || "component"]).includes("none") || P({});
233
+ ), w && p && U && $e(_, I, t, U, Date.now()), Ie(I), (Array.isArray(A) ? A : [A || "component"]).includes("none") || G({});
234
234
  }
235
235
  }, [
236
236
  a,
@@ -249,16 +249,16 @@ function Ze(e, {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return o.components.set(t, {
252
- forceUpdate: () => P({}),
252
+ forceUpdate: () => G({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
255
  depsFunction: N || void 0,
256
256
  reactiveType: A ?? ["component", "deps"]
257
- }), r.getState().stateComponents.set(I, o), P({}), () => {
257
+ }), r.getState().stateComponents.set(I, o), G({}), () => {
258
258
  o && (o.components.delete(t), o.components.size === 0 && r.getState().stateComponents.delete(I));
259
259
  };
260
260
  }, []);
261
- const oe = (t, o, s, c) => {
261
+ const ae = (t, o, s, c) => {
262
262
  if (Array.isArray(o)) {
263
263
  const u = `${I}-${o.join(".")}`;
264
264
  le.current.add(u);
@@ -279,11 +279,11 @@ function Ze(e, {
279
279
  if (y) {
280
280
  const x = M ? z(p, o.slice(0, -1)) : z(p, o);
281
281
  y.forEach(({ parentId: R, position: O, effect: j }) => {
282
- const G = document.querySelector(
282
+ const P = document.querySelector(
283
283
  `[data-parent-id="${R}"]`
284
284
  );
285
- if (G) {
286
- const B = Array.from(G.childNodes);
285
+ if (P) {
286
+ const B = Array.from(P.childNodes);
287
287
  if (B[O]) {
288
288
  const E = j ? new Function("state", `return (${j})(state)`)(x) : x;
289
289
  B[O].textContent = String(E);
@@ -298,7 +298,7 @@ function Ze(e, {
298
298
  const w = o.slice(0, o.length - 1);
299
299
  s.updateType === "cut" && V.current?.validation?.key && J(
300
300
  V.current?.validation?.key + "." + w.join(".")
301
- ), s.updateType === "insert" && V.current?.validation?.key && Le(
301
+ ), s.updateType === "insert" && V.current?.validation?.key && We(
302
302
  V.current?.validation?.key + "." + w.join(".")
303
303
  ).filter(([y, x]) => {
304
304
  let R = y?.split(".").length;
@@ -315,13 +315,13 @@ function Ze(e, {
315
315
  O
316
316
  ] of C.components.entries()) {
317
317
  let j = !1;
318
- const G = Array.isArray(O.reactiveType) ? O.reactiveType : [O.reactiveType || "component"];
319
- if (console.log("component", O), !G.includes("none")) {
320
- if (G.includes("all")) {
318
+ const P = Array.isArray(O.reactiveType) ? O.reactiveType : [O.reactiveType || "component"];
319
+ if (console.log("component", O), !P.includes("none")) {
320
+ if (P.includes("all")) {
321
321
  O.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (G.includes("component") && ((O.paths.has(x) || O.paths.has("")) && (j = !0), !j))
324
+ if (P.includes("component") && ((O.paths.has(x) || O.paths.has("")) && (j = !0), !j))
325
325
  for (const B of y) {
326
326
  let E = B;
327
327
  for (; ; ) {
@@ -331,17 +331,17 @@ function Ze(e, {
331
331
  }
332
332
  const b = E.lastIndexOf(".");
333
333
  if (b !== -1) {
334
- const F = E.substring(
334
+ const H = E.substring(
335
335
  0,
336
336
  b
337
337
  );
338
338
  if (!isNaN(
339
339
  Number(E.substring(b + 1))
340
- ) && O.paths.has(F)) {
340
+ ) && O.paths.has(H)) {
341
341
  j = !0;
342
342
  break;
343
343
  }
344
- E = F;
344
+ E = H;
345
345
  } else
346
346
  E = "";
347
347
  if (E === "")
@@ -349,7 +349,7 @@ function Ze(e, {
349
349
  }
350
350
  if (j) break;
351
351
  }
352
- if (!j && G.includes("deps") && O.depsFunction) {
352
+ if (!j && P.includes("deps") && O.depsFunction) {
353
353
  const B = O.depsFunction(p);
354
354
  let E = !1;
355
355
  typeof B == "boolean" ? B && (E = !0) : Y(O.deps, B) || (O.deps = B, E = !0), E && (j = !0);
@@ -392,8 +392,8 @@ function Ze(e, {
392
392
  }
393
393
  if (Be(I, (M) => {
394
394
  const x = [...M ?? [], q].reduce((R, O) => {
395
- const j = `${O.stateKey}:${JSON.stringify(O.path)}`, G = R.get(j);
396
- return G ? (G.timeStamp = Math.max(G.timeStamp, O.timeStamp), G.newValue = O.newValue, G.oldValue = G.oldValue ?? O.oldValue, G.updateType = O.updateType) : R.set(j, { ...O }), R;
395
+ const j = `${O.stateKey}:${JSON.stringify(O.path)}`, P = R.get(j);
396
+ return P ? (P.timeStamp = Math.max(P.timeStamp, O.timeStamp), P.newValue = O.newValue, P.oldValue = P.oldValue ?? O.oldValue, P.updateType = O.updateType) : R.set(j, { ...O }), R;
397
397
  }, /* @__PURE__ */ new Map());
398
398
  return Array.from(x.values());
399
399
  }), $e(
@@ -420,14 +420,14 @@ function Ze(e, {
420
420
  I,
421
421
  Se(
422
422
  I,
423
- oe,
423
+ ae,
424
424
  K.current,
425
425
  U
426
426
  )
427
427
  ), r.getState().cogsStateStore[I] || te(I, e), r.getState().initialStateGlobal[I] || _e(I, e));
428
428
  const d = ye(() => Se(
429
429
  I,
430
- oe,
430
+ ae,
431
431
  K.current,
432
432
  U
433
433
  ), [I, U]);
@@ -450,8 +450,8 @@ function Se(e, i, m, g) {
450
450
  n?.key && J(n?.key), h?.validationKey && J(h.validationKey);
451
451
  const S = r.getState().initialStateGlobal[e];
452
452
  r.getState().clearSelectedIndexesForState(e), T.clear(), N++;
453
- const H = a(S, []), P = ne(e), U = X(P?.localStorage?.key) ? P?.localStorage?.key(S) : P?.localStorage?.key, W = `${g}-${e}-${U}`;
454
- W && localStorage.removeItem(W), fe(e, H), te(e, S);
453
+ const L = a(S, []), G = re(e), U = X(G?.localStorage?.key) ? G?.localStorage?.key(S) : G?.localStorage?.key, W = `${g}-${e}-${U}`;
454
+ W && localStorage.removeItem(W), fe(e, L), te(e, S);
455
455
  const I = r.getState().stateComponents.get(e);
456
456
  return I && I.components.forEach((l) => {
457
457
  l.forceUpdate();
@@ -464,8 +464,8 @@ function Se(e, i, m, g) {
464
464
  i,
465
465
  m,
466
466
  g
467
- ), S = r.getState().initialStateGlobal[e], H = ne(e), P = X(H?.localStorage?.key) ? H?.localStorage?.key(S) : H?.localStorage?.key, U = `${g}-${e}-${P}`;
468
- return localStorage.getItem(U) && localStorage.removeItem(U), Ge(() => {
467
+ ), S = r.getState().initialStateGlobal[e], L = re(e), G = X(L?.localStorage?.key) ? L?.localStorage?.key(S) : L?.localStorage?.key, U = `${g}-${e}-${G}`;
468
+ return localStorage.getItem(U) && localStorage.removeItem(U), Pe(() => {
469
469
  _e(e, h), r.getState().initializeShadowState(e, h), fe(e, n), te(e, h);
470
470
  const W = r.getState().stateComponents.get(e);
471
471
  W && W.components.forEach((I) => {
@@ -484,13 +484,13 @@ function Se(e, i, m, g) {
484
484
  }
485
485
  };
486
486
  function a(h, n = [], S) {
487
- const H = n.map(String).join(".");
488
- T.get(H);
489
- const P = function() {
487
+ const L = n.map(String).join(".");
488
+ T.get(L);
489
+ const G = function() {
490
490
  return r().getNestedState(e, n);
491
491
  };
492
492
  Object.keys(v).forEach((I) => {
493
- P[I] = v[I];
493
+ G[I] = v[I];
494
494
  });
495
495
  const U = {
496
496
  apply(I, l, le) {
@@ -581,7 +581,7 @@ function Se(e, i, m, g) {
581
581
  };
582
582
  if (l === "removeStorage")
583
583
  return () => {
584
- const d = r.getState().initialStateGlobal[e], t = ne(e), o = X(t?.localStorage?.key) ? t?.localStorage?.key(d) : t?.localStorage?.key, s = `${g}-${e}-${o}`;
584
+ const d = r.getState().initialStateGlobal[e], t = re(e), o = X(t?.localStorage?.key) ? t?.localStorage?.key(d) : t?.localStorage?.key, s = `${g}-${e}-${o}`;
585
585
  s && localStorage.removeItem(s);
586
586
  };
587
587
  if (l === "showValidationErrors")
@@ -626,7 +626,7 @@ function Se(e, i, m, g) {
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
628
  }), [w, C] = Z("WAITING_FOR_ARRAY"), $ = Q(0), k = Q(f), [D, q] = Z(0);
629
- re(() => r.getState().subscribeToShadowState(e, () => {
629
+ oe(() => r.getState().subscribeToShadowState(e, () => {
630
630
  q((b) => b + 1);
631
631
  }), [e]);
632
632
  const M = r().getNestedState(
@@ -635,13 +635,13 @@ function Se(e, i, m, g) {
635
635
  ), y = M.length, { totalHeight: x, positions: R } = ye(() => {
636
636
  const E = r.getState().getShadowMetadata(e, n) || [];
637
637
  let b = 0;
638
- const F = [];
639
- for (let L = 0; L < y; L++) {
640
- F[L] = b;
641
- const ee = E[L]?.virtualizer?.itemHeight;
638
+ const H = [];
639
+ for (let F = 0; F < y; F++) {
640
+ H[F] = b;
641
+ const ee = E[F]?.virtualizer?.itemHeight;
642
642
  b += ee || o;
643
643
  }
644
- return { totalHeight: b, positions: F };
644
+ return { totalHeight: b, positions: H };
645
645
  }, [
646
646
  y,
647
647
  e,
@@ -649,13 +649,13 @@ function Se(e, i, m, g) {
649
649
  o,
650
650
  D
651
651
  ]), O = ye(() => {
652
- const E = Math.max(0, p.startIndex), b = Math.min(y, p.endIndex), F = Array.from(
652
+ const E = Math.max(0, p.startIndex), b = Math.min(y, p.endIndex), H = Array.from(
653
653
  { length: b - E },
654
- (ee, ae) => E + ae
655
- ), L = F.map((ee) => M[ee]);
656
- return a(L, n, {
654
+ (ee, ne) => E + ne
655
+ ), F = H.map((ee) => M[ee]);
656
+ return a(F, n, {
657
657
  ...S,
658
- validIndices: F
658
+ validIndices: H
659
659
  });
660
660
  }, [p.startIndex, p.endIndex, M, y]);
661
661
  ie(() => {
@@ -675,7 +675,7 @@ function Se(e, i, m, g) {
675
675
  }, [y, ...f]), ie(() => {
676
676
  const E = u.current;
677
677
  if (!E) return;
678
- let b;
678
+ let b, H;
679
679
  if (w === "WAITING_FOR_ARRAY" && y > 0 && c)
680
680
  console.log(
681
681
  "ACTION: WAITING_FOR_ARRAY -> GETTING_ARRAY_HEIGHTS"
@@ -698,8 +698,7 @@ function Se(e, i, m, g) {
698
698
  E.scrollTo({
699
699
  top: E.scrollHeight,
700
700
  behavior: F
701
- });
702
- const L = setTimeout(
701
+ }), H = setTimeout(
703
702
  () => {
704
703
  console.log(
705
704
  "ACTION: Scroll finished. -> LOCKED_AT_BOTTOM"
@@ -707,27 +706,26 @@ function Se(e, i, m, g) {
707
706
  },
708
707
  F === "smooth" ? 500 : 50
709
708
  );
710
- return () => clearTimeout(L);
711
709
  }
712
710
  return () => {
713
- b && clearInterval(b);
711
+ b && (console.log("CLEANUP: Clearing measurement loop timer."), clearInterval(b)), H && (console.log("CLEANUP: Clearing scroll-end timer."), clearTimeout(H));
714
712
  };
715
- }, [w, y, R]), re(() => {
713
+ }, [w, y, R]), oe(() => {
716
714
  const E = u.current;
717
715
  if (!E) return;
718
716
  const b = () => {
719
717
  !(E.scrollHeight - E.scrollTop - E.clientHeight < 1) && (w === "LOCKED_AT_BOTTOM" || w === "MOVING_TO_BOTTOM") && (console.log(
720
718
  "USER_ACTION: Scrolled up. -> IDLE_NOT_AT_BOTTOM"
721
719
  ), C("IDLE_NOT_AT_BOTTOM"));
722
- const { scrollTop: L, clientHeight: ee } = E;
723
- let ae = 0, de = y - 1;
724
- for (; ae <= de; ) {
725
- const he = Math.floor((ae + de) / 2);
726
- R[he] < L ? ae = he + 1 : de = he - 1;
720
+ const { scrollTop: F, clientHeight: ee } = E;
721
+ let ne = 0, de = y - 1;
722
+ for (; ne <= de; ) {
723
+ const he = Math.floor((ne + de) / 2);
724
+ R[he] < F ? ne = he + 1 : de = he - 1;
727
725
  }
728
726
  const we = Math.max(0, de - s);
729
727
  let ue = we;
730
- const Ve = L + ee;
728
+ const Ve = F + ee;
731
729
  for (; ue < y && R[ue] < Ve; )
732
730
  ue++;
733
731
  _({
@@ -746,7 +744,7 @@ function Se(e, i, m, g) {
746
744
  ), C("MOVING_TO_BOTTOM");
747
745
  },
748
746
  []
749
- ), G = Ee(
747
+ ), P = Ee(
750
748
  (E, b = "smooth") => {
751
749
  },
752
750
  [R]
@@ -771,7 +769,7 @@ function Se(e, i, m, g) {
771
769
  virtualState: O,
772
770
  virtualizerProps: B,
773
771
  scrollToBottom: j,
774
- scrollToIndex: G
772
+ scrollToIndex: P
775
773
  };
776
774
  };
777
775
  if (l === "stateSort")
@@ -1173,11 +1171,11 @@ function Se(e, i, m, g) {
1173
1171
  formOpts: t
1174
1172
  }
1175
1173
  );
1176
- const V = [...n, l], oe = r.getState().getNestedState(e, V);
1177
- return a(oe, V, S);
1174
+ const V = [...n, l], ae = r.getState().getNestedState(e, V);
1175
+ return a(ae, V, S);
1178
1176
  }
1179
- }, W = new Proxy(P, U);
1180
- return T.set(H, {
1177
+ }, W = new Proxy(G, U);
1178
+ return T.set(L, {
1181
1179
  proxy: W,
1182
1180
  stateVersion: N
1183
1181
  }), W;
@@ -1205,7 +1203,7 @@ function Qe({
1205
1203
  proxy: e
1206
1204
  }) {
1207
1205
  const i = Q(null), m = `${e._stateKey}-${e._path.join(".")}`;
1208
- return re(() => {
1206
+ return oe(() => {
1209
1207
  const g = i.current;
1210
1208
  if (!g || !g.parentElement) return;
1211
1209
  const T = g.parentElement, A = Array.from(T.childNodes).indexOf(g);
@@ -1226,14 +1224,14 @@ function Qe({
1226
1224
  "state",
1227
1225
  `return (${e._effect})(state)`
1228
1226
  )(n);
1229
- } catch (P) {
1230
- console.error("Error evaluating effect function during mount:", P), S = n;
1227
+ } catch (G) {
1228
+ console.error("Error evaluating effect function during mount:", G), S = n;
1231
1229
  }
1232
1230
  else
1233
1231
  S = n;
1234
1232
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1235
- const H = document.createTextNode(String(S));
1236
- g.replaceWith(H);
1233
+ const L = document.createTextNode(String(S));
1234
+ g.replaceWith(L);
1237
1235
  }, [e._stateKey, e._path.join("."), e._effect]), ce("span", {
1238
1236
  ref: i,
1239
1237
  style: { display: "none" },
@@ -1261,8 +1259,8 @@ function Ke({
1261
1259
  itemPath: m,
1262
1260
  children: g
1263
1261
  }) {
1264
- const [, T] = Z({}), [N, A] = We(), v = Q(null);
1265
- return re(() => {
1262
+ const [, T] = Z({}), [N, A] = Le(), v = Q(null);
1263
+ return oe(() => {
1266
1264
  A.height > 0 && A.height !== v.current && (v.current = A.height, r.getState().setShadowMetadata(e, m, {
1267
1265
  virtualizer: {
1268
1266
  itemHeight: A.height