cogsbox-state 0.5.6 → 0.5.7

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 (35) hide show
  1. package/dist/CogsState.jsx +323 -323
  2. package/dist/CogsState.jsx.map +1 -1
  3. package/dist/CogsStateClient.jsx +6 -6
  4. package/dist/CogsStateClient.jsx.map +1 -1
  5. package/dist/Functions.jsx +106 -106
  6. package/dist/Functions.jsx.map +1 -1
  7. package/dist/node_modules/zustand/esm/react.js +1 -1
  8. package/dist/useValidateZodPath.js +1 -1
  9. package/package.json +1 -1
  10. package/dist/_virtual/_commonjsHelpers.js +0 -7
  11. package/dist/_virtual/_commonjsHelpers.js.map +0 -1
  12. package/dist/_virtual/index.js +0 -5
  13. package/dist/_virtual/index.js.map +0 -1
  14. package/dist/_virtual/jsx-runtime.jsx +0 -5
  15. package/dist/_virtual/jsx-runtime.jsx.map +0 -1
  16. package/dist/_virtual/react-jsx-runtime.jsx +0 -5
  17. package/dist/_virtual/react-jsx-runtime.jsx.map +0 -1
  18. package/dist/_virtual/react-jsx-runtime.production.jsx +0 -5
  19. package/dist/_virtual/react-jsx-runtime.production.jsx.map +0 -1
  20. package/dist/_virtual/react.js +0 -5
  21. package/dist/_virtual/react.js.map +0 -1
  22. package/dist/_virtual/react.production.js +0 -5
  23. package/dist/_virtual/react.production.js.map +0 -1
  24. package/dist/node_modules/react/cjs/react-jsx-runtime.jsx +0 -605
  25. package/dist/node_modules/react/cjs/react-jsx-runtime.jsx.map +0 -1
  26. package/dist/node_modules/react/cjs/react-jsx-runtime.production.jsx +0 -29
  27. package/dist/node_modules/react/cjs/react-jsx-runtime.production.jsx.map +0 -1
  28. package/dist/node_modules/react/cjs/react.js +0 -1274
  29. package/dist/node_modules/react/cjs/react.js.map +0 -1
  30. package/dist/node_modules/react/cjs/react.production.js +0 -209
  31. package/dist/node_modules/react/cjs/react.production.js.map +0 -1
  32. package/dist/node_modules/react/index.js +0 -12
  33. package/dist/node_modules/react/index.js.map +0 -1
  34. package/dist/node_modules/react/jsx-runtime.jsx +0 -9
  35. package/dist/node_modules/react/jsx-runtime.jsx.map +0 -1
@@ -1,48 +1,48 @@
1
1
  "use client";
2
- import { j as et } from "./node_modules/react/jsx-runtime.jsx";
3
- import { r as _ } from "./node_modules/react/index.js";
4
- import { transformStateFunc as gt, isFunction as st, getNestedValue as L, isDeepEqual as G, debounce as St } from "./utility.js";
5
- import { pushFunc as nt, updateFn as z, cutFunc as rt, ValidationWrapper as mt, FormControlComponent as yt } from "./Functions.jsx";
2
+ import { jsx as at } from "react/jsx-runtime";
3
+ import { useState as Y, useRef as z, useEffect as K, useLayoutEffect as vt, useMemo as It, createElement as H, useSyncExternalStore as pt, startTransition as J } from "react";
4
+ import { transformStateFunc as _t, isFunction as ut, getNestedValue as D, isDeepEqual as L, debounce as Et } from "./utility.js";
5
+ import { pushFunc as it, updateFn as q, cutFunc as st, ValidationWrapper as wt, FormControlComponent as $t } from "./Functions.jsx";
6
6
  import "zod";
7
- import { getGlobalStore as a, formRefStore as at } from "./store.js";
8
- import { useCogsConfig as vt } from "./CogsStateClient.jsx";
9
- import Q from "./node_modules/uuid/dist/esm-browser/v4.js";
10
- function ot(t, i) {
11
- const f = a.getState().getInitialOptions, S = a.getState().setInitialStateOptions, g = f(t) || {};
7
+ import { getGlobalStore as o, formRefStore as ct } from "./store.js";
8
+ import { useCogsConfig as Vt } from "./CogsStateClient.jsx";
9
+ import tt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
+ function lt(t, i) {
11
+ const f = o.getState().getInitialOptions, S = o.getState().setInitialStateOptions, g = f(t) || {};
12
12
  S(t, {
13
13
  ...g,
14
14
  ...i
15
15
  });
16
16
  }
17
- function it({
17
+ function dt({
18
18
  stateKey: t,
19
19
  options: i,
20
20
  initialOptionsPart: f
21
21
  }) {
22
- const S = X(t) || {}, g = f[t] || {}, I = a.getState().setInitialStateOptions, E = { ...g, ...S };
22
+ const S = et(t) || {}, g = f[t] || {}, I = o.getState().setInitialStateOptions, _ = { ...g, ...S };
23
23
  let p = !1;
24
24
  if (i)
25
25
  for (const m in i)
26
- E.hasOwnProperty(m) || (p = !0, E[m] = i[m]);
27
- p && I(t, E);
26
+ _.hasOwnProperty(m) || (p = !0, _[m] = i[m]);
27
+ p && I(t, _);
28
28
  }
29
- const bt = (t, i) => {
29
+ const qt = (t, i) => {
30
30
  let f = t;
31
- const [S, g] = gt(f);
32
- a.getState().setInitialStates(S);
31
+ const [S, g] = _t(f);
32
+ o.getState().setInitialStates(S);
33
33
  const I = (p, m) => {
34
- const [w] = _.useState(m?.componentId ?? Q());
35
- it({
34
+ const [E] = Y(m?.componentId ?? tt());
35
+ dt({
36
36
  stateKey: p,
37
37
  options: m,
38
38
  initialOptionsPart: g
39
39
  });
40
- const l = a.getState().cogsStateStore[p] || S[p], e = m?.modifyState ? m.modifyState(l) : l, [v, T] = Nt(
40
+ const l = o.getState().cogsStateStore[p] || S[p], e = m?.modifyState ? m.modifyState(l) : l, [v, h] = jt(
41
41
  e,
42
42
  {
43
43
  stateKey: p,
44
44
  syncUpdate: m?.syncUpdate,
45
- componentId: w,
45
+ componentId: E,
46
46
  localStorage: m?.localStorage,
47
47
  middleware: m?.middleware,
48
48
  enabledSync: m?.enabledSync,
@@ -52,24 +52,24 @@ const bt = (t, i) => {
52
52
  localStorageKey: m?.localStorageKey
53
53
  }
54
54
  );
55
- return T;
55
+ return h;
56
56
  };
57
- function E(p, m) {
58
- it({ stateKey: p, options: m, initialOptionsPart: g });
57
+ function _(p, m) {
58
+ dt({ stateKey: p, options: m, initialOptionsPart: g });
59
59
  }
60
- return { useCogsState: I, setCogsOptions: E };
60
+ return { useCogsState: I, setCogsOptions: _ };
61
61
  }, {
62
- setUpdaterState: J,
63
- setState: k,
64
- getInitialOptions: X,
65
- getKeyState: ct,
66
- getValidationErrors: It,
67
- setStateLog: pt,
68
- updateInitialStateGlobal: Y,
69
- addValidationError: Et,
70
- removeValidationError: P,
71
- setServerSyncActions: _t
72
- } = a.getState(), lt = (t) => {
62
+ setUpdaterState: B,
63
+ setState: x,
64
+ getInitialOptions: et,
65
+ getKeyState: ft,
66
+ getValidationErrors: Nt,
67
+ setStateLog: ht,
68
+ updateInitialStateGlobal: nt,
69
+ addValidationError: Ct,
70
+ removeValidationError: R,
71
+ setServerSyncActions: Tt
72
+ } = o.getState(), gt = (t) => {
73
73
  if (!t) return null;
74
74
  try {
75
75
  const i = window.localStorage.getItem(t);
@@ -77,20 +77,20 @@ const bt = (t, i) => {
77
77
  } catch (i) {
78
78
  return console.error("Error loading from localStorage:", i), null;
79
79
  }
80
- }, wt = (t, i, f, S) => {
80
+ }, At = (t, i, f, S) => {
81
81
  if (f.localStorageKey) {
82
82
  const g = {
83
83
  state: t,
84
84
  lastUpdated: Date.now(),
85
- lastSyncedWithServer: a.getState().serverSyncLog[i]?.[0]?.timeStamp,
86
- baseServerState: a.getState().serverState[i]
85
+ lastSyncedWithServer: o.getState().serverSyncLog[i]?.[0]?.timeStamp,
86
+ baseServerState: o.getState().serverState[i]
87
87
  }, I = f.initState ? `${S}-${i}-${f.localStorageKey}` : i;
88
88
  window.localStorage.setItem(I, JSON.stringify(g));
89
89
  }
90
- }, $t = (t, i, f, S, g, I) => {
91
- const E = {
90
+ }, Ft = (t, i, f, S, g, I) => {
91
+ const _ = {
92
92
  initialState: i,
93
- updaterState: B(
93
+ updaterState: Z(
94
94
  t,
95
95
  S,
96
96
  g,
@@ -98,306 +98,306 @@ const bt = (t, i) => {
98
98
  ),
99
99
  state: f
100
100
  };
101
- _.startTransition(() => {
102
- Y(t, E.initialState), J(t, E.updaterState), k(t, E.state);
101
+ J(() => {
102
+ nt(t, _.initialState), B(t, _.updaterState), x(t, _.state);
103
103
  });
104
- }, Vt = (t) => {
105
- const i = a.getState().stateComponents.get(t);
104
+ }, Mt = (t) => {
105
+ const i = o.getState().stateComponents.get(t);
106
106
  if (!i) return;
107
107
  const f = /* @__PURE__ */ new Set();
108
108
  i.components.forEach((S) => {
109
109
  f.add(() => S.forceUpdate());
110
110
  }), queueMicrotask(() => {
111
- _.startTransition(() => {
111
+ J(() => {
112
112
  f.forEach((S) => S());
113
113
  });
114
114
  });
115
- }, Pt = (t, i) => {
116
- const f = a.getState().stateComponents.get(t);
115
+ }, zt = (t, i) => {
116
+ const f = o.getState().stateComponents.get(t);
117
117
  if (f) {
118
118
  const S = `${t}////${i}`, g = f.components.get(S);
119
119
  g && g.forceUpdate();
120
120
  }
121
121
  };
122
- function Nt(t, {
122
+ function jt(t, {
123
123
  stateKey: i,
124
124
  serverSync: f,
125
125
  localStorage: S,
126
126
  formElements: g,
127
127
  middleware: I,
128
- reactiveDeps: E,
128
+ reactiveDeps: _,
129
129
  reactiveType: p,
130
130
  componentId: m,
131
- localStorageKey: w,
131
+ localStorageKey: E,
132
132
  initState: l,
133
133
  syncUpdate: e
134
134
  } = {}) {
135
- const [v, T] = _.useState({}), { sessionId: j } = vt();
136
- let O = !i;
137
- const [d] = _.useState(i ?? Q()), o = a.getState().stateLog[d], W = _.useRef(/* @__PURE__ */ new Set()), U = _.useRef(m ?? Q()), F = _.useRef(null);
138
- F.current = X(d), _.useEffect(() => {
135
+ const [v, h] = Y({}), { sessionId: F } = Vt();
136
+ let U = !i;
137
+ const [d] = Y(i ?? tt()), a = o.getState().stateLog[d], G = z(/* @__PURE__ */ new Set()), b = z(m ?? tt()), A = z(null);
138
+ A.current = et(d), K(() => {
139
139
  if (e && e.stateKey === d && e.path?.[0]) {
140
- k(d, (r) => ({
140
+ x(d, (r) => ({
141
141
  ...r,
142
142
  [e.path[0]]: e.newValue
143
143
  }));
144
144
  const c = `${e.stateKey}:${e.path.join(".")}`;
145
- a.getState().setSyncInfo(c, {
145
+ o.getState().setSyncInfo(c, {
146
146
  timeStamp: e.timeStamp,
147
147
  userId: e.userId
148
148
  });
149
149
  }
150
- }, [e]), _.useEffect(() => {
151
- ot(d, {
150
+ }, [e]), K(() => {
151
+ lt(d, {
152
152
  initState: l
153
153
  });
154
154
  let c = null;
155
- w && (c = lt(
156
- j + "-" + d + "-" + w
155
+ E && (c = gt(
156
+ F + "-" + d + "-" + E
157
157
  ));
158
158
  let r = null;
159
- l?.initialState && (r = l?.initialState, console.log("newState", r), c && c.lastUpdated > (c.lastSyncedWithServer || 0) && (r = c.state), $t(
159
+ l?.initialState && (r = l?.initialState, console.log("newState", r), c && c.lastUpdated > (c.lastSyncedWithServer || 0) && (r = c.state), Ft(
160
160
  d,
161
161
  l?.initialState,
162
162
  r,
163
163
  n,
164
- U.current,
165
- j
166
- ), console.log("newState222", r), T({}));
167
- }, [w, ...l?.dependencies || []]), _.useLayoutEffect(() => {
168
- O && ot(d, {
164
+ b.current,
165
+ F
166
+ ), console.log("newState222", r), h({}));
167
+ }, [E, ...l?.dependencies || []]), vt(() => {
168
+ U && lt(d, {
169
169
  serverSync: f,
170
170
  formElements: g,
171
171
  initState: l,
172
172
  localStorage: S,
173
173
  middleware: I
174
174
  });
175
- const c = `${d}////${U.current}`, r = a.getState().stateComponents.get(d) || {
175
+ const c = `${d}////${b.current}`, r = o.getState().stateComponents.get(d) || {
176
176
  components: /* @__PURE__ */ new Map()
177
177
  };
178
178
  return r.components.set(c, {
179
- forceUpdate: () => T({}),
179
+ forceUpdate: () => h({}),
180
180
  paths: /* @__PURE__ */ new Set(),
181
181
  deps: [],
182
- depsFunction: E || void 0,
182
+ depsFunction: _ || void 0,
183
183
  reactiveType: p ?? ["component", "deps"]
184
- }), a.getState().stateComponents.set(d, r), T({}), () => {
185
- const u = `${d}////${U.current}`;
186
- r && (r.components.delete(u), r.components.size === 0 && a.getState().stateComponents.delete(d));
184
+ }), o.getState().stateComponents.set(d, r), h({}), () => {
185
+ const u = `${d}////${b.current}`;
186
+ r && (r.components.delete(u), r.components.size === 0 && o.getState().stateComponents.delete(d));
187
187
  };
188
188
  }, []);
189
- const n = (c, r, u, $) => {
189
+ const n = (c, r, u, w) => {
190
190
  if (Array.isArray(r)) {
191
191
  const M = `${d}-${r.join(".")}`;
192
- W.current.add(M);
192
+ G.current.add(M);
193
193
  }
194
- k(d, (M) => {
195
- const N = st(c) ? c(M) : c, R = `${d}-${r.join(".")}`;
196
- if (R) {
197
- let b = !1, y = a.getState().signalDomElements.get(R);
194
+ x(d, (M) => {
195
+ const V = ut(c) ? c(M) : c, j = `${d}-${r.join(".")}`;
196
+ if (j) {
197
+ let P = !1, y = o.getState().signalDomElements.get(j);
198
198
  if ((!y || y.size === 0) && (u.updateType === "insert" || u.updateType === "cut")) {
199
- const h = r.slice(0, -1), A = L(N, h);
200
- if (Array.isArray(A)) {
201
- b = !0;
202
- const V = `${d}-${h.join(".")}`;
203
- y = a.getState().signalDomElements.get(V);
199
+ const N = r.slice(0, -1), T = D(V, N);
200
+ if (Array.isArray(T)) {
201
+ P = !0;
202
+ const $ = `${d}-${N.join(".")}`;
203
+ y = o.getState().signalDomElements.get($);
204
204
  }
205
205
  }
206
206
  if (y) {
207
- const h = b ? L(N, r.slice(0, -1)) : L(N, r);
208
- y.forEach(({ parentId: A, position: V, effect: D }) => {
209
- const x = document.querySelector(
210
- `[data-parent-id="${A}"]`
207
+ const N = P ? D(V, r.slice(0, -1)) : D(V, r);
208
+ y.forEach(({ parentId: T, position: $, effect: k }) => {
209
+ const O = document.querySelector(
210
+ `[data-parent-id="${T}"]`
211
211
  );
212
- if (x) {
213
- const tt = Array.from(x.childNodes);
214
- if (tt[V]) {
215
- const ft = D ? new Function("state", `return (${D})(state)`)(h) : h;
216
- tt[V].textContent = String(ft);
212
+ if (O) {
213
+ const ot = Array.from(O.childNodes);
214
+ if (ot[$]) {
215
+ const yt = k ? new Function("state", `return (${k})(state)`)(N) : N;
216
+ ot[$].textContent = String(yt);
217
217
  }
218
218
  }
219
219
  });
220
220
  }
221
221
  }
222
- u.updateType === "update" && ($ || F.current?.validationKey) && r && P(
223
- ($ || F.current?.validationKey) + "." + r.join(".")
222
+ u.updateType === "update" && (w || A.current?.validationKey) && r && R(
223
+ (w || A.current?.validationKey) + "." + r.join(".")
224
224
  );
225
225
  const C = r.slice(0, r.length - 1);
226
- u.updateType === "cut" && F.current?.validationKey && P(
227
- F.current?.validationKey + "." + C.join(".")
228
- ), u.updateType === "insert" && F.current?.validationKey && It(
229
- F.current?.validationKey + "." + C.join(".")
230
- ).filter(([y, h]) => {
231
- let A = y?.split(".").length;
232
- if (y == C.join(".") && A == C.length - 1) {
233
- let V = y + "." + C;
234
- P(y), Et(V, h);
226
+ u.updateType === "cut" && A.current?.validationKey && R(
227
+ A.current?.validationKey + "." + C.join(".")
228
+ ), u.updateType === "insert" && A.current?.validationKey && Nt(
229
+ A.current?.validationKey + "." + C.join(".")
230
+ ).filter(([y, N]) => {
231
+ let T = y?.split(".").length;
232
+ if (y == C.join(".") && T == C.length - 1) {
233
+ let $ = y + "." + C;
234
+ R(y), Ct($, N);
235
235
  }
236
236
  });
237
- const q = L(M, r), dt = L(N, r), ut = u.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), Z = a.getState().stateComponents.get(d);
238
- if (i == "cart" && (console.log("thisKey", d), console.log("stateEntry", Z)), Z)
239
- for (const [b, y] of Z.components.entries()) {
240
- let h = !1;
241
- const A = Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"];
242
- if (!A.includes("none")) {
243
- if (A.includes("all")) {
237
+ const W = D(M, r), St = D(V, r), mt = u.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), Q = o.getState().stateComponents.get(d);
238
+ if (i == "cart" && (console.log("thisKey", d), console.log("stateEntry", Q)), Q)
239
+ for (const [P, y] of Q.components.entries()) {
240
+ let N = !1;
241
+ const T = Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"];
242
+ if (!T.includes("none")) {
243
+ if (T.includes("all")) {
244
244
  y.forceUpdate();
245
245
  continue;
246
246
  }
247
- if (A.includes("component") && y.paths && (y.paths.has(ut) || y.paths.has("")) && (h = !0), !h && A.includes("deps") && y.depsFunction) {
248
- const V = y.depsFunction(N);
249
- typeof V == "boolean" ? V && (h = !0) : G(y.deps, V) || (y.deps = V, h = !0);
247
+ if (T.includes("component") && y.paths && (y.paths.has(mt) || y.paths.has("")) && (N = !0), !N && T.includes("deps") && y.depsFunction) {
248
+ const $ = y.depsFunction(V);
249
+ typeof $ == "boolean" ? $ && (N = !0) : L(y.deps, $) || (y.deps = $, N = !0);
250
250
  }
251
- h && y.forceUpdate();
251
+ N && y.forceUpdate();
252
252
  }
253
253
  }
254
- const K = {
254
+ const rt = {
255
255
  timeStamp: Date.now(),
256
256
  stateKey: d,
257
257
  path: r,
258
258
  updateType: u.updateType,
259
259
  status: "new",
260
- oldValue: q,
261
- newValue: dt
260
+ oldValue: W,
261
+ newValue: St
262
262
  };
263
- if (pt(d, (b) => {
264
- const h = [...b ?? [], K].reduce((A, V) => {
265
- const D = `${V.stateKey}:${JSON.stringify(V.path)}`, x = A.get(D);
266
- return x ? (x.timeStamp = Math.max(x.timeStamp, V.timeStamp), x.newValue = V.newValue, x.oldValue = x.oldValue ?? V.oldValue, x.updateType = V.updateType) : A.set(D, { ...V }), A;
263
+ if (ht(d, (P) => {
264
+ const N = [...P ?? [], rt].reduce((T, $) => {
265
+ const k = `${$.stateKey}:${JSON.stringify($.path)}`, O = T.get(k);
266
+ return O ? (O.timeStamp = Math.max(O.timeStamp, $.timeStamp), O.newValue = $.newValue, O.oldValue = O.oldValue ?? $.oldValue, O.updateType = $.updateType) : T.set(k, { ...$ }), T;
267
267
  }, /* @__PURE__ */ new Map());
268
- return Array.from(h.values());
269
- }), wt(
270
- N,
268
+ return Array.from(N.values());
269
+ }), At(
270
+ V,
271
271
  d,
272
- F.current,
273
- j
272
+ A.current,
273
+ F
274
274
  ), I && I({
275
- updateLog: o,
276
- update: K
277
- }), F.current?.serverSync) {
278
- const b = a.getState().serverState[d], y = F.current?.serverSync;
279
- _t(d, {
280
- syncKey: typeof y.syncKey == "string" ? y.syncKey : y.syncKey({ state: N }),
281
- rollBackState: b,
275
+ updateLog: a,
276
+ update: rt
277
+ }), A.current?.serverSync) {
278
+ const P = o.getState().serverState[d], y = A.current?.serverSync;
279
+ Tt(d, {
280
+ syncKey: typeof y.syncKey == "string" ? y.syncKey : y.syncKey({ state: V }),
281
+ rollBackState: P,
282
282
  actionTimeStamp: Date.now() + (y.debounce ?? 3e3),
283
283
  status: "waiting"
284
284
  });
285
285
  }
286
- return N;
286
+ return V;
287
287
  });
288
288
  };
289
- a.getState().updaterState[d] || (console.log("Initializing state for", d, t), J(
289
+ o.getState().updaterState[d] || (console.log("Initializing state for", d, t), B(
290
290
  d,
291
- B(
291
+ Z(
292
292
  d,
293
293
  n,
294
- U.current,
295
- j
294
+ b.current,
295
+ F
296
296
  )
297
- ), a.getState().cogsStateStore[d] || k(d, t), a.getState().initialStateGlobal[d] || Y(d, t));
298
- const s = _.useMemo(() => B(
297
+ ), o.getState().cogsStateStore[d] || x(d, t), o.getState().initialStateGlobal[d] || nt(d, t));
298
+ const s = It(() => Z(
299
299
  d,
300
300
  n,
301
- U.current,
302
- j
301
+ b.current,
302
+ F
303
303
  ), [d]);
304
- return [ct(d), s];
304
+ return [ft(d), s];
305
305
  }
306
- function B(t, i, f, S) {
306
+ function Z(t, i, f, S) {
307
307
  const g = /* @__PURE__ */ new Map();
308
308
  let I = 0;
309
- const E = (l) => {
309
+ const _ = (l) => {
310
310
  const e = l.join(".");
311
311
  for (const [v] of g)
312
312
  (v === e || v.startsWith(e + ".")) && g.delete(v);
313
313
  I++;
314
314
  }, p = /* @__PURE__ */ new Map(), m = {
315
315
  removeValidation: (l) => {
316
- l?.validationKey && P(l.validationKey);
316
+ l?.validationKey && R(l.validationKey);
317
317
  },
318
318
  revertToInitialState: (l) => {
319
- const e = a.getState().getInitialOptions(t)?.validation;
320
- e?.key && P(e?.key), l?.validationKey && P(l.validationKey);
321
- const v = a.getState().initialStateGlobal[t];
319
+ const e = o.getState().getInitialOptions(t)?.validation;
320
+ e?.key && R(e?.key), l?.validationKey && R(l.validationKey);
321
+ const v = o.getState().initialStateGlobal[t];
322
322
  g.clear(), I++;
323
- const T = w(v, []);
324
- _.startTransition(() => {
325
- J(t, T), k(t, v);
326
- const j = a.getState().stateComponents.get(t);
327
- j && j.components.forEach((d) => {
323
+ const h = E(v, []);
324
+ J(() => {
325
+ B(t, h), x(t, v);
326
+ const F = o.getState().stateComponents.get(t);
327
+ F && F.components.forEach((d) => {
328
328
  d.forceUpdate();
329
329
  });
330
- const O = X(t);
331
- O?.localStorageKey && localStorage.removeItem(
332
- O?.initState ? S + "-" + t + "-" + O?.localStorageKey : t
330
+ const U = et(t);
331
+ U?.localStorageKey && localStorage.removeItem(
332
+ U?.initState ? S + "-" + t + "-" + U?.localStorageKey : t
333
333
  ), localStorage.removeItem(t);
334
334
  });
335
335
  },
336
336
  updateInitialState: (l) => {
337
337
  g.clear(), I++;
338
- const e = B(
338
+ const e = Z(
339
339
  t,
340
340
  i,
341
341
  f,
342
342
  S
343
343
  );
344
- return _.startTransition(() => {
345
- Y(t, l), J(t, e), k(t, l);
346
- const v = a.getState().stateComponents.get(t);
347
- v && v.components.forEach((T) => {
348
- T.forceUpdate();
344
+ return J(() => {
345
+ nt(t, l), B(t, e), x(t, l);
346
+ const v = o.getState().stateComponents.get(t);
347
+ v && v.components.forEach((h) => {
348
+ h.forceUpdate();
349
349
  }), localStorage.removeItem(t);
350
350
  }), {
351
351
  fetchId: (v) => e.get()[v]
352
352
  };
353
353
  },
354
- _initialState: a.getState().initialStateGlobal[t],
355
- _serverState: a.getState().serverState[t],
356
- _isLoading: a.getState().isLoadingGlobal[t],
354
+ _initialState: o.getState().initialStateGlobal[t],
355
+ _serverState: o.getState().serverState[t],
356
+ _isLoading: o.getState().isLoadingGlobal[t],
357
357
  _isServerSynced: () => {
358
- const l = a.getState().serverState[t];
359
- return !!(l && G(l, ct(t)));
358
+ const l = o.getState().serverState[t];
359
+ return !!(l && L(l, ft(t)));
360
360
  }
361
361
  };
362
- function w(l, e = [], v) {
363
- const T = e.map(String).join(".");
364
- g.get(T);
365
- const j = {
366
- get(d, o) {
367
- if (o !== "then" && !o.startsWith("$") && o !== "stateMapNoRender") {
368
- const n = e.join("."), s = `${t}////${f}`, c = a.getState().stateComponents.get(t);
362
+ function E(l, e = [], v) {
363
+ const h = e.map(String).join(".");
364
+ g.get(h);
365
+ const F = {
366
+ get(d, a) {
367
+ if (a !== "then" && !a.startsWith("$") && a !== "stateMapNoRender") {
368
+ const n = e.join("."), s = `${t}////${f}`, c = o.getState().stateComponents.get(t);
369
369
  if (c) {
370
370
  const r = c.components.get(s);
371
- r && (e.length > 0 || o === "get") && r.paths.add(n);
371
+ r && (e.length > 0 || a === "get") && r.paths.add(n);
372
372
  }
373
373
  }
374
- if (o === "showValidationErrors")
374
+ if (a === "showValidationErrors")
375
375
  return () => {
376
- const n = a.getState().getInitialOptions(t)?.validation;
376
+ const n = o.getState().getInitialOptions(t)?.validation;
377
377
  if (!n?.key)
378
378
  throw new Error("Validation key not found");
379
- return a.getState().getValidationErrors(n.key + "." + e.join("."));
379
+ return o.getState().getValidationErrors(n.key + "." + e.join("."));
380
380
  };
381
381
  if (Array.isArray(l)) {
382
- if (o === "getSelected")
382
+ if (a === "getSelected")
383
383
  return () => {
384
384
  const n = p.get(e.join("."));
385
385
  if (n !== void 0)
386
- return w(
386
+ return E(
387
387
  l[n],
388
388
  [...e, n.toString()],
389
389
  v
390
390
  );
391
391
  };
392
- if (o === "stateMap" || o === "stateMapNoRender")
392
+ if (a === "stateMap" || a === "stateMapNoRender")
393
393
  return (n) => {
394
394
  const s = v?.filtered?.some(
395
395
  (r) => r.join(".") === e.join(".")
396
- ), c = s ? l : a.getState().getNestedState(t, e);
397
- return o !== "stateMapNoRender" && (g.clear(), I++), c.map((r, u) => {
398
- const $ = s && r.__origIndex ? r.__origIndex : u, M = w(
396
+ ), c = s ? l : o.getState().getNestedState(t, e);
397
+ return a !== "stateMapNoRender" && (g.clear(), I++), c.map((r, u) => {
398
+ const w = s && r.__origIndex ? r.__origIndex : u, M = E(
399
399
  r,
400
- [...e, $.toString()],
400
+ [...e, w.toString()],
401
401
  v
402
402
  );
403
403
  return n(
@@ -405,222 +405,222 @@ function B(t, i, f, S) {
405
405
  M,
406
406
  u,
407
407
  l,
408
- w(l, e, v)
408
+ E(l, e, v)
409
409
  );
410
410
  });
411
411
  };
412
- if (o === "$stateMap")
413
- return (n) => _.createElement(ht, {
412
+ if (a === "$stateMap")
413
+ return (n) => H(Ot, {
414
414
  proxy: {
415
415
  _stateKey: t,
416
416
  _path: e,
417
417
  _mapFn: n
418
418
  // Pass the actual function, not string
419
419
  },
420
- rebuildStateShape: w
420
+ rebuildStateShape: E
421
421
  });
422
- if (o === "stateFlattenOn")
422
+ if (a === "stateFlattenOn")
423
423
  return (n) => {
424
424
  const c = v?.filtered?.some(
425
425
  (u) => u.join(".") === e.join(".")
426
- ) ? l : a.getState().getNestedState(t, e);
426
+ ) ? l : o.getState().getNestedState(t, e);
427
427
  g.clear(), I++;
428
428
  const r = c.flatMap(
429
- (u, $) => u[n] ?? []
429
+ (u, w) => u[n] ?? []
430
430
  );
431
- return w(
431
+ return E(
432
432
  r,
433
433
  [...e, "[*]", n],
434
434
  v
435
435
  );
436
436
  };
437
- if (o === "findWith")
437
+ if (a === "findWith")
438
438
  return (n, s) => {
439
439
  const c = l.findIndex(
440
- ($) => $[n] === s
440
+ (w) => w[n] === s
441
441
  );
442
442
  if (c === -1) return;
443
443
  const r = l[c], u = [...e, c.toString()];
444
- return g.clear(), I++, g.clear(), I++, w(r, u);
444
+ return g.clear(), I++, g.clear(), I++, E(r, u);
445
445
  };
446
- if (o === "index")
446
+ if (a === "index")
447
447
  return (n) => {
448
448
  const s = l[n];
449
- return w(s, [...e, n.toString()]);
449
+ return E(s, [...e, n.toString()]);
450
450
  };
451
- if (o === "insert")
452
- return (n) => (E(e), nt(i, n, e, t), w(
453
- a.getState().cogsStateStore[t],
451
+ if (a === "insert")
452
+ return (n) => (_(e), it(i, n, e, t), E(
453
+ o.getState().cogsStateStore[t],
454
454
  []
455
455
  ));
456
- if (o === "uniqueInsert")
456
+ if (a === "uniqueInsert")
457
457
  return (n, s, c) => {
458
- const r = a.getState().getNestedState(t, e), u = st(n) ? n(r) : n;
459
- let $ = null;
460
- if (!r.some((N) => {
458
+ const r = o.getState().getNestedState(t, e), u = ut(n) ? n(r) : n;
459
+ let w = null;
460
+ if (!r.some((V) => {
461
461
  if (s) {
462
462
  const C = s.every(
463
- (q) => G(N[q], u[q])
463
+ (W) => L(V[W], u[W])
464
464
  );
465
- return C && ($ = N), C;
465
+ return C && (w = V), C;
466
466
  }
467
- const R = G(N, u);
468
- return R && ($ = N), R;
467
+ const j = L(V, u);
468
+ return j && (w = V), j;
469
469
  }))
470
- E(e), nt(i, u, e, t);
471
- else if (c && $) {
472
- const N = c($), R = r.map(
473
- (C) => G(C, $) ? N : C
470
+ _(e), it(i, u, e, t);
471
+ else if (c && w) {
472
+ const V = c(w), j = r.map(
473
+ (C) => L(C, w) ? V : C
474
474
  );
475
- E(e), z(i, R, e);
475
+ _(e), q(i, j, e);
476
476
  }
477
477
  };
478
- if (o === "cut")
478
+ if (a === "cut")
479
479
  return (n, s) => {
480
- s?.waitForSync || (E(e), rt(i, e, t, n));
480
+ s?.waitForSync || (_(e), st(i, e, t, n));
481
481
  };
482
- if (o === "stateFilter")
482
+ if (a === "stateFilter")
483
483
  return (n) => {
484
- const s = l.map((u, $) => ({
484
+ const s = l.map((u, w) => ({
485
485
  ...u,
486
- __origIndex: $.toString()
486
+ __origIndex: w.toString()
487
487
  })), c = [], r = [];
488
488
  for (let u = 0; u < s.length; u++)
489
489
  n(s[u], u) && (c.push(u), r.push(s[u]));
490
- return g.clear(), I++, w(r, e, {
490
+ return g.clear(), I++, E(r, e, {
491
491
  filtered: [...v?.filtered || [], e],
492
492
  validIndices: c
493
493
  // Pass through the meta
494
494
  });
495
495
  };
496
496
  }
497
- const W = e[e.length - 1];
498
- if (!isNaN(Number(W))) {
499
- const n = e.slice(0, -1), s = a.getState().getNestedState(t, n);
500
- if (Array.isArray(s) && o === "cut")
501
- return () => rt(
497
+ const G = e[e.length - 1];
498
+ if (!isNaN(Number(G))) {
499
+ const n = e.slice(0, -1), s = o.getState().getNestedState(t, n);
500
+ if (Array.isArray(s) && a === "cut")
501
+ return () => st(
502
502
  i,
503
503
  n,
504
504
  t,
505
- Number(W)
505
+ Number(G)
506
506
  );
507
507
  }
508
- if (o === "get")
509
- return () => a.getState().getNestedState(t, e);
510
- if (o === "$derive")
511
- return (n) => H({
508
+ if (a === "get")
509
+ return () => o.getState().getNestedState(t, e);
510
+ if (a === "$derive")
511
+ return (n) => X({
512
512
  _stateKey: t,
513
513
  _path: e,
514
514
  _effect: n.toString()
515
515
  });
516
- if (o === "$derive")
517
- return (n) => H({
516
+ if (a === "$derive")
517
+ return (n) => X({
518
518
  _stateKey: t,
519
519
  _path: e,
520
520
  _effect: n.toString()
521
521
  });
522
- if (o === "$get")
523
- return () => H({
522
+ if (a === "$get")
523
+ return () => X({
524
524
  _stateKey: t,
525
525
  _path: e
526
526
  });
527
- if (o === "lastSynced") {
527
+ if (a === "lastSynced") {
528
528
  const n = `${t}:${e.join(".")}`;
529
- return a.getState().getSyncInfo(n);
529
+ return o.getState().getSyncInfo(n);
530
530
  }
531
- if (o === "_selected") {
532
- const n = e.slice(0, -1), s = n.join("."), c = a.getState().getNestedState(t, n);
531
+ if (a === "_selected") {
532
+ const n = e.slice(0, -1), s = n.join("."), c = o.getState().getNestedState(t, n);
533
533
  return Array.isArray(c) ? Number(e[e.length - 1]) === p.get(s) : void 0;
534
534
  }
535
- if (o == "getLocalStorage")
536
- return (n) => lt(S + "-" + t + "-" + n);
537
- if (o === "setSelected")
535
+ if (a == "getLocalStorage")
536
+ return (n) => gt(S + "-" + t + "-" + n);
537
+ if (a === "setSelected")
538
538
  return (n) => {
539
539
  const s = e.slice(0, -1), c = Number(e[e.length - 1]), r = s.join(".");
540
540
  n ? p.set(r, c) : p.delete(r);
541
- const u = a.getState().getNestedState(t, [...s]);
542
- z(i, u, s), E(s);
541
+ const u = o.getState().getNestedState(t, [...s]);
542
+ q(i, u, s), _(s);
543
543
  };
544
544
  if (e.length == 0) {
545
- if (o === "validateZodSchema")
545
+ if (a === "validateZodSchema")
546
546
  return () => {
547
- const n = a.getState().getInitialOptions(t)?.validation, s = a.getState().addValidationError;
547
+ const n = o.getState().getInitialOptions(t)?.validation, s = o.getState().addValidationError;
548
548
  if (!n?.zodSchema)
549
549
  throw new Error("Zod schema not found");
550
550
  if (!n?.key)
551
551
  throw new Error("Validation key not found");
552
- P(n.key);
553
- const c = a.getState().cogsStateStore[t];
552
+ R(n.key);
553
+ const c = o.getState().cogsStateStore[t];
554
554
  try {
555
- const r = a.getState().getValidationErrors(n.key);
556
- r && r.length > 0 && r.forEach(([$]) => {
557
- $ && $.startsWith(n.key) && P($);
555
+ const r = o.getState().getValidationErrors(n.key);
556
+ r && r.length > 0 && r.forEach(([w]) => {
557
+ w && w.startsWith(n.key) && R(w);
558
558
  });
559
559
  const u = n.zodSchema.safeParse(c);
560
560
  return u.success ? !0 : (u.error.errors.forEach((M) => {
561
- const N = M.path, R = M.message, C = [n.key, ...N].join(".");
562
- s(C, R), console.log(
563
- `Validation error at ${C}: ${R}`
561
+ const V = M.path, j = M.message, C = [n.key, ...V].join(".");
562
+ s(C, j), console.log(
563
+ `Validation error at ${C}: ${j}`
564
564
  );
565
- }), Vt(t), !1);
565
+ }), Mt(t), !1);
566
566
  } catch (r) {
567
567
  return console.error("Zod schema validation failed", r), !1;
568
568
  }
569
569
  };
570
- if (o === "_componentId") return f;
571
- if (o === "getComponents")
572
- return () => a().stateComponents.get(t);
573
- if (o === "getAllFormRefs")
574
- return () => at.getState().getFormRefsByStateKey(t);
575
- if (o === "_initialState")
576
- return a.getState().initialStateGlobal[t];
577
- if (o === "_serverState")
578
- return a.getState().serverState[t];
579
- if (o === "_isLoading")
580
- return a.getState().isLoadingGlobal[t];
581
- if (o === "revertToInitialState")
570
+ if (a === "_componentId") return f;
571
+ if (a === "getComponents")
572
+ return () => o().stateComponents.get(t);
573
+ if (a === "getAllFormRefs")
574
+ return () => ct.getState().getFormRefsByStateKey(t);
575
+ if (a === "_initialState")
576
+ return o.getState().initialStateGlobal[t];
577
+ if (a === "_serverState")
578
+ return o.getState().serverState[t];
579
+ if (a === "_isLoading")
580
+ return o.getState().isLoadingGlobal[t];
581
+ if (a === "revertToInitialState")
582
582
  return m.revertToInitialState;
583
- if (o === "updateInitialState") return m.updateInitialState;
584
- if (o === "removeValidation") return m.removeValidation;
583
+ if (a === "updateInitialState") return m.updateInitialState;
584
+ if (a === "removeValidation") return m.removeValidation;
585
585
  }
586
- if (o === "getFormRef")
587
- return () => at.getState().getFormRef(t + "." + e.join("."));
588
- if (o === "validationWrapper")
586
+ if (a === "getFormRef")
587
+ return () => ct.getState().getFormRef(t + "." + e.join("."));
588
+ if (a === "validationWrapper")
589
589
  return ({
590
590
  children: n,
591
591
  hideMessage: s
592
- }) => /* @__PURE__ */ et.jsx(
593
- mt,
592
+ }) => /* @__PURE__ */ at(
593
+ wt,
594
594
  {
595
595
  formOpts: s ? { validation: { message: "" } } : void 0,
596
596
  path: e,
597
- validationKey: a.getState().getInitialOptions(t)?.validation?.key || "",
597
+ validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
598
598
  stateKey: t,
599
599
  validIndices: v?.validIndices,
600
600
  children: n
601
601
  }
602
602
  );
603
- if (o === "_stateKey") return t;
604
- if (o === "_path") return e;
605
- if (o === "_isServerSynced") return m._isServerSynced;
606
- if (o === "update")
603
+ if (a === "_stateKey") return t;
604
+ if (a === "_path") return e;
605
+ if (a === "_isServerSynced") return m._isServerSynced;
606
+ if (a === "update")
607
607
  return (n, s) => {
608
608
  if (s?.debounce)
609
- St(() => {
610
- z(i, n, e, "");
611
- const c = a.getState().getNestedState(t, e);
609
+ Et(() => {
610
+ q(i, n, e, "");
611
+ const c = o.getState().getNestedState(t, e);
612
612
  s?.afterUpdate && s.afterUpdate(c);
613
613
  }, s.debounce);
614
614
  else {
615
- z(i, n, e, "");
616
- const c = a.getState().getNestedState(t, e);
615
+ q(i, n, e, "");
616
+ const c = o.getState().getNestedState(t, e);
617
617
  s?.afterUpdate && s.afterUpdate(c);
618
618
  }
619
- E(e);
619
+ _(e);
620
620
  };
621
- if (o === "formElement")
622
- return (n, s) => /* @__PURE__ */ et.jsx(
623
- yt,
621
+ if (a === "formElement")
622
+ return (n, s) => /* @__PURE__ */ at(
623
+ $t,
624
624
  {
625
625
  setState: i,
626
626
  stateKey: t,
@@ -629,52 +629,52 @@ function B(t, i, f, S) {
629
629
  formOpts: s
630
630
  }
631
631
  );
632
- const U = [...e, o], F = a.getState().getNestedState(t, U);
633
- return w(F, U, v);
632
+ const b = [...e, a], A = o.getState().getNestedState(t, b);
633
+ return E(A, b, v);
634
634
  }
635
- }, O = new Proxy(m, j);
636
- return g.set(T, {
637
- proxy: O,
635
+ }, U = new Proxy(m, F);
636
+ return g.set(h, {
637
+ proxy: U,
638
638
  stateVersion: I
639
- }), O;
639
+ }), U;
640
640
  }
641
- return w(
642
- a.getState().getNestedState(t, [])
641
+ return E(
642
+ o.getState().getNestedState(t, [])
643
643
  );
644
644
  }
645
- function H(t) {
646
- return _.createElement(Tt, { proxy: t });
645
+ function X(t) {
646
+ return H(Ut, { proxy: t });
647
647
  }
648
- function ht({
648
+ function Ot({
649
649
  proxy: t,
650
650
  rebuildStateShape: i
651
651
  }) {
652
- const f = a().getNestedState(t._stateKey, t._path);
652
+ const f = o().getNestedState(t._stateKey, t._path);
653
653
  return console.log("value", f), Array.isArray(f) ? i(
654
654
  f,
655
655
  t._path
656
656
  ).stateMapNoRender(
657
- (g, I, E, p, m) => t._mapFn(g, I, E, p, m)
657
+ (g, I, _, p, m) => t._mapFn(g, I, _, p, m)
658
658
  ) : null;
659
659
  }
660
- function Tt({
660
+ function Ut({
661
661
  proxy: t
662
662
  }) {
663
- const i = _.useRef(null), f = `${t._stateKey}-${t._path.join(".")}`;
664
- return _.useEffect(() => {
663
+ const i = z(null), f = `${t._stateKey}-${t._path.join(".")}`;
664
+ return K(() => {
665
665
  const S = i.current;
666
666
  if (!S || !S.parentElement) return;
667
- const g = S.parentElement, E = Array.from(g.childNodes).indexOf(S);
667
+ const g = S.parentElement, _ = Array.from(g.childNodes).indexOf(S);
668
668
  let p = g.getAttribute("data-parent-id");
669
669
  p || (p = `parent-${crypto.randomUUID()}`, g.setAttribute("data-parent-id", p));
670
- const w = {
670
+ const E = {
671
671
  instanceId: `instance-${crypto.randomUUID()}`,
672
672
  parentId: p,
673
- position: E,
673
+ position: _,
674
674
  effect: t._effect
675
675
  };
676
- a.getState().addSignalElement(f, w);
677
- const l = a.getState().getNestedState(t._stateKey, t._path);
676
+ o.getState().addSignalElement(f, E);
677
+ const l = o.getState().getNestedState(t._stateKey, t._path);
678
678
  let e;
679
679
  if (t._effect)
680
680
  try {
@@ -682,24 +682,24 @@ function Tt({
682
682
  "state",
683
683
  `return (${t._effect})(state)`
684
684
  )(l);
685
- } catch (T) {
686
- console.error("Error evaluating effect function during mount:", T), e = l;
685
+ } catch (h) {
686
+ console.error("Error evaluating effect function during mount:", h), e = l;
687
687
  }
688
688
  else
689
689
  e = l;
690
690
  e !== null && typeof e == "object" && (e = JSON.stringify(e));
691
691
  const v = document.createTextNode(String(e));
692
692
  S.replaceWith(v);
693
- }, [t._stateKey, t._path.join("."), t._effect]), _.createElement("span", {
693
+ }, [t._stateKey, t._path.join("."), t._effect]), H("span", {
694
694
  ref: i,
695
695
  style: { display: "none" },
696
696
  "data-signal-id": f
697
697
  });
698
698
  }
699
- function kt(t) {
700
- const i = _.useSyncExternalStore(
699
+ function Jt(t) {
700
+ const i = pt(
701
701
  (f) => {
702
- const S = a.getState().stateComponents.get(t._stateKey) || {
702
+ const S = o.getState().stateComponents.get(t._stateKey) || {
703
703
  components: /* @__PURE__ */ new Map()
704
704
  };
705
705
  return S.components.set(t._stateKey, {
@@ -707,15 +707,15 @@ function kt(t) {
707
707
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
708
708
  }), () => S.components.delete(t._stateKey);
709
709
  },
710
- () => a.getState().getNestedState(t._stateKey, t._path)
710
+ () => o.getState().getNestedState(t._stateKey, t._path)
711
711
  );
712
- return _.createElement("text", {}, String(i));
712
+ return H("text", {}, String(i));
713
713
  }
714
714
  export {
715
- H as $cogsSignal,
716
- kt as $cogsSignalStore,
717
- bt as createCogsState,
718
- Pt as notifyComponent,
719
- Nt as useCogsStateFn
715
+ X as $cogsSignal,
716
+ Jt as $cogsSignalStore,
717
+ qt as createCogsState,
718
+ zt as notifyComponent,
719
+ jt as useCogsStateFn
720
720
  };
721
721
  //# sourceMappingURL=CogsState.jsx.map