cogsbox-state 0.5.361 → 0.5.362

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,16 @@
1
1
  "use client";
2
- import { jsx as ye } from "react/jsx-runtime";
3
- import { useState as ee, useRef as J, useEffect as oe, useLayoutEffect as ie, useMemo as Ie, createElement as ce, useSyncExternalStore as Me, startTransition as Oe, useCallback as Te } from "react";
4
- import { transformStateFunc as Re, isDeepEqual as B, isFunction as X, getNestedValue as q, getDifferences as pe, debounce as Ue } from "./utility.js";
5
- import { pushFunc as ve, updateFn as se, cutFunc as ue, ValidationWrapper as je, FormControlComponent as Fe } from "./Functions.jsx";
6
- import De from "superjson";
7
- import { v4 as we } from "uuid";
2
+ import { jsx as Ie } from "react/jsx-runtime";
3
+ import { useState as ee, useRef as J, useEffect as oe, useLayoutEffect as ie, useMemo as pe, createElement as ce, useSyncExternalStore as Oe, startTransition as Re, useCallback as $e } from "react";
4
+ import { transformStateFunc as Ue, isDeepEqual as B, isFunction as X, getNestedValue as q, getDifferences as we, debounce as je } from "./utility.js";
5
+ import { pushFunc as ye, updateFn as se, cutFunc as ue, ValidationWrapper as Fe, FormControlComponent as De } from "./Functions.jsx";
6
+ import Le from "superjson";
7
+ import { v4 as Ee } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as r, formRefStore as $e } from "./store.js";
10
- import { useCogsConfig as xe } from "./CogsStateClient.jsx";
11
- import { applyPatch as Le } from "fast-json-patch";
9
+ import { getGlobalStore as r, formRefStore as ke } from "./store.js";
10
+ import { useCogsConfig as Pe } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Ge } from "fast-json-patch";
12
12
  import We from "react-use-measure";
13
- function ke(e, i) {
13
+ function Ne(e, i) {
14
14
  const S = r.getState().getInitialOptions, g = r.getState().setInitialStateOptions, y = S(e) || {};
15
15
  g(e, {
16
16
  ...y,
@@ -22,20 +22,20 @@ function be({
22
22
  options: i,
23
23
  initialOptionsPart: S
24
24
  }) {
25
- const g = re(e) || {}, y = S[e] || {}, C = r.getState().setInitialStateOptions, w = { ...y, ...g };
25
+ const g = re(e) || {}, y = S[e] || {}, b = r.getState().setInitialStateOptions, w = { ...y, ...g };
26
26
  let I = !1;
27
27
  if (i)
28
28
  for (const a in i)
29
29
  w.hasOwnProperty(a) ? (a == "localStorage" && i[a] && w[a].key !== i[a]?.key && (I = !0, w[a] = i[a]), a == "initialState" && i[a] && w[a] !== i[a] && // Different references
30
30
  !B(w[a], i[a]) && (I = !0, w[a] = i[a])) : (I = !0, w[a] = i[a]);
31
- I && C(e, w);
31
+ I && b(e, w);
32
32
  }
33
- function dt(e, { formElements: i, validation: S }) {
33
+ function ut(e, { formElements: i, validation: S }) {
34
34
  return { initialState: e, formElements: i, validation: S };
35
35
  }
36
- const ut = (e, i) => {
36
+ const gt = (e, i) => {
37
37
  let S = e;
38
- const [g, y] = Re(S);
38
+ const [g, y] = Ue(S);
39
39
  (Object.keys(y).length > 0 || i && Object.keys(i).length > 0) && Object.keys(y).forEach((I) => {
40
40
  y[I] = y[I] || {}, y[I].formElements = {
41
41
  ...i?.formElements,
@@ -45,14 +45,14 @@ const ut = (e, i) => {
45
45
  // State-specific overrides
46
46
  }, re(I) || r.getState().setInitialStateOptions(I, y[I]);
47
47
  }), r.getState().setInitialStates(g), r.getState().setCreatedState(g);
48
- const C = (I, a) => {
49
- const [v] = ee(a?.componentId ?? we());
48
+ const b = (I, a) => {
49
+ const [v] = ee(a?.componentId ?? Ee());
50
50
  be({
51
51
  stateKey: I,
52
52
  options: a,
53
53
  initialOptionsPart: y
54
54
  });
55
- const n = r.getState().cogsStateStore[I] || g[I], f = a?.modifyState ? a.modifyState(n) : n, [G, U] = Ye(
55
+ const n = r.getState().cogsStateStore[I] || g[I], f = a?.modifyState ? a.modifyState(n) : n, [W, U] = Ze(
56
56
  f,
57
57
  {
58
58
  stateKey: I,
@@ -71,20 +71,20 @@ const ut = (e, i) => {
71
71
  return U;
72
72
  };
73
73
  function w(I, a) {
74
- be({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && qe(I, a), me(I);
74
+ be({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && Je(I, a), me(I);
75
75
  }
76
- return { useCogsState: C, setCogsOptions: w };
76
+ return { useCogsState: b, setCogsOptions: w };
77
77
  }, {
78
78
  setUpdaterState: ge,
79
79
  setState: te,
80
80
  getInitialOptions: re,
81
81
  getKeyState: Ve,
82
- getValidationErrors: Ge,
83
- setStateLog: He,
84
- updateInitialStateGlobal: Ee,
85
- addValidationError: ze,
82
+ getValidationErrors: He,
83
+ setStateLog: ze,
84
+ updateInitialStateGlobal: Ae,
85
+ addValidationError: Be,
86
86
  removeValidationError: Z,
87
- setServerSyncActions: Be
87
+ setServerSyncActions: qe
88
88
  } = r.getState(), Ce = (e, i, S, g, y) => {
89
89
  S?.log && console.log(
90
90
  "saving to localstorage",
@@ -92,9 +92,9 @@ const ut = (e, i) => {
92
92
  S.localStorage?.key,
93
93
  g
94
94
  );
95
- const C = X(S?.localStorage?.key) ? S.localStorage?.key(e) : S?.localStorage?.key;
96
- if (C && g) {
97
- const w = `${g}-${i}-${C}`;
95
+ const b = X(S?.localStorage?.key) ? S.localStorage?.key(e) : S?.localStorage?.key;
96
+ if (b && g) {
97
+ const w = `${g}-${i}-${b}`;
98
98
  let I;
99
99
  try {
100
100
  I = Se(w)?.lastSyncedWithServer;
@@ -104,7 +104,7 @@ const ut = (e, i) => {
104
104
  state: e,
105
105
  lastUpdated: Date.now(),
106
106
  lastSyncedWithServer: y ?? I
107
- }, v = De.serialize(a);
107
+ }, v = Le.serialize(a);
108
108
  window.localStorage.setItem(
109
109
  w,
110
110
  JSON.stringify(v.json)
@@ -118,28 +118,28 @@ const ut = (e, i) => {
118
118
  } catch (i) {
119
119
  return console.error("Error loading from localStorage:", i), null;
120
120
  }
121
- }, qe = (e, i) => {
122
- const S = r.getState().cogsStateStore[e], { sessionId: g } = xe(), y = X(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
121
+ }, Je = (e, i) => {
122
+ const S = r.getState().cogsStateStore[e], { sessionId: g } = Pe(), y = X(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
123
123
  if (y && g) {
124
- const C = Se(
124
+ const b = Se(
125
125
  `${g}-${e}-${y}`
126
126
  );
127
- if (C && C.lastUpdated > (C.lastSyncedWithServer || 0))
128
- return te(e, C.state), me(e), !0;
127
+ if (b && b.lastUpdated > (b.lastSyncedWithServer || 0))
128
+ return te(e, b.state), me(e), !0;
129
129
  }
130
130
  return !1;
131
- }, Pe = (e, i, S, g, y, C) => {
131
+ }, _e = (e, i, S, g, y, b) => {
132
132
  const w = {
133
133
  initialState: i,
134
134
  updaterState: fe(
135
135
  e,
136
136
  g,
137
137
  y,
138
- C
138
+ b
139
139
  ),
140
140
  state: S
141
141
  };
142
- Ee(e, w.initialState), ge(e, w.updaterState), te(e, w.state);
142
+ Ae(e, w.initialState), ge(e, w.updaterState), te(e, w.state);
143
143
  }, me = (e) => {
144
144
  const i = r.getState().stateComponents.get(e);
145
145
  if (!i) return;
@@ -149,7 +149,7 @@ const ut = (e, i) => {
149
149
  }), queueMicrotask(() => {
150
150
  S.forEach((g) => g());
151
151
  });
152
- }, gt = (e, i) => {
152
+ }, ft = (e, i) => {
153
153
  const S = r.getState().stateComponents.get(e);
154
154
  if (S) {
155
155
  const g = `${e}////${i}`, y = S.components.get(g);
@@ -157,7 +157,7 @@ const ut = (e, i) => {
157
157
  return;
158
158
  y && y.forceUpdate();
159
159
  }
160
- }, Je = (e, i, S, g) => {
160
+ }, Ye = (e, i, S, g) => {
161
161
  switch (e) {
162
162
  case "update":
163
163
  return {
@@ -180,12 +180,12 @@ const ut = (e, i) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function Ye(e, {
183
+ function Ze(e, {
184
184
  stateKey: i,
185
185
  serverSync: S,
186
186
  localStorage: g,
187
187
  formElements: y,
188
- reactiveDeps: C,
188
+ reactiveDeps: b,
189
189
  reactiveType: w,
190
190
  componentId: I,
191
191
  initialState: a,
@@ -193,9 +193,9 @@ function Ye(e, {
193
193
  dependencies: n,
194
194
  serverState: f
195
195
  } = {}) {
196
- const [G, U] = ee({}), { sessionId: j } = xe();
196
+ const [W, U] = ee({}), { sessionId: j } = Pe();
197
197
  let H = !i;
198
- const [m] = ee(i ?? we()), l = r.getState().stateLog[m], le = J(/* @__PURE__ */ new Set()), Q = J(I ?? we()), _ = J(
198
+ const [m] = ee(i ?? Ee()), l = r.getState().stateLog[m], le = J(/* @__PURE__ */ new Set()), Q = J(I ?? Ee()), _ = J(
199
199
  null
200
200
  );
201
201
  _.current = re(m) ?? null, oe(() => {
@@ -212,7 +212,7 @@ function Ye(e, {
212
212
  }
213
213
  }, [v]), oe(() => {
214
214
  if (a) {
215
- ke(m, {
215
+ Ne(m, {
216
216
  initialState: a
217
217
  });
218
218
  const t = _.current, s = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, c = r.getState().initialStateGlobal[m];
@@ -222,8 +222,8 @@ function Ye(e, {
222
222
  const p = X(t?.localStorage?.key) ? t?.localStorage?.key(a) : t?.localStorage?.key;
223
223
  p && j && (d = Se(`${j}-${m}-${p}`));
224
224
  let E = a, A = !1;
225
- const P = s ? Date.now() : 0, x = d?.lastUpdated || 0, M = d?.lastSyncedWithServer || 0;
226
- s && P > x ? (E = t.serverState.data, A = !0) : d && x > M && (E = d.state, t?.localStorage?.onChange && t?.localStorage?.onChange(E)), r.getState().initializeShadowState(m, a), Pe(
225
+ const V = s ? Date.now() : 0, P = d?.lastUpdated || 0, M = d?.lastSyncedWithServer || 0;
226
+ s && V > P ? (E = t.serverState.data, A = !0) : d && P > M && (E = d.state, t?.localStorage?.onChange && t?.localStorage?.onChange(E)), r.getState().initializeShadowState(m, a), _e(
227
227
  m,
228
228
  a,
229
229
  E,
@@ -238,7 +238,7 @@ function Ye(e, {
238
238
  f?.data,
239
239
  ...n || []
240
240
  ]), ie(() => {
241
- H && ke(m, {
241
+ H && Ne(m, {
242
242
  serverSync: S,
243
243
  formElements: y,
244
244
  initialState: a,
@@ -252,7 +252,7 @@ function Ye(e, {
252
252
  forceUpdate: () => U({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: C || void 0,
255
+ depsFunction: b || void 0,
256
256
  reactiveType: w ?? ["component", "deps"]
257
257
  }), r.getState().stateComponents.set(m, o), U({}), () => {
258
258
  o && (o.components.delete(t), o.components.size === 0 && r.getState().stateComponents.delete(m));
@@ -267,26 +267,26 @@ function Ye(e, {
267
267
  te(m, (d) => {
268
268
  const p = X(t) ? t(d) : t, E = `${m}-${o.join(".")}`;
269
269
  if (E) {
270
- let $ = !1, N = h.signalDomElements.get(E);
271
- if ((!N || N.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
- const V = o.slice(0, -1), L = q(p, V);
270
+ let T = !1, C = h.signalDomElements.get(E);
271
+ if ((!C || C.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
+ const x = o.slice(0, -1), L = q(p, x);
273
273
  if (Array.isArray(L)) {
274
- $ = !0;
275
- const k = `${m}-${V.join(".")}`;
276
- N = h.signalDomElements.get(k);
274
+ T = !0;
275
+ const k = `${m}-${x.join(".")}`;
276
+ C = h.signalDomElements.get(k);
277
277
  }
278
278
  }
279
- if (N) {
280
- const V = $ ? q(p, o.slice(0, -1)) : q(p, o);
281
- N.forEach(({ parentId: L, position: k, effect: F }) => {
279
+ if (C) {
280
+ const x = T ? q(p, o.slice(0, -1)) : q(p, o);
281
+ C.forEach(({ parentId: L, position: k, effect: F }) => {
282
282
  const O = document.querySelector(
283
283
  `[data-parent-id="${L}"]`
284
284
  );
285
285
  if (O) {
286
- const T = Array.from(O.childNodes);
287
- if (T[k]) {
288
- const b = F ? new Function("state", `return (${F})(state)`)(V) : V;
289
- T[k].textContent = String(b);
286
+ const $ = Array.from(O.childNodes);
287
+ if ($[k]) {
288
+ const N = F ? new Function("state", `return (${F})(state)`)(x) : x;
289
+ $[k].textContent = String(N);
290
290
  }
291
291
  }
292
292
  });
@@ -298,22 +298,22 @@ function Ye(e, {
298
298
  const A = o.slice(0, o.length - 1);
299
299
  s.updateType === "cut" && _.current?.validation?.key && Z(
300
300
  _.current?.validation?.key + "." + A.join(".")
301
- ), s.updateType === "insert" && _.current?.validation?.key && Ge(
301
+ ), s.updateType === "insert" && _.current?.validation?.key && He(
302
302
  _.current?.validation?.key + "." + A.join(".")
303
- ).filter(([N, V]) => {
304
- let L = N?.split(".").length;
305
- if (N == A.join(".") && L == A.length - 1) {
306
- let k = N + "." + A;
307
- Z(N), ze(k, V);
303
+ ).filter(([C, x]) => {
304
+ let L = C?.split(".").length;
305
+ if (C == A.join(".") && L == A.length - 1) {
306
+ let k = C + "." + A;
307
+ Z(C), Be(k, x);
308
308
  }
309
309
  });
310
- const P = h.stateComponents.get(m);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", P), P) {
312
- const $ = pe(d, p), N = new Set($), V = s.updateType === "update" ? o.join(".") : o.slice(0, -1).join(".") || "";
310
+ const V = h.stateComponents.get(m);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", V), V) {
312
+ const T = we(d, p), C = new Set(T), x = s.updateType === "update" ? o.join(".") : o.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  L,
315
315
  k
316
- ] of P.components.entries()) {
316
+ ] of V.components.entries()) {
317
317
  let F = !1;
318
318
  const O = Array.isArray(k.reactiveType) ? k.reactiveType : [k.reactiveType || "component"];
319
319
  if (console.log("component", k), !O.includes("none")) {
@@ -321,81 +321,81 @@ function Ye(e, {
321
321
  k.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (O.includes("component") && ((k.paths.has(V) || k.paths.has("")) && (F = !0), !F))
325
- for (const T of N) {
326
- let b = T;
324
+ if (O.includes("component") && ((k.paths.has(x) || k.paths.has("")) && (F = !0), !F))
325
+ for (const $ of C) {
326
+ let N = $;
327
327
  for (; ; ) {
328
- if (k.paths.has(b)) {
328
+ if (k.paths.has(N)) {
329
329
  F = !0;
330
330
  break;
331
331
  }
332
- const D = b.lastIndexOf(".");
332
+ const D = N.lastIndexOf(".");
333
333
  if (D !== -1) {
334
- const R = b.substring(
334
+ const R = N.substring(
335
335
  0,
336
336
  D
337
337
  );
338
338
  if (!isNaN(
339
- Number(b.substring(D + 1))
339
+ Number(N.substring(D + 1))
340
340
  ) && k.paths.has(R)) {
341
341
  F = !0;
342
342
  break;
343
343
  }
344
- b = R;
344
+ N = R;
345
345
  } else
346
- b = "";
347
- if (b === "")
346
+ N = "";
347
+ if (N === "")
348
348
  break;
349
349
  }
350
350
  if (F) break;
351
351
  }
352
352
  if (!F && O.includes("deps") && k.depsFunction) {
353
- const T = k.depsFunction(p);
354
- let b = !1;
355
- typeof T == "boolean" ? T && (b = !0) : B(k.deps, T) || (k.deps = T, b = !0), b && (F = !0);
353
+ const $ = k.depsFunction(p);
354
+ let N = !1;
355
+ typeof $ == "boolean" ? $ && (N = !0) : B(k.deps, $) || (k.deps = $, N = !0), N && (F = !0);
356
356
  }
357
357
  F && k.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
- const x = Date.now();
362
- o = o.map(($, N) => {
363
- const V = o.slice(0, -1), L = q(p, V);
364
- return N === o.length - 1 && ["insert", "cut"].includes(s.updateType) ? (L.length - 1).toString() : $;
361
+ const P = Date.now();
362
+ o = o.map((T, C) => {
363
+ const x = o.slice(0, -1), L = q(p, x);
364
+ return C === o.length - 1 && ["insert", "cut"].includes(s.updateType) ? (L.length - 1).toString() : T;
365
365
  });
366
- const { oldValue: M, newValue: W } = Je(
366
+ const { oldValue: M, newValue: G } = Ye(
367
367
  s.updateType,
368
368
  d,
369
369
  p,
370
370
  o
371
371
  ), z = {
372
- timeStamp: x,
372
+ timeStamp: P,
373
373
  stateKey: m,
374
374
  path: o,
375
375
  updateType: s.updateType,
376
376
  status: "new",
377
377
  oldValue: M,
378
- newValue: W
378
+ newValue: G
379
379
  };
380
380
  switch (s.updateType) {
381
381
  case "update":
382
382
  h.updateShadowAtPath(m, o, p);
383
383
  break;
384
384
  case "insert":
385
- const $ = o.slice(0, -1);
386
- h.insertShadowArrayElement(m, $, W);
385
+ const T = o.slice(0, -1);
386
+ h.insertShadowArrayElement(m, T, G);
387
387
  break;
388
388
  case "cut":
389
- const N = o.slice(0, -1), V = parseInt(o[o.length - 1]);
390
- h.removeShadowArrayElement(m, N, V);
389
+ const C = o.slice(0, -1), x = parseInt(o[o.length - 1]);
390
+ h.removeShadowArrayElement(m, C, x);
391
391
  break;
392
392
  }
393
- if (He(m, ($) => {
394
- const V = [...$ ?? [], z].reduce((L, k) => {
393
+ if (ze(m, (T) => {
394
+ const x = [...T ?? [], z].reduce((L, k) => {
395
395
  const F = `${k.stateKey}:${JSON.stringify(k.path)}`, O = L.get(F);
396
396
  return O ? (O.timeStamp = Math.max(O.timeStamp, k.timeStamp), O.newValue = k.newValue, O.oldValue = O.oldValue ?? k.oldValue, O.updateType = k.updateType) : L.set(F, { ...k }), L;
397
397
  }, /* @__PURE__ */ new Map());
398
- return Array.from(V.values());
398
+ return Array.from(x.values());
399
399
  }), Ce(
400
400
  p,
401
401
  m,
@@ -405,11 +405,11 @@ function Ye(e, {
405
405
  updateLog: l,
406
406
  update: z
407
407
  }), _.current?.serverSync) {
408
- const $ = h.serverState[m], N = _.current?.serverSync;
409
- Be(m, {
410
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: p }),
411
- rollBackState: $,
412
- actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
408
+ const T = h.serverState[m], C = _.current?.serverSync;
409
+ qe(m, {
410
+ syncKey: typeof C.syncKey == "string" ? C.syncKey : C.syncKey({ state: p }),
411
+ rollBackState: T,
412
+ actionTimeStamp: Date.now() + (C.debounce ?? 3e3),
413
413
  status: "waiting"
414
414
  });
415
415
  }
@@ -424,8 +424,8 @@ function Ye(e, {
424
424
  Q.current,
425
425
  j
426
426
  )
427
- ), r.getState().cogsStateStore[m] || te(m, e), r.getState().initialStateGlobal[m] || Ee(m, e));
428
- const u = Ie(() => fe(
427
+ ), r.getState().cogsStateStore[m] || te(m, e), r.getState().initialStateGlobal[m] || Ae(m, e));
428
+ const u = pe(() => fe(
429
429
  m,
430
430
  ae,
431
431
  Q.current,
@@ -435,12 +435,12 @@ function Ye(e, {
435
435
  }
436
436
  function fe(e, i, S, g) {
437
437
  const y = /* @__PURE__ */ new Map();
438
- let C = 0;
438
+ let b = 0;
439
439
  const w = (v) => {
440
440
  const n = v.join(".");
441
441
  for (const [f] of y)
442
442
  (f === n || f.startsWith(n + ".")) && y.delete(f);
443
- C++;
443
+ b++;
444
444
  }, I = {
445
445
  removeValidation: (v) => {
446
446
  v?.validationKey && Z(v.validationKey);
@@ -449,24 +449,24 @@ function fe(e, i, S, g) {
449
449
  const n = r.getState().getInitialOptions(e)?.validation;
450
450
  n?.key && Z(n?.key), v?.validationKey && Z(v.validationKey);
451
451
  const f = r.getState().initialStateGlobal[e];
452
- r.getState().clearSelectedIndexesForState(e), y.clear(), C++;
453
- const G = a(f, []), U = re(e), j = X(U?.localStorage?.key) ? U?.localStorage?.key(f) : U?.localStorage?.key, H = `${g}-${e}-${j}`;
454
- H && localStorage.removeItem(H), ge(e, G), te(e, f);
452
+ r.getState().clearSelectedIndexesForState(e), y.clear(), b++;
453
+ const W = a(f, []), U = re(e), j = X(U?.localStorage?.key) ? U?.localStorage?.key(f) : U?.localStorage?.key, H = `${g}-${e}-${j}`;
454
+ H && localStorage.removeItem(H), ge(e, W), te(e, f);
455
455
  const m = r.getState().stateComponents.get(e);
456
456
  return m && m.components.forEach((l) => {
457
457
  l.forceUpdate();
458
458
  }), f;
459
459
  },
460
460
  updateInitialState: (v) => {
461
- y.clear(), C++;
461
+ y.clear(), b++;
462
462
  const n = fe(
463
463
  e,
464
464
  i,
465
465
  S,
466
466
  g
467
- ), f = r.getState().initialStateGlobal[e], G = re(e), U = X(G?.localStorage?.key) ? G?.localStorage?.key(f) : G?.localStorage?.key, j = `${g}-${e}-${U}`;
468
- return localStorage.getItem(j) && localStorage.removeItem(j), Oe(() => {
469
- Ee(e, v), r.getState().initializeShadowState(e, v), ge(e, n), te(e, v);
467
+ ), f = r.getState().initialStateGlobal[e], W = re(e), U = X(W?.localStorage?.key) ? W?.localStorage?.key(f) : W?.localStorage?.key, j = `${g}-${e}-${U}`;
468
+ return localStorage.getItem(j) && localStorage.removeItem(j), Re(() => {
469
+ Ae(e, v), r.getState().initializeShadowState(e, v), ge(e, n), te(e, v);
470
470
  const H = r.getState().stateComponents.get(e);
471
471
  H && H.components.forEach((m) => {
472
472
  m.forceUpdate();
@@ -484,8 +484,8 @@ function fe(e, i, S, g) {
484
484
  }
485
485
  };
486
486
  function a(v, n = [], f) {
487
- const G = n.map(String).join(".");
488
- y.get(G);
487
+ const W = n.map(String).join(".");
488
+ y.get(W);
489
489
  const U = function() {
490
490
  return r().getNestedState(e, n);
491
491
  };
@@ -540,7 +540,7 @@ function fe(e, i, S, g) {
540
540
  }
541
541
  }
542
542
  if (l === "getDifferences")
543
- return () => pe(
543
+ return () => we(
544
544
  r.getState().cogsStateStore[e],
545
545
  r.getState().initialStateGlobal[e]
546
546
  );
@@ -625,50 +625,50 @@ function fe(e, i, S, g) {
625
625
  } = t, d = J(null), [p, E] = ee({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), A = J(c), P = J(!1), x = J(0), [M, W] = ee(0);
628
+ }), A = J(c), V = J(!1), P = J(0), [M, G] = ee(0);
629
629
  oe(() => r.getState().subscribeToShadowState(e, () => {
630
- W((b) => b + 1);
630
+ G((N) => N + 1);
631
631
  }), [e]);
632
632
  const z = r().getNestedState(
633
633
  e,
634
634
  n
635
- ), $ = z.length, { totalHeight: N, positions: V } = Ie(() => {
636
- const T = r.getState().getShadowMetadata(e, n) || [];
637
- let b = 0;
635
+ ), T = z.length, { totalHeight: C, positions: x } = pe(() => {
636
+ const $ = r.getState().getShadowMetadata(e, n) || [];
637
+ let N = 0;
638
638
  const D = [];
639
- for (let R = 0; R < $; R++) {
640
- D[R] = b;
641
- const Y = T[R]?.virtualizer?.itemHeight;
642
- b += Y || o;
639
+ for (let R = 0; R < T; R++) {
640
+ D[R] = N;
641
+ const Y = $[R]?.virtualizer?.itemHeight;
642
+ N += Y || o;
643
643
  }
644
- return { totalHeight: b, positions: D };
644
+ return { totalHeight: N, positions: D };
645
645
  }, [
646
- $,
646
+ T,
647
647
  e,
648
648
  n.join("."),
649
649
  o,
650
650
  M
651
- ]), L = Ie(() => {
652
- const T = Math.max(0, p.startIndex), b = Math.min($, p.endIndex), D = Array.from(
653
- { length: b - T },
654
- (Y, ne) => T + ne
651
+ ]), L = pe(() => {
652
+ const $ = Math.max(0, p.startIndex), N = Math.min(T, p.endIndex), D = Array.from(
653
+ { length: N - $ },
654
+ (Y, ne) => $ + ne
655
655
  ), R = D.map((Y) => z[Y]);
656
656
  return a(R, n, {
657
657
  ...f,
658
658
  validIndices: D
659
659
  });
660
- }, [p.startIndex, p.endIndex, z, $]);
660
+ }, [p.startIndex, p.endIndex, z, T]);
661
661
  ie(() => {
662
- const T = $ > x.current;
663
- A.current && T && (console.log(
662
+ const $ = T > P.current;
663
+ A.current && $ && (console.log(
664
664
  "PHASE 1: Auto-scroll needed. Setting range to render the last item."
665
665
  ), E({
666
- startIndex: Math.max(0, $ - 10 - s),
667
- endIndex: $
668
- })), x.current = $;
669
- }, [$]), ie(() => {
670
- const T = d.current, b = p.endIndex === $ && $ > 0;
671
- if (!T || !A.current || !b)
666
+ startIndex: Math.max(0, T - 10 - s),
667
+ endIndex: T
668
+ })), P.current = T;
669
+ }, [T]), ie(() => {
670
+ const $ = d.current, N = p.endIndex === T && T > 0;
671
+ if (!$ || !A.current || !N)
672
672
  return;
673
673
  console.log(
674
674
  "PHASE 2: Range is at the end. Starting the measurement loop."
@@ -676,66 +676,66 @@ function fe(e, i, S, g) {
676
676
  let D = 0;
677
677
  const R = setInterval(() => {
678
678
  D++, console.log(`LOOP ${D}: Checking last item...`);
679
- const Y = $ - 1, K = (r.getState().getShadowMetadata(e, n) || [])[Y]?.virtualizer?.itemHeight || 0;
679
+ const Y = T - 1, K = (r.getState().getShadowMetadata(e, n) || [])[Y]?.virtualizer?.itemHeight || 0;
680
680
  K > 0 ? (console.log(
681
681
  `%cSUCCESS: Last item height is ${K}. Scrolling now.`,
682
682
  "color: green; font-weight: bold;"
683
- ), clearInterval(R), P.current = !0, T.scrollTo({
684
- top: T.scrollHeight,
683
+ ), clearInterval(R), V.current = !0, $.scrollTo({
684
+ top: $.scrollHeight,
685
685
  behavior: "smooth"
686
686
  }), setTimeout(() => {
687
- P.current = !1;
687
+ V.current = !1;
688
688
  }, 1e3)) : D > 20 ? (console.error(
689
689
  "LOOP TIMEOUT: Last item was never measured. Stopping loop."
690
690
  ), clearInterval(R)) : console.log("...WAITING. Height is not ready.");
691
691
  }, 100);
692
692
  return () => clearInterval(R);
693
- }, [p.endIndex, $, V]), oe(() => {
694
- const T = d.current;
695
- if (!T) return;
693
+ }, [p.endIndex, T, x]), oe(() => {
694
+ const $ = d.current;
695
+ if (!$) return;
696
696
  console.log(
697
697
  "DEPENDENCY CHANGE: Resetting scroll lock and initial view."
698
698
  ), A.current = c;
699
- const b = () => {
700
- const { scrollTop: R, clientHeight: Y } = T;
701
- let ne = 0, K = $ - 1;
699
+ const N = () => {
700
+ const { scrollTop: R, clientHeight: Y } = $;
701
+ let ne = 0, K = T - 1;
702
702
  for (; ne <= K; ) {
703
- const he = Math.floor((ne + K) / 2);
704
- V[he] < R ? ne = he + 1 : K = he - 1;
703
+ const ve = Math.floor((ne + K) / 2);
704
+ x[ve] < R ? ne = ve + 1 : K = ve - 1;
705
705
  }
706
- const Ae = Math.max(0, K - s);
707
- let de = Ae;
708
- const _e = R + Y;
709
- for (; de < $ && V[de] < _e; )
706
+ const he = Math.max(0, K - s);
707
+ let de = he;
708
+ const Me = R + Y;
709
+ for (; de < T && x[de] < Me; )
710
710
  de++;
711
- E({
712
- startIndex: Ae,
713
- endIndex: Math.min($, de + s)
714
- });
711
+ const Te = Math.min(T, de + s);
712
+ console.log(
713
+ `RANGE UPDATE: Start: ${he}, End: ${Te}, Total: ${T}`
714
+ ), E({ startIndex: he, endIndex: Te });
715
715
  }, D = () => {
716
- if (P.current) return;
717
- T.scrollHeight - T.scrollTop - T.clientHeight < 1 || (A.current = !1), b();
716
+ if (V.current) return;
717
+ $.scrollHeight - $.scrollTop - $.clientHeight < 1 || (A.current = !1), N();
718
718
  };
719
- return T.addEventListener("scroll", D, {
719
+ return $.addEventListener("scroll", D, {
720
720
  passive: !0
721
- }), b(), () => T.removeEventListener("scroll", D);
722
- }, [...h]);
723
- const k = Te(
724
- (T = "smooth") => {
721
+ }), N(), () => $.removeEventListener("scroll", D);
722
+ }, [...h, T, x]);
723
+ const k = $e(
724
+ ($ = "smooth") => {
725
725
  d.current && (A.current = !0, d.current.scrollTo({
726
726
  top: d.current.scrollHeight,
727
- behavior: T
727
+ behavior: $
728
728
  }));
729
729
  },
730
730
  []
731
- ), F = Te(
732
- (T, b = "smooth") => {
733
- d.current && V[T] !== void 0 && (A.current = !1, d.current.scrollTo({
734
- top: V[T],
735
- behavior: b
731
+ ), F = $e(
732
+ ($, N = "smooth") => {
733
+ d.current && x[$] !== void 0 && (A.current = !1, d.current.scrollTo({
734
+ top: x[$],
735
+ behavior: N
736
736
  }));
737
737
  },
738
- [V]
738
+ [x]
739
739
  ), O = {
740
740
  outer: {
741
741
  ref: d,
@@ -743,13 +743,13 @@ function fe(e, i, S, g) {
743
743
  },
744
744
  inner: {
745
745
  style: {
746
- height: `${N}px`,
746
+ height: `${C}px`,
747
747
  position: "relative"
748
748
  }
749
749
  },
750
750
  list: {
751
751
  style: {
752
- transform: `translateY(${V[p.startIndex] || 0}px)`
752
+ transform: `translateY(${x[p.startIndex] || 0}px)`
753
753
  }
754
754
  }
755
755
  };
@@ -791,19 +791,19 @@ function fe(e, i, S, g) {
791
791
  const d = o[c], p = [...n, c.toString()], E = a(d, p, f);
792
792
  return t(d, E, {
793
793
  register: () => {
794
- const [, P] = ee({}), x = `${S}-${n.join(".")}-${c}`;
794
+ const [, V] = ee({}), P = `${S}-${n.join(".")}-${c}`;
795
795
  ie(() => {
796
- const M = `${e}////${x}`, W = r.getState().stateComponents.get(e) || {
796
+ const M = `${e}////${P}`, G = r.getState().stateComponents.get(e) || {
797
797
  components: /* @__PURE__ */ new Map()
798
798
  };
799
- return W.components.set(M, {
800
- forceUpdate: () => P({}),
799
+ return G.components.set(M, {
800
+ forceUpdate: () => V({}),
801
801
  paths: /* @__PURE__ */ new Set([p.join(".")])
802
- }), r.getState().stateComponents.set(e, W), () => {
802
+ }), r.getState().stateComponents.set(e, G), () => {
803
803
  const z = r.getState().stateComponents.get(e);
804
804
  z && z.components.delete(M);
805
805
  };
806
- }, [e, x]);
806
+ }, [e, P]);
807
807
  },
808
808
  index: h,
809
809
  originalIndex: c
@@ -827,7 +827,7 @@ function fe(e, i, S, g) {
827
827
  );
828
828
  });
829
829
  if (l === "$stateMap")
830
- return (t) => ce(Ze, {
830
+ return (t) => ce(Xe, {
831
831
  proxy: {
832
832
  _stateKey: e,
833
833
  _path: n,
@@ -841,7 +841,7 @@ function fe(e, i, S, g) {
841
841
  const o = r.getState().getNestedState(e, n);
842
842
  return Array.isArray(o) ? (f?.validIndices || Array.from({ length: o.length }, (c, h) => h)).map((c, h) => {
843
843
  const d = o[c], p = [...n, c.toString()], E = a(d, p, f), A = `${S}-${n.join(".")}-${c}`;
844
- return ce(Qe, {
844
+ return ce(Ke, {
845
845
  key: c,
846
846
  stateKey: e,
847
847
  itemComponentId: A,
@@ -861,7 +861,7 @@ function fe(e, i, S, g) {
861
861
  if (l === "stateFlattenOn")
862
862
  return (t) => {
863
863
  const o = v;
864
- y.clear(), C++;
864
+ y.clear(), b++;
865
865
  const s = o.flatMap(
866
866
  (c) => c[t] ?? []
867
867
  );
@@ -884,7 +884,7 @@ function fe(e, i, S, g) {
884
884
  return a(s, c);
885
885
  };
886
886
  if (l === "insert")
887
- return (t) => (w(n), ve(i, t, n, e), a(
887
+ return (t) => (w(n), ye(i, t, n, e), a(
888
888
  r.getState().getNestedState(e, n),
889
889
  n
890
890
  ));
@@ -894,18 +894,18 @@ function fe(e, i, S, g) {
894
894
  let d = null;
895
895
  if (!c.some((E) => {
896
896
  if (o) {
897
- const P = o.every(
898
- (x) => B(E[x], h[x])
897
+ const V = o.every(
898
+ (P) => B(E[P], h[P])
899
899
  );
900
- return P && (d = E), P;
900
+ return V && (d = E), V;
901
901
  }
902
902
  const A = B(E, h);
903
903
  return A && (d = E), A;
904
904
  }))
905
- w(n), ve(i, h, n, e);
905
+ w(n), ye(i, h, n, e);
906
906
  else if (s && d) {
907
907
  const E = s(d), A = c.map(
908
- (P) => B(P, d) ? E : P
908
+ (V) => B(V, d) ? E : V
909
909
  );
910
910
  w(n), se(i, A, n);
911
911
  }
@@ -926,7 +926,7 @@ function fe(e, i, S, g) {
926
926
  if (l === "toggleByValue")
927
927
  return (t) => {
928
928
  const o = v.findIndex((s) => s === t);
929
- o > -1 ? ue(i, n, e, o) : ve(i, t, n, e);
929
+ o > -1 ? ue(i, n, e, o) : ye(i, t, n, e);
930
930
  };
931
931
  if (l === "stateFind")
932
932
  return (t) => {
@@ -967,13 +967,13 @@ function fe(e, i, S, g) {
967
967
  return r.getState().getNestedState(e, n);
968
968
  };
969
969
  if (l === "$derive")
970
- return (u) => Ne({
970
+ return (u) => xe({
971
971
  _stateKey: e,
972
972
  _path: n,
973
973
  _effect: u.toString()
974
974
  });
975
975
  if (l === "$get")
976
- return () => Ne({
976
+ return () => xe({
977
977
  _stateKey: e,
978
978
  _path: n
979
979
  });
@@ -1008,8 +1008,8 @@ function fe(e, i, S, g) {
1008
1008
  if (n.length == 0) {
1009
1009
  if (l === "applyJsonPatch")
1010
1010
  return (u) => {
1011
- const t = r.getState().cogsStateStore[e], s = Le(t, u).newDocument;
1012
- Pe(
1011
+ const t = r.getState().cogsStateStore[e], s = Ge(t, u).newDocument;
1012
+ _e(
1013
1013
  e,
1014
1014
  r.getState().initialStateGlobal[e],
1015
1015
  s,
@@ -1019,55 +1019,55 @@ function fe(e, i, S, g) {
1019
1019
  );
1020
1020
  const c = r.getState().stateComponents.get(e);
1021
1021
  if (c) {
1022
- const h = pe(t, s), d = new Set(h);
1022
+ const h = we(t, s), d = new Set(h);
1023
1023
  for (const [
1024
1024
  p,
1025
1025
  E
1026
1026
  ] of c.components.entries()) {
1027
1027
  let A = !1;
1028
- const P = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
1029
- if (!P.includes("none")) {
1030
- if (P.includes("all")) {
1028
+ const V = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
1029
+ if (!V.includes("none")) {
1030
+ if (V.includes("all")) {
1031
1031
  E.forceUpdate();
1032
1032
  continue;
1033
1033
  }
1034
- if (P.includes("component") && (E.paths.has("") && (A = !0), !A))
1035
- for (const x of d) {
1036
- if (E.paths.has(x)) {
1034
+ if (V.includes("component") && (E.paths.has("") && (A = !0), !A))
1035
+ for (const P of d) {
1036
+ if (E.paths.has(P)) {
1037
1037
  A = !0;
1038
1038
  break;
1039
1039
  }
1040
- let M = x.lastIndexOf(".");
1040
+ let M = P.lastIndexOf(".");
1041
1041
  for (; M !== -1; ) {
1042
- const W = x.substring(0, M);
1043
- if (E.paths.has(W)) {
1042
+ const G = P.substring(0, M);
1043
+ if (E.paths.has(G)) {
1044
1044
  A = !0;
1045
1045
  break;
1046
1046
  }
1047
- const z = x.substring(
1047
+ const z = P.substring(
1048
1048
  M + 1
1049
1049
  );
1050
1050
  if (!isNaN(Number(z))) {
1051
- const $ = W.lastIndexOf(".");
1052
- if ($ !== -1) {
1053
- const N = W.substring(
1051
+ const T = G.lastIndexOf(".");
1052
+ if (T !== -1) {
1053
+ const C = G.substring(
1054
1054
  0,
1055
- $
1055
+ T
1056
1056
  );
1057
- if (E.paths.has(N)) {
1057
+ if (E.paths.has(C)) {
1058
1058
  A = !0;
1059
1059
  break;
1060
1060
  }
1061
1061
  }
1062
1062
  }
1063
- M = W.lastIndexOf(".");
1063
+ M = G.lastIndexOf(".");
1064
1064
  }
1065
1065
  if (A) break;
1066
1066
  }
1067
- if (!A && P.includes("deps") && E.depsFunction) {
1068
- const x = E.depsFunction(s);
1067
+ if (!A && V.includes("deps") && E.depsFunction) {
1068
+ const P = E.depsFunction(s);
1069
1069
  let M = !1;
1070
- typeof x == "boolean" ? x && (M = !0) : B(E.deps, x) || (E.deps = x, M = !0), M && (A = !0);
1070
+ typeof P == "boolean" ? P && (M = !0) : B(E.deps, P) || (E.deps = P, M = !0), M && (A = !0);
1071
1071
  }
1072
1072
  A && E.forceUpdate();
1073
1073
  }
@@ -1101,7 +1101,7 @@ function fe(e, i, S, g) {
1101
1101
  if (l === "getComponents")
1102
1102
  return () => r().stateComponents.get(e);
1103
1103
  if (l === "getAllFormRefs")
1104
- return () => $e.getState().getFormRefsByStateKey(e);
1104
+ return () => ke.getState().getFormRefsByStateKey(e);
1105
1105
  if (l === "_initialState")
1106
1106
  return r.getState().initialStateGlobal[e];
1107
1107
  if (l === "_serverState")
@@ -1114,13 +1114,13 @@ function fe(e, i, S, g) {
1114
1114
  if (l === "removeValidation") return I.removeValidation;
1115
1115
  }
1116
1116
  if (l === "getFormRef")
1117
- return () => $e.getState().getFormRef(e + "." + n.join("."));
1117
+ return () => ke.getState().getFormRef(e + "." + n.join("."));
1118
1118
  if (l === "validationWrapper")
1119
1119
  return ({
1120
1120
  children: u,
1121
1121
  hideMessage: t
1122
- }) => /* @__PURE__ */ ye(
1123
- je,
1122
+ }) => /* @__PURE__ */ Ie(
1123
+ Fe,
1124
1124
  {
1125
1125
  formOpts: t ? { validation: { message: "" } } : void 0,
1126
1126
  path: n,
@@ -1136,7 +1136,7 @@ function fe(e, i, S, g) {
1136
1136
  if (l === "update")
1137
1137
  return (u, t) => {
1138
1138
  if (t?.debounce)
1139
- Ue(() => {
1139
+ je(() => {
1140
1140
  se(i, u, n, "");
1141
1141
  const o = r.getState().getNestedState(e, n);
1142
1142
  t?.afterUpdate && t.afterUpdate(o);
@@ -1149,8 +1149,8 @@ function fe(e, i, S, g) {
1149
1149
  w(n);
1150
1150
  };
1151
1151
  if (l === "formElement")
1152
- return (u, t) => /* @__PURE__ */ ye(
1153
- Fe,
1152
+ return (u, t) => /* @__PURE__ */ Ie(
1153
+ De,
1154
1154
  {
1155
1155
  setState: i,
1156
1156
  stateKey: e,
@@ -1163,19 +1163,19 @@ function fe(e, i, S, g) {
1163
1163
  return a(ae, _, f);
1164
1164
  }
1165
1165
  }, H = new Proxy(U, j);
1166
- return y.set(G, {
1166
+ return y.set(W, {
1167
1167
  proxy: H,
1168
- stateVersion: C
1168
+ stateVersion: b
1169
1169
  }), H;
1170
1170
  }
1171
1171
  return a(
1172
1172
  r.getState().getNestedState(e, [])
1173
1173
  );
1174
1174
  }
1175
- function Ne(e) {
1176
- return ce(Xe, { proxy: e });
1175
+ function xe(e) {
1176
+ return ce(Qe, { proxy: e });
1177
1177
  }
1178
- function Ze({
1178
+ function Xe({
1179
1179
  proxy: e,
1180
1180
  rebuildStateShape: i
1181
1181
  }) {
@@ -1184,10 +1184,10 @@ function Ze({
1184
1184
  S,
1185
1185
  e._path
1186
1186
  ).stateMapNoRender(
1187
- (y, C, w, I, a) => e._mapFn(y, C, w, I, a)
1187
+ (y, b, w, I, a) => e._mapFn(y, b, w, I, a)
1188
1188
  ) : null;
1189
1189
  }
1190
- function Xe({
1190
+ function Qe({
1191
1191
  proxy: e
1192
1192
  }) {
1193
1193
  const i = J(null), S = `${e._stateKey}-${e._path.join(".")}`;
@@ -1218,16 +1218,16 @@ function Xe({
1218
1218
  else
1219
1219
  f = n;
1220
1220
  f !== null && typeof f == "object" && (f = JSON.stringify(f));
1221
- const G = document.createTextNode(String(f));
1222
- g.replaceWith(G);
1221
+ const W = document.createTextNode(String(f));
1222
+ g.replaceWith(W);
1223
1223
  }, [e._stateKey, e._path.join("."), e._effect]), ce("span", {
1224
1224
  ref: i,
1225
1225
  style: { display: "none" },
1226
1226
  "data-signal-id": S
1227
1227
  });
1228
1228
  }
1229
- function ft(e) {
1230
- const i = Me(
1229
+ function St(e) {
1230
+ const i = Oe(
1231
1231
  (S) => {
1232
1232
  const g = r.getState().stateComponents.get(e._stateKey) || {
1233
1233
  components: /* @__PURE__ */ new Map()
@@ -1241,13 +1241,13 @@ function ft(e) {
1241
1241
  );
1242
1242
  return ce("text", {}, String(i));
1243
1243
  }
1244
- function Qe({
1244
+ function Ke({
1245
1245
  stateKey: e,
1246
1246
  itemComponentId: i,
1247
1247
  itemPath: S,
1248
1248
  children: g
1249
1249
  }) {
1250
- const [, y] = ee({}), [C, w] = We(), I = J(null);
1250
+ const [, y] = ee({}), [b, w] = We(), I = J(null);
1251
1251
  return oe(() => {
1252
1252
  w.height > 0 && w.height !== I.current && (I.current = w.height, r.getState().setShadowMetadata(e, S, {
1253
1253
  virtualizer: {
@@ -1265,14 +1265,14 @@ function Qe({
1265
1265
  const n = r.getState().stateComponents.get(e);
1266
1266
  n && n.components.delete(a);
1267
1267
  };
1268
- }, [e, i, S.join(".")]), /* @__PURE__ */ ye("div", { ref: C, children: g });
1268
+ }, [e, i, S.join(".")]), /* @__PURE__ */ Ie("div", { ref: b, children: g });
1269
1269
  }
1270
1270
  export {
1271
- Ne as $cogsSignal,
1272
- ft as $cogsSignalStore,
1273
- dt as addStateOptions,
1274
- ut as createCogsState,
1275
- gt as notifyComponent,
1276
- Ye as useCogsStateFn
1271
+ xe as $cogsSignal,
1272
+ St as $cogsSignalStore,
1273
+ ut as addStateOptions,
1274
+ gt as createCogsState,
1275
+ ft as notifyComponent,
1276
+ Ze as useCogsStateFn
1277
1277
  };
1278
1278
  //# sourceMappingURL=CogsState.jsx.map