cogsbox-state 0.5.170 → 0.5.174

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.
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Cogsbox State: A Practical Guide
2
2
 
3
- > **⚠️ WARNING**: This README is AI-generated based on the current implementation of Cogsbox State. The library is under active development and APIs are subject to change.
3
+ > **🚨 DANGER: DO NOT USE - UNSTABLE & EXPERIMENTAL 🚨**
4
+ >
5
+ > This library is in extremely early development and constantly changing.
6
+ > Everything will break. Nothing works properly.
7
+ > **DO NOT USE IN ANY PROJECT.**
4
8
 
5
9
  ## Getting Started
6
10
 
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as me } from "react/jsx-runtime";
3
3
  import { useState as de, useRef as Q, useEffect as ue, useLayoutEffect as we, useMemo as Ne, createElement as te, useSyncExternalStore as ke, startTransition as Ve } from "react";
4
- import { transformStateFunc as Ae, isDeepEqual as D, isFunction as L, getNestedValue as M, getDifferences as _e, debounce as Te } from "./utility.js";
4
+ import { transformStateFunc as Te, isDeepEqual as M, isFunction as L, getNestedValue as U, getDifferences as _e, debounce as Ae } from "./utility.js";
5
5
  import { pushFunc as ce, updateFn as Z, cutFunc as X, ValidationWrapper as xe, FormControlComponent as Ce } from "./Functions.jsx";
6
6
  import be from "./node_modules/superjson/dist/index.js";
7
7
  import "zod";
@@ -25,7 +25,7 @@ function Ie({
25
25
  if (s)
26
26
  for (const l in s)
27
27
  E.hasOwnProperty(l) ? (l == "localStorage" && s[l] && E[l].key !== s[l]?.key && (f = !0, E[l] = s[l]), l == "initialState" && s[l] && E[l] !== s[l] && // Different references
28
- !D(E[l], s[l]) && (f = !0, E[l] = s[l])) : (f = !0, E[l] = s[l]);
28
+ !M(E[l], s[l]) && (f = !0, E[l] = s[l])) : (f = !0, E[l] = s[l]);
29
29
  f && I(e, E);
30
30
  }
31
31
  function Xe(e, { formElements: s, validation: m }) {
@@ -33,7 +33,7 @@ function Xe(e, { formElements: s, validation: m }) {
33
33
  }
34
34
  const Qe = (e, s) => {
35
35
  let m = e;
36
- const [d, u] = Ae(m);
36
+ const [d, u] = Te(m);
37
37
  (Object.keys(u).length > 0 || s && Object.keys(s).length > 0) && Object.keys(u).forEach((f) => {
38
38
  u[f] = u[f] || {}, u[f].formElements = {
39
39
  ...s?.formElements,
@@ -50,7 +50,7 @@ const Qe = (e, s) => {
50
50
  options: l,
51
51
  initialOptionsPart: u
52
52
  });
53
- const t = a.getState().cogsStateStore[f] || d[f], v = l?.modifyState ? l.modifyState(t) : t, [A, b] = De(
53
+ const t = a.getState().cogsStateStore[f] || d[f], v = l?.modifyState ? l.modifyState(t) : t, [T, b] = De(
54
54
  v,
55
55
  {
56
56
  stateKey: f,
@@ -69,7 +69,7 @@ const Qe = (e, s) => {
69
69
  return b;
70
70
  };
71
71
  function E(f, l) {
72
- Ie({ stateKey: f, options: l, initialOptionsPart: u }), l.localStorage && Re(f, l), re(f);
72
+ Ie({ stateKey: f, options: l, initialOptionsPart: u }), l.localStorage && pe(f, l), re(f);
73
73
  }
74
74
  return { useCogsState: I, setCogsOptions: E };
75
75
  }, {
@@ -116,7 +116,7 @@ const Qe = (e, s) => {
116
116
  } catch (s) {
117
117
  return console.error("Error loading from localStorage:", s), null;
118
118
  }
119
- }, Re = (e, s) => {
119
+ }, pe = (e, s) => {
120
120
  const m = a.getState().cogsStateStore[e], { sessionId: d } = $e(), u = L(s?.localStorage?.key) ? s.localStorage.key(m) : s?.localStorage?.key;
121
121
  if (u && d) {
122
122
  const I = ne(
@@ -126,7 +126,7 @@ const Qe = (e, s) => {
126
126
  return q(e, I.state), re(e), !0;
127
127
  }
128
128
  return !1;
129
- }, pe = (e, s, m, d, u, I) => {
129
+ }, Re = (e, s, m, d, u, I) => {
130
130
  const E = {
131
131
  initialState: s,
132
132
  updaterState: ee(
@@ -159,18 +159,18 @@ const Qe = (e, s) => {
159
159
  switch (e) {
160
160
  case "update":
161
161
  return {
162
- oldValue: M(s, d),
163
- newValue: M(m, d)
162
+ oldValue: U(s, d),
163
+ newValue: U(m, d)
164
164
  };
165
165
  case "insert":
166
166
  return {
167
167
  oldValue: null,
168
168
  // or undefined
169
- newValue: M(m, d)
169
+ newValue: U(m, d)
170
170
  };
171
171
  case "cut":
172
172
  return {
173
- oldValue: M(s, d),
173
+ oldValue: U(s, d),
174
174
  newValue: null
175
175
  // or undefined
176
176
  };
@@ -190,10 +190,10 @@ function De(e, {
190
190
  initialState: S,
191
191
  syncUpdate: t,
192
192
  dependencies: v,
193
- serverState: A
193
+ serverState: T
194
194
  } = {}) {
195
- const [b, j] = de({}), { sessionId: w } = $e();
196
- let F = !s;
195
+ const [b, p] = de({}), { sessionId: w } = $e();
196
+ let R = !s;
197
197
  const [r] = de(s ?? ge()), J = a.getState().stateLog[r], H = Q(/* @__PURE__ */ new Set()), W = Q(l ?? ge()), n = Q(
198
198
  null
199
199
  );
@@ -215,29 +215,29 @@ function De(e, {
215
215
  initialState: S
216
216
  });
217
217
  const c = n.current, y = c?.serverState?.id !== void 0 && c?.serverState?.status === "success" && c?.serverState?.data, k = a.getState().initialStateGlobal[r];
218
- if (!(k && !D(k, S) || !k) && !y)
218
+ if (!(k && !M(k, S) || !k) && !y)
219
219
  return;
220
220
  let $ = null;
221
- const T = L(c?.localStorage?.key) ? c?.localStorage?.key(S) : c?.localStorage?.key;
222
- T && w && ($ = ne(`${w}-${r}-${T}`));
221
+ const A = L(c?.localStorage?.key) ? c?.localStorage?.key(S) : c?.localStorage?.key;
222
+ A && w && ($ = ne(`${w}-${r}-${A}`));
223
223
  let x = S, B = !1;
224
224
  const ae = y ? Date.now() : 0, Y = $?.lastUpdated || 0, oe = $?.lastSyncedWithServer || 0;
225
- y && ae > Y ? (x = c.serverState.data, B = !0) : $ && Y > oe && (x = $.state, c?.localStorage?.onChange && c?.localStorage?.onChange(x)), pe(
225
+ y && ae > Y ? (x = c.serverState.data, B = !0) : $ && Y > oe && (x = $.state, c?.localStorage?.onChange && c?.localStorage?.onChange(x)), Re(
226
226
  r,
227
227
  S,
228
228
  x,
229
229
  o,
230
230
  W.current,
231
231
  w
232
- ), B && T && w && Ee(x, r, c, w, Date.now()), re(r), (Array.isArray(f) ? f : [f || "component"]).includes("none") || j({});
232
+ ), B && A && w && Ee(x, r, c, w, Date.now()), re(r), (Array.isArray(f) ? f : [f || "component"]).includes("none") || p({});
233
233
  }
234
234
  }, [
235
235
  S,
236
- A?.status,
237
- A?.data,
236
+ T?.status,
237
+ T?.data,
238
238
  ...v || []
239
239
  ]), we(() => {
240
- F && ve(r, {
240
+ R && ve(r, {
241
241
  serverSync: m,
242
242
  formElements: u,
243
243
  initialState: S,
@@ -248,12 +248,12 @@ function De(e, {
248
248
  components: /* @__PURE__ */ new Map()
249
249
  };
250
250
  return i.components.set(c, {
251
- forceUpdate: () => j({}),
251
+ forceUpdate: () => p({}),
252
252
  paths: /* @__PURE__ */ new Set(),
253
253
  deps: [],
254
254
  depsFunction: E || void 0,
255
255
  reactiveType: f ?? ["component", "deps"]
256
- }), a.getState().stateComponents.set(r, i), j({}), () => {
256
+ }), a.getState().stateComponents.set(r, i), p({}), () => {
257
257
  const y = `${r}////${W.current}`;
258
258
  i && (i.components.delete(y), i.components.size === 0 && a.getState().stateComponents.delete(r));
259
259
  };
@@ -264,28 +264,28 @@ function De(e, {
264
264
  H.current.add(N);
265
265
  }
266
266
  q(r, (N) => {
267
- const $ = L(c) ? c(N) : c, T = `${r}-${i.join(".")}`;
268
- if (T) {
269
- let O = !1, h = a.getState().signalDomElements.get(T);
267
+ const $ = L(c) ? c(N) : c, A = `${r}-${i.join(".")}`;
268
+ if (A) {
269
+ let j = !1, h = a.getState().signalDomElements.get(A);
270
270
  if ((!h || h.size === 0) && (y.updateType === "insert" || y.updateType === "cut")) {
271
- const R = i.slice(0, -1), p = M($, R);
272
- if (Array.isArray(p)) {
273
- O = !0;
274
- const _ = `${r}-${R.join(".")}`;
271
+ const P = i.slice(0, -1), F = U($, P);
272
+ if (Array.isArray(F)) {
273
+ j = !0;
274
+ const _ = `${r}-${P.join(".")}`;
275
275
  h = a.getState().signalDomElements.get(_);
276
276
  }
277
277
  }
278
278
  if (h) {
279
- const R = O ? M($, i.slice(0, -1)) : M($, i);
280
- h.forEach(({ parentId: p, position: _, effect: C }) => {
279
+ const P = j ? U($, i.slice(0, -1)) : U($, i);
280
+ h.forEach(({ parentId: F, position: _, effect: C }) => {
281
281
  const V = document.querySelector(
282
- `[data-parent-id="${p}"]`
282
+ `[data-parent-id="${F}"]`
283
283
  );
284
284
  if (V) {
285
- const U = Array.from(V.childNodes);
286
- if (U[_]) {
287
- const P = C ? new Function("state", `return (${C})(state)`)(R) : R;
288
- U[_].textContent = String(P);
285
+ const D = Array.from(V.childNodes);
286
+ if (D[_]) {
287
+ const O = C ? new Function("state", `return (${C})(state)`)(P) : P;
288
+ D[_].textContent = String(O);
289
289
  }
290
290
  }
291
291
  });
@@ -299,18 +299,18 @@ function De(e, {
299
299
  n.current?.validation?.key + "." + x.join(".")
300
300
  ), y.updateType === "insert" && n.current?.validation?.key && Pe(
301
301
  n.current?.validation?.key + "." + x.join(".")
302
- ).filter(([h, R]) => {
303
- let p = h?.split(".").length;
304
- if (h == x.join(".") && p == x.length - 1) {
302
+ ).filter(([h, P]) => {
303
+ let F = h?.split(".").length;
304
+ if (h == x.join(".") && F == x.length - 1) {
305
305
  let _ = h + "." + x;
306
- G(h), je(_, R);
306
+ G(h), je(_, P);
307
307
  }
308
308
  });
309
309
  const B = a.getState().stateComponents.get(r);
310
310
  if (B) {
311
- const O = _e(N, $), h = new Set(O), R = y.updateType === "update" ? i.join(".") : i.slice(0, -1).join(".") || "";
311
+ const j = _e(N, $), h = new Set(j), P = y.updateType === "update" ? i.join(".") : i.slice(0, -1).join(".") || "";
312
312
  for (const [
313
- p,
313
+ F,
314
314
  _
315
315
  ] of B.components.entries()) {
316
316
  let C = !1;
@@ -320,47 +320,48 @@ function De(e, {
320
320
  _.forceUpdate();
321
321
  continue;
322
322
  }
323
- if (V.includes("component") && ((_.paths.has(R) || _.paths.has("")) && (C = !0), !C))
324
- for (const U of h) {
325
- let P = U;
323
+ if (V.includes("component") && ((_.paths.has(P) || _.paths.has("")) && (C = !0), !C))
324
+ for (const D of h) {
325
+ let O = D;
326
326
  for (; ; ) {
327
- if (_.paths.has(P)) {
327
+ if (_.paths.has(O)) {
328
328
  C = !0;
329
329
  break;
330
330
  }
331
- const se = P.lastIndexOf(".");
331
+ const se = O.lastIndexOf(".");
332
332
  if (se !== -1) {
333
- const Se = P.substring(
333
+ const Se = O.substring(
334
334
  0,
335
335
  se
336
336
  );
337
337
  if (!isNaN(
338
- Number(P.substring(se + 1))
338
+ Number(O.substring(se + 1))
339
339
  ) && _.paths.has(Se)) {
340
340
  C = !0;
341
341
  break;
342
342
  }
343
- P = Se;
343
+ O = Se;
344
344
  } else
345
- P = "";
346
- if (P === "")
345
+ O = "";
346
+ if (O === "")
347
347
  break;
348
348
  }
349
349
  if (C) break;
350
350
  }
351
351
  if (!C && V.includes("deps") && _.depsFunction) {
352
- const U = _.depsFunction($);
353
- let P = !1;
354
- typeof U == "boolean" ? U && (P = !0) : D(_.deps, U) || (_.deps = U, P = !0), P && (C = !0);
352
+ const D = _.depsFunction($);
353
+ let O = !1;
354
+ typeof D == "boolean" ? D && (O = !0) : M(_.deps, D) || (_.deps = D, O = !0), O && (C = !0);
355
355
  }
356
356
  C && _.forceUpdate();
357
357
  }
358
358
  }
359
359
  }
360
360
  const ae = Date.now();
361
- i = i.map(
362
- (O, h) => h === i.length - 1 && O === "-1" ? (i.length - 1).toString() : O
363
- ), console.log(
361
+ i = i.map((j, h) => {
362
+ const P = i.slice(0, -1), F = U($, P);
363
+ return h === i.length - 1 && ["isnert", "cut"].includes(y.updateType) && j === "-1" ? F.length.toString() : j;
364
+ }), console.log(
364
365
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
365
366
  y.updateType,
366
367
  N,
@@ -381,12 +382,12 @@ function De(e, {
381
382
  oldValue: Y,
382
383
  newValue: oe
383
384
  };
384
- if (Oe(r, (O) => {
385
- const R = [...O ?? [], ie].reduce((p, _) => {
386
- const C = `${_.stateKey}:${JSON.stringify(_.path)}`, V = p.get(C);
387
- return V ? (V.timeStamp = Math.max(V.timeStamp, _.timeStamp), V.newValue = _.newValue, V.oldValue = V.oldValue ?? _.oldValue, V.updateType = _.updateType) : p.set(C, { ..._ }), p;
385
+ if (Oe(r, (j) => {
386
+ const P = [...j ?? [], ie].reduce((F, _) => {
387
+ const C = `${_.stateKey}:${JSON.stringify(_.path)}`, V = F.get(C);
388
+ return V ? (V.timeStamp = Math.max(V.timeStamp, _.timeStamp), V.newValue = _.newValue, V.oldValue = V.oldValue ?? _.oldValue, V.updateType = _.updateType) : F.set(C, { ..._ }), F;
388
389
  }, /* @__PURE__ */ new Map());
389
- return Array.from(R.values());
390
+ return Array.from(P.values());
390
391
  }), Ee(
391
392
  $,
392
393
  r,
@@ -396,10 +397,10 @@ function De(e, {
396
397
  updateLog: J,
397
398
  update: ie
398
399
  }), n.current?.serverSync) {
399
- const O = a.getState().serverState[r], h = n.current?.serverSync;
400
+ const j = a.getState().serverState[r], h = n.current?.serverSync;
400
401
  Fe(r, {
401
402
  syncKey: typeof h.syncKey == "string" ? h.syncKey : h.syncKey({ state: $ }),
402
- rollBackState: O,
403
+ rollBackState: j,
403
404
  actionTimeStamp: Date.now() + (h.debounce ?? 3e3),
404
405
  status: "waiting"
405
406
  });
@@ -441,10 +442,10 @@ function ee(e, s, m, d) {
441
442
  t?.key && G(t?.key), S?.validationKey && G(S.validationKey);
442
443
  const v = a.getState().initialStateGlobal[e];
443
444
  a.getState().clearSelectedIndexesForState(e), u.clear(), I++;
444
- const A = l(v, []), b = z(e), j = L(b?.localStorage?.key) ? b?.localStorage?.key(v) : b?.localStorage?.key, w = `${d}-${e}-${j}`;
445
- w && localStorage.removeItem(w), K(e, A), q(e, v);
446
- const F = a.getState().stateComponents.get(e);
447
- return F && F.components.forEach((r) => {
445
+ const T = l(v, []), b = z(e), p = L(b?.localStorage?.key) ? b?.localStorage?.key(v) : b?.localStorage?.key, w = `${d}-${e}-${p}`;
446
+ w && localStorage.removeItem(w), K(e, T), q(e, v);
447
+ const R = a.getState().stateComponents.get(e);
448
+ return R && R.components.forEach((r) => {
448
449
  r.forceUpdate();
449
450
  }), v;
450
451
  },
@@ -455,12 +456,12 @@ function ee(e, s, m, d) {
455
456
  s,
456
457
  m,
457
458
  d
458
- ), v = a.getState().initialStateGlobal[e], A = z(e), b = L(A?.localStorage?.key) ? A?.localStorage?.key(v) : A?.localStorage?.key, j = `${d}-${e}-${b}`;
459
- return console.log("removing storage", j), localStorage.getItem(j) && localStorage.removeItem(j), Ve(() => {
459
+ ), v = a.getState().initialStateGlobal[e], T = z(e), b = L(T?.localStorage?.key) ? T?.localStorage?.key(v) : T?.localStorage?.key, p = `${d}-${e}-${b}`;
460
+ return console.log("removing storage", p), localStorage.getItem(p) && localStorage.removeItem(p), Ve(() => {
460
461
  fe(e, S), K(e, t), q(e, S);
461
462
  const w = a.getState().stateComponents.get(e);
462
- w && w.components.forEach((F) => {
463
- F.forceUpdate();
463
+ w && w.components.forEach((R) => {
464
+ R.forceUpdate();
464
465
  });
465
466
  }), {
466
467
  fetchId: (w) => t.get()[w]
@@ -471,25 +472,25 @@ function ee(e, s, m, d) {
471
472
  _isLoading: a.getState().isLoadingGlobal[e],
472
473
  _isServerSynced: () => {
473
474
  const S = a.getState().serverState[e];
474
- return !!(S && D(S, he(e)));
475
+ return !!(S && M(S, he(e)));
475
476
  }
476
477
  };
477
478
  function l(S, t = [], v) {
478
- const A = t.map(String).join(".");
479
- u.get(A);
479
+ const T = t.map(String).join(".");
480
+ u.get(T);
480
481
  const b = function() {
481
482
  return a().getNestedState(e, t);
482
483
  };
483
- Object.keys(f).forEach((F) => {
484
- b[F] = f[F];
484
+ Object.keys(f).forEach((R) => {
485
+ b[R] = f[R];
485
486
  });
486
- const j = {
487
- apply(F, r, J) {
487
+ const p = {
488
+ apply(R, r, J) {
488
489
  return console.log(
489
490
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
490
491
  ), console.trace("Apply trap stack trace"), a().getNestedState(e, t);
491
492
  },
492
- get(F, r) {
493
+ get(R, r) {
493
494
  if (r !== "then" && !r.startsWith("$") && r !== "stateMapNoRender") {
494
495
  const n = t.join("."), o = `${e}////${m}`, g = a.getState().stateComponents.get(e);
495
496
  if (g) {
@@ -526,16 +527,16 @@ function ee(e, s, m, d) {
526
527
  }
527
528
  };
528
529
  if (r === "_status") {
529
- const n = a.getState().getNestedState(e, t), o = a.getState().initialStateGlobal[e], g = M(o, t);
530
- return D(n, g) ? "fresh" : "stale";
530
+ const n = a.getState().getNestedState(e, t), o = a.getState().initialStateGlobal[e], g = U(o, t);
531
+ return M(n, g) ? "fresh" : "stale";
531
532
  }
532
533
  if (r === "getStatus")
533
534
  return function() {
534
535
  const n = a().getNestedState(
535
536
  e,
536
537
  t
537
- ), o = a.getState().initialStateGlobal[e], g = M(o, t);
538
- return D(n, g) ? "fresh" : "stale";
538
+ ), o = a.getState().initialStateGlobal[e], g = U(o, t);
539
+ return M(n, g) ? "fresh" : "stale";
539
540
  };
540
541
  if (r === "removeStorage")
541
542
  return () => {
@@ -654,18 +655,18 @@ function ee(e, s, m, d) {
654
655
  let y = null;
655
656
  if (!c.some((N) => {
656
657
  if (o) {
657
- const T = o.every(
658
- (x) => D(N[x], i[x])
658
+ const A = o.every(
659
+ (x) => M(N[x], i[x])
659
660
  );
660
- return T && (y = N), T;
661
+ return A && (y = N), A;
661
662
  }
662
- const $ = D(N, i);
663
+ const $ = M(N, i);
663
664
  return $ && (y = N), $;
664
665
  }))
665
666
  E(t), ce(s, i, t, e);
666
667
  else if (g && y) {
667
668
  const N = g(y), $ = c.map(
668
- (T) => D(T, y) ? N : T
669
+ (A) => M(A, y) ? N : A
669
670
  );
670
671
  E(t), Z(s, $, t);
671
672
  }
@@ -778,8 +779,8 @@ function ee(e, s, m, d) {
778
779
  });
779
780
  const i = n.zodSchema.safeParse(g);
780
781
  return i.success ? !0 : (i.error.errors.forEach((k) => {
781
- const N = k.path, $ = k.message, T = [n.key, ...N].join(".");
782
- o(T, $);
782
+ const N = k.path, $ = k.message, A = [n.key, ...N].join(".");
783
+ o(A, $);
783
784
  }), re(e), !1);
784
785
  } catch (c) {
785
786
  return console.error("Zod schema validation failed", c), !1;
@@ -824,7 +825,7 @@ function ee(e, s, m, d) {
824
825
  if (r === "update")
825
826
  return (n, o) => {
826
827
  if (o?.debounce)
827
- Te(() => {
828
+ Ae(() => {
828
829
  Z(s, n, t, "");
829
830
  const g = a.getState().getNestedState(e, t);
830
831
  o?.afterUpdate && o.afterUpdate(g);
@@ -850,8 +851,8 @@ function ee(e, s, m, d) {
850
851
  const H = [...t, r], W = a.getState().getNestedState(e, H);
851
852
  return l(W, H, v);
852
853
  }
853
- }, w = new Proxy(b, j);
854
- return u.set(A, {
854
+ }, w = new Proxy(b, p);
855
+ return u.set(T, {
855
856
  proxy: w,
856
857
  stateVersion: I
857
858
  }), w;
@@ -906,8 +907,8 @@ function Ge({
906
907
  else
907
908
  v = t;
908
909
  v !== null && typeof v == "object" && (v = JSON.stringify(v));
909
- const A = document.createTextNode(String(v));
910
- d.replaceWith(A);
910
+ const T = document.createTextNode(String(v));
911
+ d.replaceWith(T);
911
912
  }, [e._stateKey, e._path.join("."), e._effect]), te("span", {
912
913
  ref: s,
913
914
  style: { display: "none" },