cogsbox-state 0.5.156 → 0.5.158

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.
Files changed (29) hide show
  1. package/dist/CogsState.jsx +75 -72
  2. package/dist/CogsState.jsx.map +1 -1
  3. package/dist/node_modules/copy-anything/dist/index.js +27 -0
  4. package/dist/node_modules/copy-anything/dist/index.js.map +1 -0
  5. package/dist/node_modules/is-what/dist/index.js +18 -0
  6. package/dist/node_modules/is-what/dist/index.js.map +1 -0
  7. package/dist/node_modules/superjson/dist/accessDeep.js +92 -0
  8. package/dist/node_modules/superjson/dist/accessDeep.js.map +1 -0
  9. package/dist/node_modules/superjson/dist/class-registry.js +16 -0
  10. package/dist/node_modules/superjson/dist/class-registry.js.map +1 -0
  11. package/dist/node_modules/superjson/dist/custom-transformer-registry.js +19 -0
  12. package/dist/node_modules/superjson/dist/custom-transformer-registry.js.map +1 -0
  13. package/dist/node_modules/superjson/dist/double-indexed-kv.js +21 -0
  14. package/dist/node_modules/superjson/dist/double-indexed-kv.js.map +1 -0
  15. package/dist/node_modules/superjson/dist/index.js +75 -0
  16. package/dist/node_modules/superjson/dist/index.js.map +1 -0
  17. package/dist/node_modules/superjson/dist/is.js +24 -0
  18. package/dist/node_modules/superjson/dist/is.js.map +1 -0
  19. package/dist/node_modules/superjson/dist/pathstringifier.js +24 -0
  20. package/dist/node_modules/superjson/dist/pathstringifier.js.map +1 -0
  21. package/dist/node_modules/superjson/dist/plainer.js +102 -0
  22. package/dist/node_modules/superjson/dist/plainer.js.map +1 -0
  23. package/dist/node_modules/superjson/dist/registry.js +22 -0
  24. package/dist/node_modules/superjson/dist/registry.js.map +1 -0
  25. package/dist/node_modules/superjson/dist/transformer.js +140 -0
  26. package/dist/node_modules/superjson/dist/transformer.js.map +1 -0
  27. package/dist/node_modules/superjson/dist/util.js +39 -0
  28. package/dist/node_modules/superjson/dist/util.js.map +1 -0
  29. package/package.json +2 -1
@@ -1,11 +1,12 @@
1
1
  "use client";
2
2
  import { jsx as ue } from "react/jsx-runtime";
3
- import { useState as re, useRef as Y, useEffect as ae, useLayoutEffect as Ee, useMemo as _e, createElement as K, useSyncExternalStore as Ne, startTransition as we } from "react";
4
- import { transformStateFunc as $e, isFunction as W, isDeepEqual as F, getNestedValue as L, getDifferences as me, debounce as he } from "./utility.js";
5
- import { pushFunc as te, updateFn as B, cutFunc as H, ValidationWrapper as Ae, FormControlComponent as Ve } from "./Functions.jsx";
3
+ import { useState as re, useRef as Y, useEffect as ae, useLayoutEffect as _e, useMemo as Ne, createElement as K, useSyncExternalStore as we, startTransition as $e } from "react";
4
+ import { transformStateFunc as he, isFunction as W, isDeepEqual as F, getNestedValue as L, getDifferences as me, debounce as Ae } from "./utility.js";
5
+ import { pushFunc as te, updateFn as B, cutFunc as H, ValidationWrapper as Ve, FormControlComponent as Te } from "./Functions.jsx";
6
+ import ye from "./node_modules/superjson/dist/index.js";
6
7
  import "zod";
7
8
  import { getGlobalStore as r, formRefStore as ge } from "./store.js";
8
- import { useCogsConfig as Te } from "./CogsStateClient.jsx";
9
+ import { useCogsConfig as xe } from "./CogsStateClient.jsx";
9
10
  import oe from "./node_modules/uuid/dist/esm-browser/v4.js";
10
11
  function fe(e, i) {
11
12
  const v = r.getState().getInitialOptions, S = r.getState().setInitialStateOptions, u = v(e) || {};
@@ -27,12 +28,12 @@ function Se({
27
28
  !F(E[c], i[c]) && (m = !0, E[c] = i[c])) : (m = !0, E[c] = i[c]);
28
29
  m && N(e, E);
29
30
  }
30
- function Be(e, { formElements: i, validation: v }) {
31
+ function Ze(e, { formElements: i, validation: v }) {
31
32
  return { initialState: e, formElements: i, validation: v };
32
33
  }
33
- const Je = (e, i) => {
34
+ const He = (e, i) => {
34
35
  let v = e;
35
- const [S, u] = $e(v);
36
+ const [S, u] = he(v);
36
37
  (Object.keys(u).length > 0 || i && Object.keys(i).length > 0) && Object.keys(u).forEach((m) => {
37
38
  u[m] = u[m] || {}, u[m].formElements = {
38
39
  ...i?.formElements,
@@ -45,12 +46,12 @@ const Je = (e, i) => {
45
46
  const N = (m, c) => {
46
47
  const [f] = re(c?.componentId ?? oe());
47
48
  if (c && typeof c.initialState < "u" && W(c.initialState)) {
48
- const C = r.getState().cogsStateStore[m] || S[m], R = c.initialState(
49
+ const C = r.getState().cogsStateStore[m] || S[m], p = c.initialState(
49
50
  C
50
51
  );
51
52
  c = {
52
53
  ...c,
53
- initialState: R
54
+ initialState: p
54
55
  };
55
56
  }
56
57
  Se({
@@ -58,7 +59,7 @@ const Je = (e, i) => {
58
59
  options: c,
59
60
  initialOptionsPart: u
60
61
  });
61
- const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [j, V] = Oe(
62
+ const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [j, V] = Fe(
62
63
  y,
63
64
  {
64
65
  stateKey: m,
@@ -83,22 +84,14 @@ const Je = (e, i) => {
83
84
  setUpdaterState: X,
84
85
  setState: q,
85
86
  getInitialOptions: J,
86
- getKeyState: ye,
87
- getValidationErrors: xe,
88
- setStateLog: be,
87
+ getKeyState: ve,
88
+ getValidationErrors: be,
89
+ setStateLog: ke,
89
90
  updateInitialStateGlobal: ie,
90
- addValidationError: ke,
91
+ addValidationError: Ce,
91
92
  removeValidationError: D,
92
- setServerSyncActions: Ce
93
- } = r.getState(), ve = (e) => {
94
- if (!e) return null;
95
- try {
96
- const i = window.localStorage.getItem(e);
97
- return i ? JSON.parse(i) : null;
98
- } catch (i) {
99
- return console.error("Error loading from localStorage:", i), null;
100
- }
101
- }, Pe = (e, i, v, S) => {
93
+ setServerSyncActions: Pe
94
+ } = r.getState(), je = (e, i, v, S) => {
102
95
  v?.log && console.log(
103
96
  "saving to localstorage",
104
97
  i,
@@ -109,13 +102,23 @@ const Je = (e, i) => {
109
102
  if (u && S) {
110
103
  const N = {
111
104
  state: e,
105
+ // ← This is your state part
112
106
  lastUpdated: Date.now(),
107
+ // ← This is your lastUpdated
113
108
  lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
114
109
  baseServerState: r.getState().serverState[i]
115
110
  }, E = `${S}-${i}-${u}`;
116
- window.localStorage.setItem(E, JSON.stringify(N));
111
+ window.localStorage.setItem(E, ye.stringify(N));
112
+ }
113
+ }, Ie = (e) => {
114
+ if (!e) return null;
115
+ try {
116
+ const i = window.localStorage.getItem(e);
117
+ return i ? ye.parse(i) : null;
118
+ } catch (i) {
119
+ return console.error("Error loading from localStorage:", i), null;
117
120
  }
118
- }, je = (e, i, v, S, u, N) => {
121
+ }, Oe = (e, i, v, S, u, N) => {
119
122
  const E = {
120
123
  initialState: i,
121
124
  updaterState: Q(
@@ -136,7 +139,7 @@ const Je = (e, i) => {
136
139
  }), queueMicrotask(() => {
137
140
  v.forEach((S) => S());
138
141
  });
139
- }, Ze = (e, i) => {
142
+ }, Ye = (e, i) => {
140
143
  const v = r.getState().stateComponents.get(e);
141
144
  if (v) {
142
145
  const S = `${e}////${i}`, u = v.components.get(S);
@@ -145,7 +148,7 @@ const Je = (e, i) => {
145
148
  u && u.forceUpdate();
146
149
  }
147
150
  };
148
- function Oe(e, {
151
+ function Fe(e, {
149
152
  stateKey: i,
150
153
  serverSync: v,
151
154
  localStorage: S,
@@ -158,8 +161,8 @@ function Oe(e, {
158
161
  syncUpdate: t,
159
162
  dependencies: y
160
163
  } = {}) {
161
- const [j, V] = re({}), { sessionId: C } = Te();
162
- let R = !i;
164
+ const [j, V] = re({}), { sessionId: C } = xe();
165
+ let p = !i;
163
166
  const [g] = re(i ?? oe()), s = r.getState().stateLog[g], z = Y(/* @__PURE__ */ new Set()), G = Y(c ?? oe()), x = Y(null);
164
167
  x.current = J(g), ae(() => {
165
168
  if (t && t.stateKey === g && t.path?.[0]) {
@@ -181,7 +184,7 @@ function Oe(e, {
181
184
  const d = x.current;
182
185
  let a = null;
183
186
  const l = W(d?.localStorage?.key) ? d?.localStorage?.key(f) : d?.localStorage?.key;
184
- l && C && (a = ve(
187
+ l && C && (a = Ie(
185
188
  C + "-" + g + "-" + l
186
189
  ));
187
190
  const I = r.getState().initialStateGlobal[g];
@@ -192,7 +195,7 @@ function Oe(e, {
192
195
  F(I, f)
193
196
  ), I && !F(I, f) || !I) {
194
197
  let $ = f;
195
- a && a.lastUpdated > (a.lastSyncedWithServer || 0) && ($ = a.state, d?.localStorage?.onChange && d?.localStorage?.onChange($)), je(
198
+ a && a.lastUpdated > (a.lastSyncedWithServer || 0) && ($ = a.state, d?.localStorage?.onChange && d?.localStorage?.onChange($)), Oe(
196
199
  g,
197
200
  f,
198
201
  $,
@@ -202,8 +205,8 @@ function Oe(e, {
202
205
  ), se(g), (Array.isArray(m) ? m : [m || "component"]).includes("none") || V({});
203
206
  }
204
207
  }
205
- }, [f, ...y || []]), Ee(() => {
206
- R && fe(g, {
208
+ }, [f, ...y || []]), _e(() => {
209
+ p && fe(g, {
207
210
  serverSync: v,
208
211
  formElements: u,
209
212
  initialState: f,
@@ -230,9 +233,9 @@ function Oe(e, {
230
233
  z.current.add($);
231
234
  }
232
235
  q(g, ($) => {
233
- const w = W(d) ? d($) : d, p = `${g}-${a.join(".")}`;
234
- if (p) {
235
- let M = !1, h = r.getState().signalDomElements.get(p);
236
+ const w = W(d) ? d($) : d, R = `${g}-${a.join(".")}`;
237
+ if (R) {
238
+ let M = !1, h = r.getState().signalDomElements.get(R);
236
239
  if ((!h || h.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
237
240
  const P = a.slice(0, -1), O = L(w, P);
238
241
  if (Array.isArray(O)) {
@@ -263,16 +266,16 @@ function Oe(e, {
263
266
  const b = a.slice(0, a.length - 1);
264
267
  l.updateType === "cut" && x.current?.validationKey && D(
265
268
  x.current?.validationKey + "." + b.join(".")
266
- ), l.updateType === "insert" && x.current?.validationKey && xe(
269
+ ), l.updateType === "insert" && x.current?.validationKey && be(
267
270
  x.current?.validationKey + "." + b.join(".")
268
271
  ).filter(([h, P]) => {
269
272
  let O = h?.split(".").length;
270
273
  if (h == b.join(".") && O == b.length - 1) {
271
274
  let _ = h + "." + b;
272
- D(h), ke(_, P);
275
+ D(h), Ce(_, P);
273
276
  }
274
277
  });
275
- const Z = L($, a), Ie = L(w, a);
278
+ const Z = L($, a), Ee = L(w, a);
276
279
  l.updateType === "update" ? a.join(".") : [...a].slice(0, -1).join(".");
277
280
  const ce = r.getState().stateComponents.get(g);
278
281
  if (ce) {
@@ -332,15 +335,15 @@ function Oe(e, {
332
335
  updateType: l.updateType,
333
336
  status: "new",
334
337
  oldValue: Z,
335
- newValue: Ie
338
+ newValue: Ee
336
339
  };
337
- if (be(g, (M) => {
340
+ if (ke(g, (M) => {
338
341
  const P = [...M ?? [], le].reduce((O, _) => {
339
342
  const T = `${_.stateKey}:${JSON.stringify(_.path)}`, A = O.get(T);
340
343
  return A ? (A.timeStamp = Math.max(A.timeStamp, _.timeStamp), A.newValue = _.newValue, A.oldValue = A.oldValue ?? _.oldValue, A.updateType = _.updateType) : O.set(T, { ..._ }), O;
341
344
  }, /* @__PURE__ */ new Map());
342
345
  return Array.from(P.values());
343
- }), Pe(
346
+ }), je(
344
347
  w,
345
348
  g,
346
349
  x.current,
@@ -350,7 +353,7 @@ function Oe(e, {
350
353
  update: le
351
354
  }), x.current?.serverSync) {
352
355
  const M = r.getState().serverState[g], h = x.current?.serverSync;
353
- Ce(g, {
356
+ Pe(g, {
354
357
  syncKey: typeof h.syncKey == "string" ? h.syncKey : h.syncKey({ state: w }),
355
358
  rollBackState: M,
356
359
  actionTimeStamp: Date.now() + (h.debounce ?? 3e3),
@@ -369,13 +372,13 @@ function Oe(e, {
369
372
  C
370
373
  )
371
374
  ), r.getState().cogsStateStore[g] || q(g, e), r.getState().initialStateGlobal[g] || ie(g, e));
372
- const o = _e(() => Q(
375
+ const o = Ne(() => Q(
373
376
  g,
374
377
  n,
375
378
  G.current,
376
379
  C
377
380
  ), [g]);
378
- return [ye(g), o];
381
+ return [ve(g), o];
379
382
  }
380
383
  function Q(e, i, v, S) {
381
384
  const u = /* @__PURE__ */ new Map();
@@ -394,8 +397,8 @@ function Q(e, i, v, S) {
394
397
  t?.key && D(t?.key), f?.validationKey && D(f.validationKey);
395
398
  const y = r.getState().initialStateGlobal[e];
396
399
  r.getState().clearSelectedIndexesForState(e), u.clear(), N++;
397
- const j = c(y, []), V = J(e), C = W(V?.localStorage?.key) ? V?.localStorage?.key(y) : V?.localStorage?.key, R = `${S}-${e}-${C}`;
398
- R && localStorage.removeItem(R), X(e, j), q(e, y);
400
+ const j = c(y, []), V = J(e), C = W(V?.localStorage?.key) ? V?.localStorage?.key(y) : V?.localStorage?.key, p = `${S}-${e}-${C}`;
401
+ p && localStorage.removeItem(p), X(e, j), q(e, y);
399
402
  const g = r.getState().stateComponents.get(e);
400
403
  return g && g.components.forEach((s) => {
401
404
  s.forceUpdate();
@@ -409,7 +412,7 @@ function Q(e, i, v, S) {
409
412
  v,
410
413
  S
411
414
  );
412
- return we(() => {
415
+ return $e(() => {
413
416
  ie(e, f), X(e, t), q(e, f);
414
417
  const y = r.getState().stateComponents.get(e);
415
418
  y && y.components.forEach((j) => {
@@ -424,7 +427,7 @@ function Q(e, i, v, S) {
424
427
  _isLoading: r.getState().isLoadingGlobal[e],
425
428
  _isServerSynced: () => {
426
429
  const f = r.getState().serverState[e];
427
- return !!(f && F(f, ye(e)));
430
+ return !!(f && F(f, ve(e)));
428
431
  }
429
432
  };
430
433
  function c(f, t = [], y) {
@@ -553,7 +556,7 @@ function Q(e, i, v, S) {
553
556
  });
554
557
  };
555
558
  if (s === "$stateMap")
556
- return (n) => K(Fe, {
559
+ return (n) => K(pe, {
557
560
  proxy: {
558
561
  _stateKey: e,
559
562
  _path: t,
@@ -612,15 +615,15 @@ function Q(e, i, v, S) {
612
615
  );
613
616
  return b && (I = w), b;
614
617
  }
615
- const p = F(w, l);
616
- return p && (I = w), p;
618
+ const R = F(w, l);
619
+ return R && (I = w), R;
617
620
  }))
618
621
  E(t), te(i, l, t, e);
619
622
  else if (d && I) {
620
- const w = d(I), p = a.map(
623
+ const w = d(I), R = a.map(
621
624
  (b) => F(b, I) ? w : b
622
625
  );
623
- E(t), B(i, p, t);
626
+ E(t), B(i, R, t);
624
627
  }
625
628
  };
626
629
  if (s === "cut")
@@ -691,7 +694,7 @@ function Q(e, i, v, S) {
691
694
  return r.getState().getSyncInfo(n);
692
695
  }
693
696
  if (s == "getLocalStorage")
694
- return (n) => ve(S + "-" + e + "-" + n);
697
+ return (n) => Ie(S + "-" + e + "-" + n);
695
698
  if (s === "_selected") {
696
699
  const n = t.slice(0, -1), o = n.join("."), d = r.getState().getNestedState(e, n);
697
700
  return Array.isArray(d) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
@@ -731,8 +734,8 @@ function Q(e, i, v, S) {
731
734
  });
732
735
  const l = n.zodSchema.safeParse(d);
733
736
  return l.success ? !0 : (l.error.errors.forEach(($) => {
734
- const w = $.path, p = $.message, b = [n.key, ...w].join(".");
735
- o(b, p);
737
+ const w = $.path, R = $.message, b = [n.key, ...w].join(".");
738
+ o(b, R);
736
739
  }), se(e), !1);
737
740
  } catch (a) {
738
741
  return console.error("Zod schema validation failed", a), !1;
@@ -761,7 +764,7 @@ function Q(e, i, v, S) {
761
764
  children: n,
762
765
  hideMessage: o
763
766
  }) => /* @__PURE__ */ ue(
764
- Ae,
767
+ Ve,
765
768
  {
766
769
  formOpts: o ? { validation: { message: "" } } : void 0,
767
770
  path: t,
@@ -777,7 +780,7 @@ function Q(e, i, v, S) {
777
780
  if (s === "update")
778
781
  return (n, o) => {
779
782
  if (o?.debounce)
780
- he(() => {
783
+ Ae(() => {
781
784
  B(i, n, t, "");
782
785
  const d = r.getState().getNestedState(e, t);
783
786
  o?.afterUpdate && o.afterUpdate(d);
@@ -791,7 +794,7 @@ function Q(e, i, v, S) {
791
794
  };
792
795
  if (s === "formElement")
793
796
  return (n, o) => /* @__PURE__ */ ue(
794
- Ve,
797
+ Te,
795
798
  {
796
799
  setState: i,
797
800
  stateKey: e,
@@ -803,11 +806,11 @@ function Q(e, i, v, S) {
803
806
  const G = [...t, s], x = r.getState().getNestedState(e, G);
804
807
  return c(x, G, y);
805
808
  }
806
- }, R = new Proxy(V, C);
809
+ }, p = new Proxy(V, C);
807
810
  return u.set(j, {
808
- proxy: R,
811
+ proxy: p,
809
812
  stateVersion: N
810
- }), R;
813
+ }), p;
811
814
  }
812
815
  return c(
813
816
  r.getState().getNestedState(e, [])
@@ -816,7 +819,7 @@ function Q(e, i, v, S) {
816
819
  function ne(e) {
817
820
  return K(Re, { proxy: e });
818
821
  }
819
- function Fe({
822
+ function pe({
820
823
  proxy: e,
821
824
  rebuildStateShape: i
822
825
  }) {
@@ -867,8 +870,8 @@ function Re({
867
870
  "data-signal-id": v
868
871
  });
869
872
  }
870
- function He(e) {
871
- const i = Ne(
873
+ function Xe(e) {
874
+ const i = we(
872
875
  (v) => {
873
876
  const S = r.getState().stateComponents.get(e._stateKey) || {
874
877
  components: /* @__PURE__ */ new Map()
@@ -884,10 +887,10 @@ function He(e) {
884
887
  }
885
888
  export {
886
889
  ne as $cogsSignal,
887
- He as $cogsSignalStore,
888
- Be as addStateOptions,
889
- Je as createCogsState,
890
- Ze as notifyComponent,
891
- Oe as useCogsStateFn
890
+ Xe as $cogsSignalStore,
891
+ Ze as addStateOptions,
892
+ He as createCogsState,
893
+ Ye as notifyComponent,
894
+ Fe as useCogsStateFn
892
895
  };
893
896
  //# sourceMappingURL=CogsState.jsx.map