cogsbox-state 0.5.388 → 0.5.389

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.
@@ -78,7 +78,7 @@ const vt = (e, i) => {
78
78
  setUpdaterState: he,
79
79
  setState: ne,
80
80
  getInitialOptions: re,
81
- getKeyState: Re,
81
+ getKeyState: Le,
82
82
  getValidationErrors: Ze,
83
83
  setStateLog: Xe,
84
84
  updateInitialStateGlobal: Ne,
@@ -128,7 +128,7 @@ const vt = (e, i) => {
128
128
  return ne(e, N.state), ve(e), !0;
129
129
  }
130
130
  return !1;
131
- }, Le = (e, i, m, g, I, N) => {
131
+ }, Re = (e, i, m, g, I, N) => {
132
132
  const p = {
133
133
  initialState: i,
134
134
  updaterState: Te(
@@ -195,10 +195,10 @@ function nt(e, {
195
195
  } = {}) {
196
196
  const [W, U] = K({}), { sessionId: j } = Ge();
197
197
  let z = !i;
198
- const [h] = K(i ?? Ae()), l = o.getState().stateLog[h], de = q(/* @__PURE__ */ new Set()), te = q(v ?? Ae()), R = q(
198
+ const [h] = K(i ?? Ae()), l = o.getState().stateLog[h], de = q(/* @__PURE__ */ new Set()), te = q(v ?? Ae()), L = q(
199
199
  null
200
200
  );
201
- R.current = re(h) ?? null, oe(() => {
201
+ L.current = re(h) ?? null, oe(() => {
202
202
  if (T && T.stateKey === h && T.path?.[0]) {
203
203
  ne(h, (r) => ({
204
204
  ...r,
@@ -215,15 +215,15 @@ function nt(e, {
215
215
  Me(h, {
216
216
  initialState: s
217
217
  });
218
- const t = R.current, a = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, c = o.getState().initialStateGlobal[h];
218
+ const t = L.current, a = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, c = o.getState().initialStateGlobal[h];
219
219
  if (!(c && !J(c, s) || !c) && !a)
220
220
  return;
221
221
  let u = null;
222
222
  const E = ee(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
223
223
  E && j && (u = Ie(`${j}-${h}-${E}`));
224
224
  let y = s, w = !1;
225
- const C = a ? Date.now() : 0, A = u?.lastUpdated || 0, M = u?.lastSyncedWithServer || 0;
226
- a && C > A ? (y = t.serverState.data, w = !0) : u && A > M && (y = u.state, t?.localStorage?.onChange && t?.localStorage?.onChange(y)), o.getState().initializeShadowState(h, s), Le(
225
+ const C = a ? Date.now() : 0, A = u?.lastUpdated || 0, b = u?.lastSyncedWithServer || 0;
226
+ a && C > A ? (y = t.serverState.data, w = !0) : u && A > b && (y = u.state, t?.localStorage?.onChange && t?.localStorage?.onChange(y)), o.getState().initializeShadowState(h, s), Re(
227
227
  h,
228
228
  s,
229
229
  y,
@@ -243,7 +243,7 @@ function nt(e, {
243
243
  formElements: I,
244
244
  initialState: s,
245
245
  localStorage: g,
246
- middleware: R.current?.middleware
246
+ middleware: L.current?.middleware
247
247
  });
248
248
  const t = `${h}////${te.current}`, r = o.getState().stateComponents.get(h) || {
249
249
  components: /* @__PURE__ */ new Map()
@@ -267,17 +267,17 @@ function nt(e, {
267
267
  ne(h, (u) => {
268
268
  const E = ee(t) ? t(u) : t, y = `${h}-${r.join(".")}`;
269
269
  if (y) {
270
- let x = !1, $ = f.signalDomElements.get(y);
270
+ let M = !1, $ = f.signalDomElements.get(y);
271
271
  if ((!$ || $.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
272
- const L = r.slice(0, -1), V = Z(E, L);
272
+ const R = r.slice(0, -1), V = Z(E, R);
273
273
  if (Array.isArray(V)) {
274
- x = !0;
275
- const O = `${h}-${L.join(".")}`;
274
+ M = !0;
275
+ const O = `${h}-${R.join(".")}`;
276
276
  $ = f.signalDomElements.get(O);
277
277
  }
278
278
  }
279
279
  if ($) {
280
- const L = x ? Z(E, r.slice(0, -1)) : Z(E, r);
280
+ const R = M ? Z(E, r.slice(0, -1)) : Z(E, r);
281
281
  $.forEach(({ parentId: V, position: O, effect: _ }) => {
282
282
  const D = document.querySelector(
283
283
  `[data-parent-id="${V}"]`
@@ -285,31 +285,31 @@ function nt(e, {
285
285
  if (D) {
286
286
  const G = Array.from(D.childNodes);
287
287
  if (G[O]) {
288
- const B = _ ? new Function("state", `return (${_})(state)`)(L) : L;
288
+ const B = _ ? new Function("state", `return (${_})(state)`)(R) : R;
289
289
  G[O].textContent = String(B);
290
290
  }
291
291
  }
292
292
  });
293
293
  }
294
294
  }
295
- console.log("shadowState", f.shadowStateStore), a.updateType === "update" && (c || R.current?.validation?.key) && r && Q(
296
- (c || R.current?.validation?.key) + "." + r.join(".")
295
+ console.log("shadowState", f.shadowStateStore), a.updateType === "update" && (c || L.current?.validation?.key) && r && Q(
296
+ (c || L.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
298
  const w = r.slice(0, r.length - 1);
299
- a.updateType === "cut" && R.current?.validation?.key && Q(
300
- R.current?.validation?.key + "." + w.join(".")
301
- ), a.updateType === "insert" && R.current?.validation?.key && Ze(
302
- R.current?.validation?.key + "." + w.join(".")
303
- ).filter(([$, L]) => {
299
+ a.updateType === "cut" && L.current?.validation?.key && Q(
300
+ L.current?.validation?.key + "." + w.join(".")
301
+ ), a.updateType === "insert" && L.current?.validation?.key && Ze(
302
+ L.current?.validation?.key + "." + w.join(".")
303
+ ).filter(([$, R]) => {
304
304
  let V = $?.split(".").length;
305
305
  if ($ == w.join(".") && V == w.length - 1) {
306
306
  let O = $ + "." + w;
307
- Q($), Qe(O, L);
307
+ Q($), Qe(O, R);
308
308
  }
309
309
  });
310
310
  const C = f.stateComponents.get(h);
311
311
  if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", C), C) {
312
- const x = _e(u, E), $ = new Set(x), L = a.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
312
+ const M = _e(u, E), $ = new Set(M), R = a.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  V,
315
315
  O
@@ -321,7 +321,7 @@ function nt(e, {
321
321
  O.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (D.includes("component") && ((O.paths.has(L) || O.paths.has("")) && (_ = !0), !_))
324
+ if (D.includes("component") && ((O.paths.has(R) || O.paths.has("")) && (_ = !0), !_))
325
325
  for (const G of $) {
326
326
  let B = G;
327
327
  for (; ; ) {
@@ -359,11 +359,11 @@ function nt(e, {
359
359
  }
360
360
  }
361
361
  const A = Date.now();
362
- r = r.map((x, $) => {
363
- const L = r.slice(0, -1), V = Z(E, L);
364
- return $ === r.length - 1 && ["insert", "cut"].includes(a.updateType) ? (V.length - 1).toString() : x;
362
+ r = r.map((M, $) => {
363
+ const R = r.slice(0, -1), V = Z(E, R);
364
+ return $ === r.length - 1 && ["insert", "cut"].includes(a.updateType) ? (V.length - 1).toString() : M;
365
365
  });
366
- const { oldValue: M, newValue: F } = tt(
366
+ const { oldValue: b, newValue: F } = tt(
367
367
  a.updateType,
368
368
  u,
369
369
  E,
@@ -374,7 +374,7 @@ function nt(e, {
374
374
  path: r,
375
375
  updateType: a.updateType,
376
376
  status: "new",
377
- oldValue: M,
377
+ oldValue: b,
378
378
  newValue: F
379
379
  };
380
380
  switch (a.updateType) {
@@ -382,33 +382,33 @@ function nt(e, {
382
382
  f.updateShadowAtPath(h, r, E);
383
383
  break;
384
384
  case "insert":
385
- const x = r.slice(0, -1);
386
- f.insertShadowArrayElement(h, x, F);
385
+ const M = r.slice(0, -1);
386
+ f.insertShadowArrayElement(h, M, F);
387
387
  break;
388
388
  case "cut":
389
- const $ = r.slice(0, -1), L = parseInt(r[r.length - 1]);
390
- f.removeShadowArrayElement(h, $, L);
389
+ const $ = r.slice(0, -1), R = parseInt(r[r.length - 1]);
390
+ f.removeShadowArrayElement(h, $, R);
391
391
  break;
392
392
  }
393
- if (Xe(h, (x) => {
394
- const L = [...x ?? [], Y].reduce((V, O) => {
393
+ if (Xe(h, (M) => {
394
+ const R = [...M ?? [], Y].reduce((V, O) => {
395
395
  const _ = `${O.stateKey}:${JSON.stringify(O.path)}`, D = V.get(_);
396
396
  return D ? (D.timeStamp = Math.max(D.timeStamp, O.timeStamp), D.newValue = O.newValue, D.oldValue = D.oldValue ?? O.oldValue, D.updateType = O.updateType) : V.set(_, { ...O }), V;
397
397
  }, /* @__PURE__ */ new Map());
398
- return Array.from(L.values());
398
+ return Array.from(R.values());
399
399
  }), Pe(
400
400
  E,
401
401
  h,
402
- R.current,
402
+ L.current,
403
403
  j
404
- ), R.current?.middleware && R.current.middleware({
404
+ ), L.current?.middleware && L.current.middleware({
405
405
  updateLog: l,
406
406
  update: Y
407
- }), R.current?.serverSync) {
408
- const x = f.serverState[h], $ = R.current?.serverSync;
407
+ }), L.current?.serverSync) {
408
+ const M = f.serverState[h], $ = L.current?.serverSync;
409
409
  Ke(h, {
410
410
  syncKey: typeof $.syncKey == "string" ? $.syncKey : $.syncKey({ state: E }),
411
- rollBackState: x,
411
+ rollBackState: M,
412
412
  actionTimeStamp: Date.now() + ($.debounce ?? 3e3),
413
413
  status: "waiting"
414
414
  });
@@ -431,7 +431,7 @@ function nt(e, {
431
431
  te.current,
432
432
  j
433
433
  ), [h, j]);
434
- return [Re(h), d];
434
+ return [Le(h), d];
435
435
  }
436
436
  function Te(e, i, m, g) {
437
437
  const I = /* @__PURE__ */ new Map();
@@ -480,7 +480,7 @@ function Te(e, i, m, g) {
480
480
  _isLoading: o.getState().isLoadingGlobal[e],
481
481
  _isServerSynced: () => {
482
482
  const T = o.getState().serverState[e];
483
- return !!(T && J(T, Re(e)));
483
+ return !!(T && J(T, Le(e)));
484
484
  }
485
485
  };
486
486
  function s(T, n = [], S) {
@@ -625,23 +625,23 @@ function Te(e, i, m, g) {
625
625
  } = t, u = q(!1), E = q(null), [y, w] = K({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), [C, A] = K("IDLE_AT_TOP"), M = q(!1), F = q(0), Y = q(f), x = q(0), [$, L] = K(0), V = q(null);
628
+ }), [C, A] = K("IDLE_AT_TOP"), b = q(!1), F = q(0), Y = q(f), M = q(0), [$, R] = K(0), V = q(null);
629
629
  oe(() => o.getState().subscribeToShadowState(e, () => {
630
- L((P) => P + 1);
630
+ R((x) => x + 1);
631
631
  }), [e]);
632
632
  const O = o().getNestedState(
633
633
  e,
634
634
  n
635
635
  ), _ = O.length, { totalHeight: D, positions: G } = we(() => {
636
636
  const k = o.getState().getShadowMetadata(e, n) || [];
637
- let P = 0;
637
+ let x = 0;
638
638
  const H = [];
639
- for (let b = 0; b < _; b++) {
640
- H[b] = P;
641
- const X = k[b]?.virtualizer?.itemHeight;
642
- P += X || r;
639
+ for (let P = 0; P < _; P++) {
640
+ H[P] = x;
641
+ const X = k[P]?.virtualizer?.itemHeight;
642
+ x += X || r;
643
643
  }
644
- return { totalHeight: P, positions: H };
644
+ return { totalHeight: x, positions: H };
645
645
  }, [
646
646
  _,
647
647
  e,
@@ -649,22 +649,26 @@ function Te(e, i, m, g) {
649
649
  r,
650
650
  $
651
651
  ]), B = we(() => {
652
- const k = Math.max(0, y.startIndex), P = Math.min(_, y.endIndex), H = Array.from(
653
- { length: P - k },
652
+ const k = Math.max(0, y.startIndex), x = Math.min(_, y.endIndex), H = Array.from(
653
+ { length: x - k },
654
654
  (X, ge) => k + ge
655
- ), b = H.map((X) => O[X]);
656
- return s(b, n, {
655
+ ), P = H.map((X) => O[X]);
656
+ return s(P, n, {
657
657
  ...S,
658
658
  validIndices: H
659
659
  });
660
660
  }, [y.startIndex, y.endIndex, O, _]);
661
661
  ce(() => {
662
- const k = E.current, P = _ > F.current;
663
- if (P && V.current && k) {
664
- const { top: H, height: b } = V.current;
665
- k.scrollTop = H + (k.scrollHeight - b), V.current = null, console.log(
662
+ const k = E.current;
663
+ if (!k) return;
664
+ const x = _ > F.current;
665
+ if (x && V.current) {
666
+ const { top: H, height: P } = V.current;
667
+ b.current = !0, k.scrollTop = H + (k.scrollHeight - P), console.log(
666
668
  `ANCHOR RESTORED to scrollTop: ${k.scrollTop}`
667
- );
669
+ ), setTimeout(() => {
670
+ b.current = !1;
671
+ }, 100), V.current = null;
668
672
  } else {
669
673
  if (!J(
670
674
  f,
@@ -673,7 +677,7 @@ function Te(e, i, m, g) {
673
677
  console.log("TRANSITION: Deps changed -> IDLE_AT_TOP"), A("IDLE_AT_TOP");
674
678
  return;
675
679
  }
676
- P && C === "LOCKED_AT_BOTTOM" && c && (console.log(
680
+ x && C === "LOCKED_AT_BOTTOM" && c && (console.log(
677
681
  "TRANSITION: New items arrived while locked -> GETTING_HEIGHTS"
678
682
  ), A("GETTING_HEIGHTS"));
679
683
  }
@@ -681,7 +685,7 @@ function Te(e, i, m, g) {
681
685
  }, [_, ...f]), ce(() => {
682
686
  const k = E.current;
683
687
  if (!k) return;
684
- let P;
688
+ let x;
685
689
  if (C === "IDLE_AT_TOP" && c && _ > 0)
686
690
  console.log(
687
691
  "ACTION (IDLE_AT_TOP): Data has arrived -> GETTING_HEIGHTS"
@@ -692,63 +696,63 @@ function Te(e, i, m, g) {
692
696
  ), w({
693
697
  startIndex: Math.max(0, _ - 10 - a),
694
698
  endIndex: _
695
- }), P = setInterval(() => {
699
+ }), x = setInterval(() => {
696
700
  const H = _ - 1;
697
- ((o.getState().getShadowMetadata(e, n) || [])[H]?.virtualizer?.itemHeight || 0) > 0 && (clearInterval(P), u.current || (console.log(
701
+ ((o.getState().getShadowMetadata(e, n) || [])[H]?.virtualizer?.itemHeight || 0) > 0 && (clearInterval(x), u.current || (console.log(
698
702
  "ACTION (GETTING_HEIGHTS): Measurement success -> SCROLLING_TO_BOTTOM"
699
703
  ), A("SCROLLING_TO_BOTTOM")));
700
704
  }, 100);
701
705
  else if (C === "SCROLLING_TO_BOTTOM") {
702
706
  console.log(
703
707
  "ACTION (SCROLLING_TO_BOTTOM): Executing scroll."
704
- ), M.current = !0;
708
+ ), b.current = !0;
705
709
  const H = F.current === 0 ? "auto" : "smooth";
706
710
  k.scrollTo({
707
711
  top: k.scrollHeight,
708
712
  behavior: H
709
713
  });
710
- const b = setTimeout(
714
+ const P = setTimeout(
711
715
  () => {
712
716
  console.log(
713
717
  "ACTION (SCROLLING_TO_BOTTOM): Scroll finished -> LOCKED_AT_BOTTOM"
714
- ), M.current = !1, u.current = !1, A("LOCKED_AT_BOTTOM");
718
+ ), b.current = !1, u.current = !1, A("LOCKED_AT_BOTTOM");
715
719
  },
716
720
  H === "smooth" ? 500 : 50
717
721
  );
718
- return () => clearTimeout(b);
722
+ return () => clearTimeout(P);
719
723
  }
720
724
  return () => {
721
- P && clearInterval(P);
725
+ x && clearInterval(x);
722
726
  };
723
727
  }, [C, _, G]), oe(() => {
724
728
  const k = E.current;
725
729
  if (!k) return;
726
- const P = r, H = () => {
727
- if (M.current)
730
+ const x = r, H = () => {
731
+ if (b.current)
728
732
  return;
729
- const { scrollTop: b, scrollHeight: X, clientHeight: ge } = k;
730
- if (X - b - ge < 10 ? C !== "LOCKED_AT_BOTTOM" && (A("LOCKED_AT_BOTTOM"), V.current = null) : C !== "IDLE_NOT_AT_BOTTOM" && (A("IDLE_NOT_AT_BOTTOM"), V.current = {
731
- top: b,
733
+ const { scrollTop: P, scrollHeight: X, clientHeight: ge } = k;
734
+ if (X - P - ge < 10 ? (C !== "LOCKED_AT_BOTTOM" && A("LOCKED_AT_BOTTOM"), V.current = null) : (C !== "IDLE_NOT_AT_BOTTOM" && A("IDLE_NOT_AT_BOTTOM"), V.current = {
735
+ top: P,
732
736
  height: X
733
- }, console.log(`ANCHOR SET at scrollTop: ${b}`)), Math.abs(b - x.current) < P)
737
+ }), Math.abs(P - M.current) < x)
734
738
  return;
735
739
  console.log(
736
- `Threshold passed at ${b}px. Recalculating range...`
740
+ `Threshold passed at ${P}px. Recalculating range...`
737
741
  );
738
742
  let ye = _ - 1, pe = 0, Ce = 0;
739
743
  for (; pe <= ye; ) {
740
744
  const Se = Math.floor((pe + ye) / 2);
741
- G[Se] < b ? (Ce = Se, pe = Se + 1) : ye = Se - 1;
745
+ G[Se] < P ? (Ce = Se, pe = Se + 1) : ye = Se - 1;
742
746
  }
743
747
  const $e = Math.max(0, Ce - a);
744
748
  let fe = $e;
745
- const Ue = b + ge;
749
+ const Ue = P + ge;
746
750
  for (; fe < _ && G[fe] < Ue; )
747
751
  fe++;
748
752
  w({
749
753
  startIndex: $e,
750
754
  endIndex: Math.min(_, fe + a)
751
- }), x.current = b;
755
+ }), M.current = P;
752
756
  };
753
757
  return k.addEventListener("scroll", H, {
754
758
  passive: !0
@@ -759,10 +763,10 @@ function Te(e, i, m, g) {
759
763
  "USER ACTION: Clicked scroll button -> SCROLLING_TO_BOTTOM"
760
764
  ), A("SCROLLING_TO_BOTTOM");
761
765
  }, []), ue = ke(
762
- (k, P = "smooth") => {
766
+ (k, x = "smooth") => {
763
767
  E.current && G[k] !== void 0 && (A("IDLE_NOT_AT_BOTTOM"), E.current.scrollTo({
764
768
  top: G[k],
765
- behavior: P
769
+ behavior: x
766
770
  }));
767
771
  },
768
772
  [G]
@@ -823,15 +827,15 @@ function Te(e, i, m, g) {
823
827
  register: () => {
824
828
  const [, C] = K({}), A = `${m}-${n.join(".")}-${c}`;
825
829
  ce(() => {
826
- const M = `${e}////${A}`, F = o.getState().stateComponents.get(e) || {
830
+ const b = `${e}////${A}`, F = o.getState().stateComponents.get(e) || {
827
831
  components: /* @__PURE__ */ new Map()
828
832
  };
829
- return F.components.set(M, {
833
+ return F.components.set(b, {
830
834
  forceUpdate: () => C({}),
831
835
  paths: /* @__PURE__ */ new Set([E.join(".")])
832
836
  }), o.getState().stateComponents.set(e, F), () => {
833
837
  const Y = o.getState().stateComponents.get(e);
834
- Y && Y.components.delete(M);
838
+ Y && Y.components.delete(b);
835
839
  };
836
840
  }, [e, A]);
837
841
  },
@@ -1039,7 +1043,7 @@ function Te(e, i, m, g) {
1039
1043
  if (l === "applyJsonPatch")
1040
1044
  return (d) => {
1041
1045
  const t = o.getState().cogsStateStore[e], a = Je(t, d).newDocument;
1042
- Le(
1046
+ Re(
1043
1047
  e,
1044
1048
  o.getState().initialStateGlobal[e],
1045
1049
  a,
@@ -1067,22 +1071,22 @@ function Te(e, i, m, g) {
1067
1071
  w = !0;
1068
1072
  break;
1069
1073
  }
1070
- let M = A.lastIndexOf(".");
1071
- for (; M !== -1; ) {
1072
- const F = A.substring(0, M);
1074
+ let b = A.lastIndexOf(".");
1075
+ for (; b !== -1; ) {
1076
+ const F = A.substring(0, b);
1073
1077
  if (y.paths.has(F)) {
1074
1078
  w = !0;
1075
1079
  break;
1076
1080
  }
1077
1081
  const Y = A.substring(
1078
- M + 1
1082
+ b + 1
1079
1083
  );
1080
1084
  if (!isNaN(Number(Y))) {
1081
- const x = F.lastIndexOf(".");
1082
- if (x !== -1) {
1085
+ const M = F.lastIndexOf(".");
1086
+ if (M !== -1) {
1083
1087
  const $ = F.substring(
1084
1088
  0,
1085
- x
1089
+ M
1086
1090
  );
1087
1091
  if (y.paths.has($)) {
1088
1092
  w = !0;
@@ -1090,14 +1094,14 @@ function Te(e, i, m, g) {
1090
1094
  }
1091
1095
  }
1092
1096
  }
1093
- M = F.lastIndexOf(".");
1097
+ b = F.lastIndexOf(".");
1094
1098
  }
1095
1099
  if (w) break;
1096
1100
  }
1097
1101
  if (!w && C.includes("deps") && y.depsFunction) {
1098
1102
  const A = y.depsFunction(a);
1099
- let M = !1;
1100
- typeof A == "boolean" ? A && (M = !0) : J(y.deps, A) || (y.deps = A, M = !0), M && (w = !0);
1103
+ let b = !1;
1104
+ typeof A == "boolean" ? A && (b = !0) : J(y.deps, A) || (y.deps = A, b = !0), b && (w = !0);
1101
1105
  }
1102
1106
  w && y.forceUpdate();
1103
1107
  }
@@ -1189,8 +1193,8 @@ function Te(e, i, m, g) {
1189
1193
  formOpts: t
1190
1194
  }
1191
1195
  );
1192
- const R = [...n, l], ae = o.getState().getNestedState(e, R);
1193
- return s(ae, R, S);
1196
+ const L = [...n, l], ae = o.getState().getNestedState(e, L);
1197
+ return s(ae, L, S);
1194
1198
  }
1195
1199
  }, z = new Proxy(U, j);
1196
1200
  return I.set(W, {