cogsbox-state 0.5.124 → 0.5.126
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/dist/CogsState.jsx +275 -265
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
package/dist/CogsState.jsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as le } from "react/jsx-runtime";
|
|
3
|
-
import { useState as ne, useRef as
|
|
4
|
-
import { transformStateFunc as
|
|
5
|
-
import { pushFunc as ee, updateFn as
|
|
3
|
+
import { useState as ne, useRef as H, useEffect as re, useLayoutEffect as _e, useMemo as Ee, createElement as Q, useSyncExternalStore as we, startTransition as $e } from "react";
|
|
4
|
+
import { transformStateFunc as he, isFunction as L, getDifferences as Ae, getNestedValue as D, isDeepEqual as U, debounce as Ne } from "./utility.js";
|
|
5
|
+
import { pushFunc as ee, updateFn as J, cutFunc as Z, ValidationWrapper as Te, FormControlComponent as Ve } from "./Functions.jsx";
|
|
6
6
|
import "zod";
|
|
7
7
|
import { getGlobalStore as r, formRefStore as de } from "./store.js";
|
|
8
|
-
import { useCogsConfig as
|
|
9
|
-
import
|
|
8
|
+
import { useCogsConfig as pe } from "./CogsStateClient.jsx";
|
|
9
|
+
import ae from "./node_modules/uuid/dist/esm-browser/v4.js";
|
|
10
10
|
function ue(e, i) {
|
|
11
11
|
const v = r.getState().getInitialOptions, S = r.getState().setInitialStateOptions, d = v(e) || {};
|
|
12
12
|
S(e, {
|
|
@@ -19,19 +19,19 @@ function ge({
|
|
|
19
19
|
options: i,
|
|
20
20
|
initialOptionsPart: v
|
|
21
21
|
}) {
|
|
22
|
-
const S =
|
|
22
|
+
const S = B(e) || {}, d = v[e] || {}, E = r.getState().setInitialStateOptions, _ = { ...d, ...S };
|
|
23
23
|
let m = !1;
|
|
24
24
|
if (i)
|
|
25
25
|
for (const c in i)
|
|
26
26
|
_.hasOwnProperty(c) ? (c == "localStorage" && i[c] && _[c].key !== i[c]?.key && (m = !0, _[c] = i[c]), c == "initialState" && i[c] && _[c] !== i[c] && (m = !0, _[c] = i[c])) : (m = !0, _[c] = i[c]);
|
|
27
27
|
m && E(e, _);
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function Je(e, { formElements: i, validation: v }) {
|
|
30
30
|
return { initialState: e, formElements: i, validation: v };
|
|
31
31
|
}
|
|
32
|
-
const
|
|
32
|
+
const Ze = (e, i) => {
|
|
33
33
|
let v = e;
|
|
34
|
-
const [S, d] =
|
|
34
|
+
const [S, d] = he(v);
|
|
35
35
|
(Object.keys(d).length > 0 || i && Object.keys(i).length > 0) && Object.keys(d).forEach((m) => {
|
|
36
36
|
d[m] = d[m] || {}, d[m].formElements = {
|
|
37
37
|
...i?.formElements,
|
|
@@ -39,16 +39,16 @@ const ze = (e, i) => {
|
|
|
39
39
|
...i?.validation,
|
|
40
40
|
...d[m].formElements || {}
|
|
41
41
|
// State-specific overrides
|
|
42
|
-
},
|
|
42
|
+
}, B(m) || r.getState().setInitialStateOptions(m, d[m]);
|
|
43
43
|
}), r.getState().setInitialStates(S), r.getState().setCreatedState(S);
|
|
44
44
|
const E = (m, c) => {
|
|
45
|
-
const [f] = ne(c?.componentId ??
|
|
45
|
+
const [f] = ne(c?.componentId ?? ae());
|
|
46
46
|
ge({
|
|
47
47
|
stateKey: m,
|
|
48
48
|
options: c,
|
|
49
49
|
initialOptionsPart: d
|
|
50
50
|
});
|
|
51
|
-
const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [
|
|
51
|
+
const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [C, T] = je(
|
|
52
52
|
y,
|
|
53
53
|
{
|
|
54
54
|
stateKey: m,
|
|
@@ -70,16 +70,16 @@ const ze = (e, i) => {
|
|
|
70
70
|
}
|
|
71
71
|
return { useCogsState: E, setCogsOptions: _ };
|
|
72
72
|
}, {
|
|
73
|
-
setUpdaterState:
|
|
74
|
-
setState:
|
|
75
|
-
getInitialOptions:
|
|
73
|
+
setUpdaterState: Y,
|
|
74
|
+
setState: G,
|
|
75
|
+
getInitialOptions: B,
|
|
76
76
|
getKeyState: fe,
|
|
77
|
-
getValidationErrors:
|
|
78
|
-
setStateLog:
|
|
79
|
-
updateInitialStateGlobal:
|
|
80
|
-
addValidationError:
|
|
81
|
-
removeValidationError:
|
|
82
|
-
setServerSyncActions:
|
|
77
|
+
getValidationErrors: ke,
|
|
78
|
+
setStateLog: Ce,
|
|
79
|
+
updateInitialStateGlobal: oe,
|
|
80
|
+
addValidationError: xe,
|
|
81
|
+
removeValidationError: R,
|
|
82
|
+
setServerSyncActions: be
|
|
83
83
|
} = r.getState(), Se = (e) => {
|
|
84
84
|
if (!e) return null;
|
|
85
85
|
try {
|
|
@@ -88,7 +88,7 @@ const ze = (e, i) => {
|
|
|
88
88
|
} catch (i) {
|
|
89
89
|
return console.error("Error loading from localStorage:", i), null;
|
|
90
90
|
}
|
|
91
|
-
},
|
|
91
|
+
}, Oe = (e, i, v, S) => {
|
|
92
92
|
v?.log && console.log(
|
|
93
93
|
"saving to localstorage",
|
|
94
94
|
i,
|
|
@@ -105,10 +105,10 @@ const ze = (e, i) => {
|
|
|
105
105
|
}, _ = `${S}-${i}-${d}`;
|
|
106
106
|
window.localStorage.setItem(_, JSON.stringify(E));
|
|
107
107
|
}
|
|
108
|
-
},
|
|
108
|
+
}, Pe = (e, i, v, S, d, E) => {
|
|
109
109
|
const _ = {
|
|
110
110
|
initialState: i,
|
|
111
|
-
updaterState:
|
|
111
|
+
updaterState: X(
|
|
112
112
|
e,
|
|
113
113
|
S,
|
|
114
114
|
d,
|
|
@@ -116,7 +116,7 @@ const ze = (e, i) => {
|
|
|
116
116
|
),
|
|
117
117
|
state: v
|
|
118
118
|
};
|
|
119
|
-
|
|
119
|
+
oe(e, _.initialState), Y(e, _.updaterState), G(e, _.state);
|
|
120
120
|
}, ie = (e) => {
|
|
121
121
|
const i = r.getState().stateComponents.get(e);
|
|
122
122
|
if (!i) return;
|
|
@@ -126,7 +126,7 @@ const ze = (e, i) => {
|
|
|
126
126
|
}), queueMicrotask(() => {
|
|
127
127
|
v.forEach((S) => S());
|
|
128
128
|
});
|
|
129
|
-
},
|
|
129
|
+
}, He = (e, i) => {
|
|
130
130
|
const v = r.getState().stateComponents.get(e);
|
|
131
131
|
if (v) {
|
|
132
132
|
const S = `${e}////${i}`, d = v.components.get(S);
|
|
@@ -135,7 +135,7 @@ const ze = (e, i) => {
|
|
|
135
135
|
d && d.forceUpdate();
|
|
136
136
|
}
|
|
137
137
|
};
|
|
138
|
-
function
|
|
138
|
+
function je(e, {
|
|
139
139
|
stateKey: i,
|
|
140
140
|
serverSync: v,
|
|
141
141
|
localStorage: S,
|
|
@@ -148,13 +148,13 @@ function Oe(e, {
|
|
|
148
148
|
syncUpdate: t,
|
|
149
149
|
dependencies: y
|
|
150
150
|
} = {}) {
|
|
151
|
-
const [
|
|
152
|
-
let
|
|
153
|
-
const [g] = ne(i ??
|
|
154
|
-
|
|
151
|
+
const [C, T] = ne({}), { sessionId: x } = pe();
|
|
152
|
+
let M = !i;
|
|
153
|
+
const [g] = ne(i ?? ae()), s = r.getState().stateLog[g], W = H(/* @__PURE__ */ new Set()), P = H(c ?? ae()), p = H(null);
|
|
154
|
+
p.current = B(g), re(() => {
|
|
155
155
|
if (t && t.stateKey === g && t.path?.[0]) {
|
|
156
|
-
|
|
157
|
-
...
|
|
156
|
+
G(g, (a) => ({
|
|
157
|
+
...a,
|
|
158
158
|
[t.path[0]]: t.newValue
|
|
159
159
|
}));
|
|
160
160
|
const l = `${t.stateKey}:${t.path.join(".")}`;
|
|
@@ -167,188 +167,198 @@ function Oe(e, {
|
|
|
167
167
|
f && ue(g, {
|
|
168
168
|
initialState: f
|
|
169
169
|
});
|
|
170
|
-
const l =
|
|
171
|
-
let
|
|
170
|
+
const l = p.current;
|
|
171
|
+
let a = null;
|
|
172
172
|
const u = L(l?.localStorage?.key) ? l?.localStorage?.key(f) : l?.localStorage?.key;
|
|
173
|
-
console.log("newoptions", l), console.log("localkey", u), console.log("initialState", f), u &&
|
|
174
|
-
|
|
173
|
+
console.log("newoptions", l), console.log("localkey", u), console.log("initialState", f), u && x && (a = Se(
|
|
174
|
+
x + "-" + g + "-" + u
|
|
175
175
|
));
|
|
176
|
-
const
|
|
177
|
-
console.log("createdState - intiual",
|
|
178
|
-
let
|
|
176
|
+
const w = r.getState().iniitialCreatedState[g];
|
|
177
|
+
console.log("createdState - intiual", w, f);
|
|
178
|
+
let N = null;
|
|
179
179
|
if (f) {
|
|
180
|
-
|
|
180
|
+
N = f, a && a.lastUpdated > (a.lastSyncedWithServer || 0) && (N = a.state, l?.localStorage?.onChange && l?.localStorage?.onChange(N)), console.log("newState thius is newstate", N), Pe(
|
|
181
181
|
g,
|
|
182
182
|
f,
|
|
183
|
-
|
|
183
|
+
N,
|
|
184
184
|
n,
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
P.current,
|
|
186
|
+
x
|
|
187
187
|
), ie(g);
|
|
188
|
-
const
|
|
189
|
-
console.log("reactiveTypes.............................",
|
|
188
|
+
const h = Array.isArray(m) ? m : [m || "component"];
|
|
189
|
+
console.log("reactiveTypes.............................", h), h.includes("none") || T({});
|
|
190
190
|
}
|
|
191
|
-
}, [f, ...y || []]),
|
|
192
|
-
|
|
191
|
+
}, [f, ...y || []]), _e(() => {
|
|
192
|
+
M && ue(g, {
|
|
193
193
|
serverSync: v,
|
|
194
194
|
formElements: d,
|
|
195
195
|
initialState: f,
|
|
196
196
|
localStorage: S,
|
|
197
197
|
middleware: E
|
|
198
198
|
});
|
|
199
|
-
const l = `${g}////${
|
|
199
|
+
const l = `${g}////${P.current}`, a = r.getState().stateComponents.get(g) || {
|
|
200
200
|
components: /* @__PURE__ */ new Map()
|
|
201
201
|
};
|
|
202
|
-
return
|
|
202
|
+
return a.components.set(l, {
|
|
203
203
|
forceUpdate: () => T({}),
|
|
204
204
|
paths: /* @__PURE__ */ new Set(),
|
|
205
205
|
deps: [],
|
|
206
206
|
depsFunction: _ || void 0,
|
|
207
207
|
reactiveType: m ?? ["component", "deps"]
|
|
208
|
-
}), r.getState().stateComponents.set(g,
|
|
209
|
-
const u = `${g}////${
|
|
210
|
-
|
|
208
|
+
}), r.getState().stateComponents.set(g, a), T({}), () => {
|
|
209
|
+
const u = `${g}////${P.current}`;
|
|
210
|
+
a && (a.components.delete(u), a.components.size === 0 && r.getState().stateComponents.delete(g));
|
|
211
211
|
};
|
|
212
212
|
}, []);
|
|
213
|
-
const n = (l,
|
|
214
|
-
if (Array.isArray(
|
|
215
|
-
const
|
|
216
|
-
|
|
213
|
+
const n = (l, a, u, w) => {
|
|
214
|
+
if (Array.isArray(a)) {
|
|
215
|
+
const N = `${g}-${a.join(".")}`;
|
|
216
|
+
W.current.add(N);
|
|
217
217
|
}
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
if (
|
|
221
|
-
let
|
|
218
|
+
G(g, (N) => {
|
|
219
|
+
const h = L(l) ? l(N) : l, j = Ae(N, h), O = new Set(j), q = `${g}-${a.join(".")}`;
|
|
220
|
+
if (q) {
|
|
221
|
+
let F = !1, I = r.getState().signalDomElements.get(q);
|
|
222
222
|
if ((!I || I.size === 0) && (u.updateType === "insert" || u.updateType === "cut")) {
|
|
223
|
-
const
|
|
223
|
+
const A = a.slice(0, -1), V = D(h, A);
|
|
224
224
|
if (Array.isArray(V)) {
|
|
225
|
-
|
|
226
|
-
const $ = `${g}-${
|
|
225
|
+
F = !0;
|
|
226
|
+
const $ = `${g}-${A.join(".")}`;
|
|
227
227
|
I = r.getState().signalDomElements.get($);
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
if (I) {
|
|
231
|
-
const
|
|
232
|
-
I.forEach(({ parentId: V, position: $, effect:
|
|
233
|
-
const
|
|
231
|
+
const A = F ? D(h, a.slice(0, -1)) : D(h, a);
|
|
232
|
+
I.forEach(({ parentId: V, position: $, effect: k }) => {
|
|
233
|
+
const b = document.querySelector(
|
|
234
234
|
`[data-parent-id="${V}"]`
|
|
235
235
|
);
|
|
236
|
-
if (
|
|
237
|
-
const ce = Array.from(
|
|
236
|
+
if (b) {
|
|
237
|
+
const ce = Array.from(b.childNodes);
|
|
238
238
|
if (ce[$]) {
|
|
239
|
-
const
|
|
240
|
-
ce[$].textContent = String(
|
|
239
|
+
const Ie = k ? new Function("state", `return (${k})(state)`)(A) : A;
|
|
240
|
+
ce[$].textContent = String(Ie);
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
});
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
-
u.updateType === "update" && (
|
|
247
|
-
(
|
|
246
|
+
u.updateType === "update" && (w || p.current?.validationKey) && a && R(
|
|
247
|
+
(w || p.current?.validationKey) + "." + a.join(".")
|
|
248
248
|
);
|
|
249
|
-
const
|
|
250
|
-
u.updateType === "cut" &&
|
|
251
|
-
|
|
252
|
-
), u.updateType === "insert" &&
|
|
253
|
-
|
|
254
|
-
).filter(([I,
|
|
249
|
+
const z = a.slice(0, a.length - 1);
|
|
250
|
+
u.updateType === "cut" && p.current?.validationKey && R(
|
|
251
|
+
p.current?.validationKey + "." + z.join(".")
|
|
252
|
+
), u.updateType === "insert" && p.current?.validationKey && ke(
|
|
253
|
+
p.current?.validationKey + "." + z.join(".")
|
|
254
|
+
).filter(([I, A]) => {
|
|
255
255
|
let V = I?.split(".").length;
|
|
256
|
-
if (I ==
|
|
257
|
-
let $ = I + "." +
|
|
258
|
-
|
|
256
|
+
if (I == z.join(".") && V == z.length - 1) {
|
|
257
|
+
let $ = I + "." + z;
|
|
258
|
+
R(I), xe($, A);
|
|
259
259
|
}
|
|
260
260
|
});
|
|
261
|
-
const
|
|
261
|
+
const me = D(N, a), ye = D(h, a), ve = u.updateType === "update" ? a.join(".") : [...a].slice(0, -1).join("."), K = r.getState().stateComponents.get(g);
|
|
262
262
|
if (console.log(
|
|
263
263
|
"pathetocaheck.............................",
|
|
264
|
-
|
|
264
|
+
a,
|
|
265
|
+
u,
|
|
266
|
+
ve ?? "NONE",
|
|
265
267
|
K
|
|
266
268
|
), K)
|
|
267
|
-
for (const [
|
|
268
|
-
|
|
269
|
+
for (const [
|
|
270
|
+
F,
|
|
271
|
+
I
|
|
272
|
+
] of K.components.entries()) {
|
|
273
|
+
let A = !1;
|
|
269
274
|
const V = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
|
|
270
|
-
if (
|
|
275
|
+
if (!V.includes("none")) {
|
|
271
276
|
if (V.includes("all")) {
|
|
272
277
|
I.forceUpdate();
|
|
273
278
|
continue;
|
|
274
279
|
}
|
|
275
|
-
if (V.includes("component")
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
280
|
+
if (V.includes("component")) {
|
|
281
|
+
for (const $ of O) {
|
|
282
|
+
if (I.paths.has($)) {
|
|
283
|
+
A = !0;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
let k = $;
|
|
287
|
+
for (; k.includes("."); )
|
|
288
|
+
if (k = k.substring(
|
|
289
|
+
0,
|
|
290
|
+
k.lastIndexOf(".")
|
|
291
|
+
), I.paths.has(k)) {
|
|
292
|
+
A = !0;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
if (A) break;
|
|
296
|
+
if (I.paths.has("")) {
|
|
297
|
+
A = !0;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (!A && V.includes("deps") && I.depsFunction) {
|
|
302
|
+
const $ = I.depsFunction(h);
|
|
303
|
+
typeof $ == "boolean" ? $ && (A = !0) : U(I.deps, $) || (I.deps = $, A = !0);
|
|
304
|
+
}
|
|
305
|
+
A && I.forceUpdate();
|
|
295
306
|
}
|
|
296
|
-
N && I.forceUpdate();
|
|
297
307
|
}
|
|
298
308
|
}
|
|
299
309
|
const se = {
|
|
300
310
|
timeStamp: Date.now(),
|
|
301
311
|
stateKey: g,
|
|
302
|
-
path:
|
|
312
|
+
path: a,
|
|
303
313
|
updateType: u.updateType,
|
|
304
314
|
status: "new",
|
|
305
|
-
oldValue:
|
|
306
|
-
newValue:
|
|
315
|
+
oldValue: me,
|
|
316
|
+
newValue: ye
|
|
307
317
|
};
|
|
308
|
-
if (
|
|
309
|
-
const
|
|
310
|
-
const
|
|
311
|
-
return
|
|
318
|
+
if (Ce(g, (F) => {
|
|
319
|
+
const A = [...F ?? [], se].reduce((V, $) => {
|
|
320
|
+
const k = `${$.stateKey}:${JSON.stringify($.path)}`, b = V.get(k);
|
|
321
|
+
return b ? (b.timeStamp = Math.max(b.timeStamp, $.timeStamp), b.newValue = $.newValue, b.oldValue = b.oldValue ?? $.oldValue, b.updateType = $.updateType) : V.set(k, { ...$ }), V;
|
|
312
322
|
}, /* @__PURE__ */ new Map());
|
|
313
|
-
return Array.from(
|
|
314
|
-
}),
|
|
315
|
-
|
|
323
|
+
return Array.from(A.values());
|
|
324
|
+
}), Oe(
|
|
325
|
+
h,
|
|
316
326
|
g,
|
|
317
|
-
|
|
318
|
-
|
|
327
|
+
p.current,
|
|
328
|
+
x
|
|
319
329
|
), E && E({
|
|
320
330
|
updateLog: s,
|
|
321
331
|
update: se
|
|
322
|
-
}),
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state:
|
|
326
|
-
rollBackState:
|
|
332
|
+
}), p.current?.serverSync) {
|
|
333
|
+
const F = r.getState().serverState[g], I = p.current?.serverSync;
|
|
334
|
+
be(g, {
|
|
335
|
+
syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: h }),
|
|
336
|
+
rollBackState: F,
|
|
327
337
|
actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
|
|
328
338
|
status: "waiting"
|
|
329
339
|
});
|
|
330
340
|
}
|
|
331
|
-
return
|
|
341
|
+
return h;
|
|
332
342
|
});
|
|
333
343
|
};
|
|
334
|
-
r.getState().updaterState[g] || (
|
|
344
|
+
r.getState().updaterState[g] || (Y(
|
|
335
345
|
g,
|
|
336
|
-
|
|
346
|
+
X(
|
|
337
347
|
g,
|
|
338
348
|
n,
|
|
339
|
-
|
|
340
|
-
|
|
349
|
+
P.current,
|
|
350
|
+
x
|
|
341
351
|
)
|
|
342
|
-
), r.getState().cogsStateStore[g] ||
|
|
343
|
-
const
|
|
352
|
+
), r.getState().cogsStateStore[g] || G(g, e), r.getState().initialStateGlobal[g] || oe(g, e));
|
|
353
|
+
const o = Ee(() => X(
|
|
344
354
|
g,
|
|
345
355
|
n,
|
|
346
|
-
|
|
347
|
-
|
|
356
|
+
P.current,
|
|
357
|
+
x
|
|
348
358
|
), [g]);
|
|
349
|
-
return [fe(g),
|
|
359
|
+
return [fe(g), o];
|
|
350
360
|
}
|
|
351
|
-
function
|
|
361
|
+
function X(e, i, v, S) {
|
|
352
362
|
const d = /* @__PURE__ */ new Map();
|
|
353
363
|
let E = 0;
|
|
354
364
|
const _ = (f) => {
|
|
@@ -358,15 +368,15 @@ function Y(e, i, v, S) {
|
|
|
358
368
|
E++;
|
|
359
369
|
}, m = {
|
|
360
370
|
removeValidation: (f) => {
|
|
361
|
-
f?.validationKey &&
|
|
371
|
+
f?.validationKey && R(f.validationKey);
|
|
362
372
|
},
|
|
363
373
|
revertToInitialState: (f) => {
|
|
364
374
|
const t = r.getState().getInitialOptions(e)?.validation;
|
|
365
|
-
t?.key &&
|
|
375
|
+
t?.key && R(t?.key), f?.validationKey && R(f.validationKey);
|
|
366
376
|
const y = r.getState().initialStateGlobal[e];
|
|
367
377
|
r.getState().clearSelectedIndexesForState(e), d.clear(), E++;
|
|
368
|
-
const
|
|
369
|
-
|
|
378
|
+
const C = c(y, []), T = B(e), x = L(T?.localStorage?.key) ? T?.localStorage?.key(y) : T?.localStorage?.key, M = `${S}-${e}-${x}`;
|
|
379
|
+
M && localStorage.removeItem(M), Y(e, C), G(e, y);
|
|
370
380
|
const g = r.getState().stateComponents.get(e);
|
|
371
381
|
return g && g.components.forEach((s) => {
|
|
372
382
|
s.forceUpdate();
|
|
@@ -374,17 +384,17 @@ function Y(e, i, v, S) {
|
|
|
374
384
|
},
|
|
375
385
|
updateInitialState: (f) => {
|
|
376
386
|
d.clear(), E++;
|
|
377
|
-
const t =
|
|
387
|
+
const t = X(
|
|
378
388
|
e,
|
|
379
389
|
i,
|
|
380
390
|
v,
|
|
381
391
|
S
|
|
382
392
|
);
|
|
383
|
-
return
|
|
384
|
-
|
|
393
|
+
return $e(() => {
|
|
394
|
+
oe(e, f), Y(e, t), G(e, f);
|
|
385
395
|
const y = r.getState().stateComponents.get(e);
|
|
386
|
-
y && y.components.forEach((
|
|
387
|
-
|
|
396
|
+
y && y.components.forEach((C) => {
|
|
397
|
+
C.forceUpdate();
|
|
388
398
|
});
|
|
389
399
|
}), {
|
|
390
400
|
fetchId: (y) => t.get()[y]
|
|
@@ -395,48 +405,48 @@ function Y(e, i, v, S) {
|
|
|
395
405
|
_isLoading: r.getState().isLoadingGlobal[e],
|
|
396
406
|
_isServerSynced: () => {
|
|
397
407
|
const f = r.getState().serverState[e];
|
|
398
|
-
return !!(f &&
|
|
408
|
+
return !!(f && U(f, fe(e)));
|
|
399
409
|
}
|
|
400
410
|
};
|
|
401
411
|
function c(f, t = [], y) {
|
|
402
|
-
const
|
|
403
|
-
d.get(
|
|
412
|
+
const C = t.map(String).join(".");
|
|
413
|
+
d.get(C);
|
|
404
414
|
const T = function() {
|
|
405
415
|
return r().getNestedState(e, t);
|
|
406
416
|
};
|
|
407
417
|
Object.keys(m).forEach((g) => {
|
|
408
418
|
T[g] = m[g];
|
|
409
419
|
});
|
|
410
|
-
const
|
|
411
|
-
apply(g, s,
|
|
420
|
+
const x = {
|
|
421
|
+
apply(g, s, W) {
|
|
412
422
|
return console.log(
|
|
413
423
|
`PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
|
|
414
424
|
), console.trace("Apply trap stack trace"), r().getNestedState(e, t);
|
|
415
425
|
},
|
|
416
426
|
get(g, s) {
|
|
417
427
|
if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
|
|
418
|
-
const n = t.join("."),
|
|
428
|
+
const n = t.join("."), o = `${e}////${v}`, l = r.getState().stateComponents.get(e);
|
|
419
429
|
if (l) {
|
|
420
|
-
const
|
|
421
|
-
|
|
430
|
+
const a = l.components.get(o);
|
|
431
|
+
a && (t.length > 0 || s === "get") && a.paths.add(n);
|
|
422
432
|
}
|
|
423
433
|
}
|
|
424
434
|
if (s === "_status") {
|
|
425
|
-
const n = r.getState().getNestedState(e, t),
|
|
426
|
-
return
|
|
435
|
+
const n = r.getState().getNestedState(e, t), o = r.getState().initialStateGlobal[e], l = D(o, t);
|
|
436
|
+
return U(n, l) ? "fresh" : "stale";
|
|
427
437
|
}
|
|
428
438
|
if (s === "getStatus")
|
|
429
439
|
return function() {
|
|
430
440
|
const n = r().getNestedState(
|
|
431
441
|
e,
|
|
432
442
|
t
|
|
433
|
-
),
|
|
434
|
-
return
|
|
443
|
+
), o = r.getState().initialStateGlobal[e], l = D(o, t);
|
|
444
|
+
return U(n, l) ? "fresh" : "stale";
|
|
435
445
|
};
|
|
436
446
|
if (s === "removeStorage")
|
|
437
447
|
return () => {
|
|
438
|
-
const n = r.getState().initialStateGlobal[e],
|
|
439
|
-
console.log("removing storage",
|
|
448
|
+
const n = r.getState().initialStateGlobal[e], o = B(e), l = L(o?.localStorage?.key) ? o?.localStorage?.key(n) : o?.localStorage?.key, a = `${S}-${e}-${l}`;
|
|
449
|
+
console.log("removing storage", a), a && localStorage.removeItem(a);
|
|
440
450
|
};
|
|
441
451
|
if (s === "showValidationErrors")
|
|
442
452
|
return () => {
|
|
@@ -460,31 +470,31 @@ function Y(e, i, v, S) {
|
|
|
460
470
|
return () => r.getState().getSelectedIndex(e, t.join(".")) ?? -1;
|
|
461
471
|
if (s === "stateSort")
|
|
462
472
|
return (n) => {
|
|
463
|
-
const
|
|
473
|
+
const a = [...r.getState().getNestedState(e, t).map((u, w) => ({
|
|
464
474
|
...u,
|
|
465
|
-
__origIndex:
|
|
475
|
+
__origIndex: w.toString()
|
|
466
476
|
}))].sort(n);
|
|
467
|
-
return d.clear(), E++, c(
|
|
477
|
+
return d.clear(), E++, c(a, t, {
|
|
468
478
|
filtered: [...y?.filtered || [], t],
|
|
469
|
-
validIndices:
|
|
479
|
+
validIndices: a.map(
|
|
470
480
|
(u) => parseInt(u.__origIndex)
|
|
471
481
|
)
|
|
472
482
|
});
|
|
473
483
|
};
|
|
474
484
|
if (s === "stateMap" || s === "stateMapNoRender")
|
|
475
485
|
return (n) => {
|
|
476
|
-
const
|
|
477
|
-
(
|
|
478
|
-
), l =
|
|
479
|
-
return s !== "stateMapNoRender" && (d.clear(), E++), l.map((
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
[...t,
|
|
486
|
+
const o = y?.filtered?.some(
|
|
487
|
+
(a) => a.join(".") === t.join(".")
|
|
488
|
+
), l = o ? f : r.getState().getNestedState(e, t);
|
|
489
|
+
return s !== "stateMapNoRender" && (d.clear(), E++), l.map((a, u) => {
|
|
490
|
+
const w = o && a.__origIndex ? a.__origIndex : u, N = c(
|
|
491
|
+
a,
|
|
492
|
+
[...t, w.toString()],
|
|
483
493
|
y
|
|
484
494
|
);
|
|
485
495
|
return n(
|
|
486
|
-
|
|
487
|
-
|
|
496
|
+
a,
|
|
497
|
+
N,
|
|
488
498
|
u,
|
|
489
499
|
f,
|
|
490
500
|
c(f, t, y)
|
|
@@ -492,7 +502,7 @@ function Y(e, i, v, S) {
|
|
|
492
502
|
});
|
|
493
503
|
};
|
|
494
504
|
if (s === "$stateMap")
|
|
495
|
-
return (n) =>
|
|
505
|
+
return (n) => Q(Fe, {
|
|
496
506
|
proxy: {
|
|
497
507
|
_stateKey: e,
|
|
498
508
|
_path: t,
|
|
@@ -507,28 +517,28 @@ function Y(e, i, v, S) {
|
|
|
507
517
|
(u) => u.join(".") === t.join(".")
|
|
508
518
|
) ? f : r.getState().getNestedState(e, t);
|
|
509
519
|
d.clear(), E++;
|
|
510
|
-
const
|
|
511
|
-
(u,
|
|
520
|
+
const a = l.flatMap(
|
|
521
|
+
(u, w) => u[n] ?? []
|
|
512
522
|
);
|
|
513
523
|
return c(
|
|
514
|
-
|
|
524
|
+
a,
|
|
515
525
|
[...t, "[*]", n],
|
|
516
526
|
y
|
|
517
527
|
);
|
|
518
528
|
};
|
|
519
529
|
if (s === "findWith")
|
|
520
|
-
return (n,
|
|
530
|
+
return (n, o) => {
|
|
521
531
|
const l = f.findIndex(
|
|
522
|
-
(
|
|
532
|
+
(w) => w[n] === o
|
|
523
533
|
);
|
|
524
534
|
if (l === -1) return;
|
|
525
|
-
const
|
|
526
|
-
return d.clear(), E++, d.clear(), E++, c(
|
|
535
|
+
const a = f[l], u = [...t, l.toString()];
|
|
536
|
+
return d.clear(), E++, d.clear(), E++, c(a, u);
|
|
527
537
|
};
|
|
528
538
|
if (s === "index")
|
|
529
539
|
return (n) => {
|
|
530
|
-
const
|
|
531
|
-
return c(
|
|
540
|
+
const o = f[n];
|
|
541
|
+
return c(o, [...t, n.toString()]);
|
|
532
542
|
};
|
|
533
543
|
if (s === "insert")
|
|
534
544
|
return (n) => (_(t), ee(i, n, t, e), c(
|
|
@@ -536,65 +546,65 @@ function Y(e, i, v, S) {
|
|
|
536
546
|
[]
|
|
537
547
|
));
|
|
538
548
|
if (s === "uniqueInsert")
|
|
539
|
-
return (n,
|
|
540
|
-
const
|
|
541
|
-
let
|
|
542
|
-
if (!
|
|
543
|
-
if (
|
|
544
|
-
const
|
|
545
|
-
(
|
|
549
|
+
return (n, o, l) => {
|
|
550
|
+
const a = r.getState().getNestedState(e, t), u = L(n) ? n(a) : n;
|
|
551
|
+
let w = null;
|
|
552
|
+
if (!a.some((h) => {
|
|
553
|
+
if (o) {
|
|
554
|
+
const O = o.every(
|
|
555
|
+
(q) => U(h[q], u[q])
|
|
546
556
|
);
|
|
547
|
-
return
|
|
557
|
+
return O && (w = h), O;
|
|
548
558
|
}
|
|
549
|
-
const
|
|
550
|
-
return
|
|
559
|
+
const j = U(h, u);
|
|
560
|
+
return j && (w = h), j;
|
|
551
561
|
}))
|
|
552
562
|
_(t), ee(i, u, t, e);
|
|
553
|
-
else if (l &&
|
|
554
|
-
const
|
|
555
|
-
(
|
|
563
|
+
else if (l && w) {
|
|
564
|
+
const h = l(w), j = a.map(
|
|
565
|
+
(O) => U(O, w) ? h : O
|
|
556
566
|
);
|
|
557
|
-
_(t),
|
|
567
|
+
_(t), J(i, j, t);
|
|
558
568
|
}
|
|
559
569
|
};
|
|
560
570
|
if (s === "cut")
|
|
561
|
-
return (n,
|
|
562
|
-
|
|
571
|
+
return (n, o) => {
|
|
572
|
+
o?.waitForSync || (_(t), Z(i, t, e, n));
|
|
563
573
|
};
|
|
564
574
|
if (s === "cutByValue")
|
|
565
575
|
return (n) => {
|
|
566
|
-
for (let
|
|
567
|
-
f[
|
|
576
|
+
for (let o = 0; o < f.length; o++)
|
|
577
|
+
f[o] === n && Z(i, t, e, o);
|
|
568
578
|
};
|
|
569
579
|
if (s === "toggleByValue")
|
|
570
580
|
return (n) => {
|
|
571
|
-
const
|
|
572
|
-
|
|
581
|
+
const o = f.findIndex((l) => l === n);
|
|
582
|
+
o > -1 ? Z(i, t, e, o) : ee(i, n, t, e);
|
|
573
583
|
};
|
|
574
584
|
if (s === "stateFilter")
|
|
575
585
|
return (n) => {
|
|
576
|
-
const
|
|
586
|
+
const o = f.map((u, w) => ({
|
|
577
587
|
...u,
|
|
578
|
-
__origIndex:
|
|
579
|
-
})), l = [],
|
|
580
|
-
for (let u = 0; u <
|
|
581
|
-
n(
|
|
582
|
-
return d.clear(), E++, c(
|
|
588
|
+
__origIndex: w.toString()
|
|
589
|
+
})), l = [], a = [];
|
|
590
|
+
for (let u = 0; u < o.length; u++)
|
|
591
|
+
n(o[u], u) && (l.push(u), a.push(o[u]));
|
|
592
|
+
return d.clear(), E++, c(a, t, {
|
|
583
593
|
filtered: [...y?.filtered || [], t],
|
|
584
594
|
validIndices: l
|
|
585
595
|
// Always pass validIndices, even if empty
|
|
586
596
|
});
|
|
587
597
|
};
|
|
588
598
|
}
|
|
589
|
-
const
|
|
590
|
-
if (!isNaN(Number(
|
|
591
|
-
const n = t.slice(0, -1),
|
|
592
|
-
if (Array.isArray(
|
|
593
|
-
return () =>
|
|
599
|
+
const W = t[t.length - 1];
|
|
600
|
+
if (!isNaN(Number(W))) {
|
|
601
|
+
const n = t.slice(0, -1), o = r.getState().getNestedState(e, n);
|
|
602
|
+
if (Array.isArray(o) && s === "cut")
|
|
603
|
+
return () => Z(
|
|
594
604
|
i,
|
|
595
605
|
n,
|
|
596
606
|
e,
|
|
597
|
-
Number(
|
|
607
|
+
Number(W)
|
|
598
608
|
);
|
|
599
609
|
}
|
|
600
610
|
if (s === "get")
|
|
@@ -623,38 +633,38 @@ function Y(e, i, v, S) {
|
|
|
623
633
|
if (s == "getLocalStorage")
|
|
624
634
|
return (n) => Se(S + "-" + e + "-" + n);
|
|
625
635
|
if (s === "_selected") {
|
|
626
|
-
const n = t.slice(0, -1),
|
|
627
|
-
return Array.isArray(l) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e,
|
|
636
|
+
const n = t.slice(0, -1), o = n.join("."), l = r.getState().getNestedState(e, n);
|
|
637
|
+
return Array.isArray(l) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
|
|
628
638
|
}
|
|
629
639
|
if (s === "setSelected")
|
|
630
640
|
return (n) => {
|
|
631
|
-
const
|
|
632
|
-
n ? r.getState().setSelectedIndex(e,
|
|
633
|
-
const u = r.getState().getNestedState(e, [...
|
|
634
|
-
|
|
641
|
+
const o = t.slice(0, -1), l = Number(t[t.length - 1]), a = o.join(".");
|
|
642
|
+
n ? r.getState().setSelectedIndex(e, a, l) : r.getState().setSelectedIndex(e, a, void 0);
|
|
643
|
+
const u = r.getState().getNestedState(e, [...o]);
|
|
644
|
+
J(i, u, o), _(o);
|
|
635
645
|
};
|
|
636
646
|
if (t.length == 0) {
|
|
637
647
|
if (s === "validateZodSchema")
|
|
638
648
|
return () => {
|
|
639
|
-
const n = r.getState().getInitialOptions(e)?.validation,
|
|
649
|
+
const n = r.getState().getInitialOptions(e)?.validation, o = r.getState().addValidationError;
|
|
640
650
|
if (!n?.zodSchema)
|
|
641
651
|
throw new Error("Zod schema not found");
|
|
642
652
|
if (!n?.key)
|
|
643
653
|
throw new Error("Validation key not found");
|
|
644
|
-
|
|
654
|
+
R(n.key);
|
|
645
655
|
const l = r.getState().cogsStateStore[e];
|
|
646
656
|
try {
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
657
|
+
const a = r.getState().getValidationErrors(n.key);
|
|
658
|
+
a && a.length > 0 && a.forEach(([w]) => {
|
|
659
|
+
w && w.startsWith(n.key) && R(w);
|
|
650
660
|
});
|
|
651
661
|
const u = n.zodSchema.safeParse(l);
|
|
652
|
-
return u.success ? !0 : (u.error.errors.forEach((
|
|
653
|
-
const
|
|
654
|
-
|
|
662
|
+
return u.success ? !0 : (u.error.errors.forEach((N) => {
|
|
663
|
+
const h = N.path, j = N.message, O = [n.key, ...h].join(".");
|
|
664
|
+
o(O, j);
|
|
655
665
|
}), ie(e), !1);
|
|
656
|
-
} catch (
|
|
657
|
-
return console.error("Zod schema validation failed",
|
|
666
|
+
} catch (a) {
|
|
667
|
+
return console.error("Zod schema validation failed", a), !1;
|
|
658
668
|
}
|
|
659
669
|
};
|
|
660
670
|
if (s === "_componentId") return v;
|
|
@@ -678,11 +688,11 @@ function Y(e, i, v, S) {
|
|
|
678
688
|
if (s === "validationWrapper")
|
|
679
689
|
return ({
|
|
680
690
|
children: n,
|
|
681
|
-
hideMessage:
|
|
691
|
+
hideMessage: o
|
|
682
692
|
}) => /* @__PURE__ */ le(
|
|
683
|
-
|
|
693
|
+
Te,
|
|
684
694
|
{
|
|
685
|
-
formOpts:
|
|
695
|
+
formOpts: o ? { validation: { message: "" } } : void 0,
|
|
686
696
|
path: t,
|
|
687
697
|
validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
|
|
688
698
|
stateKey: e,
|
|
@@ -694,48 +704,48 @@ function Y(e, i, v, S) {
|
|
|
694
704
|
if (s === "_path") return t;
|
|
695
705
|
if (s === "_isServerSynced") return m._isServerSynced;
|
|
696
706
|
if (s === "update")
|
|
697
|
-
return (n,
|
|
698
|
-
if (
|
|
699
|
-
|
|
700
|
-
|
|
707
|
+
return (n, o) => {
|
|
708
|
+
if (o?.debounce)
|
|
709
|
+
Ne(() => {
|
|
710
|
+
J(i, n, t, "");
|
|
701
711
|
const l = r.getState().getNestedState(e, t);
|
|
702
|
-
|
|
703
|
-
},
|
|
712
|
+
o?.afterUpdate && o.afterUpdate(l);
|
|
713
|
+
}, o.debounce);
|
|
704
714
|
else {
|
|
705
|
-
|
|
715
|
+
J(i, n, t, "");
|
|
706
716
|
const l = r.getState().getNestedState(e, t);
|
|
707
|
-
|
|
717
|
+
o?.afterUpdate && o.afterUpdate(l);
|
|
708
718
|
}
|
|
709
719
|
_(t);
|
|
710
720
|
};
|
|
711
721
|
if (s === "formElement")
|
|
712
|
-
return (n,
|
|
713
|
-
|
|
722
|
+
return (n, o) => /* @__PURE__ */ le(
|
|
723
|
+
Ve,
|
|
714
724
|
{
|
|
715
725
|
setState: i,
|
|
716
726
|
stateKey: e,
|
|
717
727
|
path: t,
|
|
718
728
|
child: n,
|
|
719
|
-
formOpts:
|
|
729
|
+
formOpts: o
|
|
720
730
|
}
|
|
721
731
|
);
|
|
722
|
-
const
|
|
723
|
-
return c(
|
|
732
|
+
const P = [...t, s], p = r.getState().getNestedState(e, P);
|
|
733
|
+
return c(p, P, y);
|
|
724
734
|
}
|
|
725
|
-
},
|
|
726
|
-
return d.set(
|
|
727
|
-
proxy:
|
|
735
|
+
}, M = new Proxy(T, x);
|
|
736
|
+
return d.set(C, {
|
|
737
|
+
proxy: M,
|
|
728
738
|
stateVersion: E
|
|
729
|
-
}),
|
|
739
|
+
}), M;
|
|
730
740
|
}
|
|
731
741
|
return c(
|
|
732
742
|
r.getState().getNestedState(e, [])
|
|
733
743
|
);
|
|
734
744
|
}
|
|
735
745
|
function te(e) {
|
|
736
|
-
return
|
|
746
|
+
return Q(Re, { proxy: e });
|
|
737
747
|
}
|
|
738
|
-
function
|
|
748
|
+
function Fe({
|
|
739
749
|
proxy: e,
|
|
740
750
|
rebuildStateShape: i
|
|
741
751
|
}) {
|
|
@@ -747,10 +757,10 @@ function je({
|
|
|
747
757
|
(d, E, _, m, c) => e._mapFn(d, E, _, m, c)
|
|
748
758
|
) : null;
|
|
749
759
|
}
|
|
750
|
-
function
|
|
760
|
+
function Re({
|
|
751
761
|
proxy: e
|
|
752
762
|
}) {
|
|
753
|
-
const i =
|
|
763
|
+
const i = H(null), v = `${e._stateKey}-${e._path.join(".")}`;
|
|
754
764
|
return re(() => {
|
|
755
765
|
const S = i.current;
|
|
756
766
|
if (!S || !S.parentElement) return;
|
|
@@ -778,16 +788,16 @@ function be({
|
|
|
778
788
|
else
|
|
779
789
|
y = t;
|
|
780
790
|
y !== null && typeof y == "object" && (y = JSON.stringify(y));
|
|
781
|
-
const
|
|
782
|
-
S.replaceWith(
|
|
783
|
-
}, [e._stateKey, e._path.join("."), e._effect]),
|
|
791
|
+
const C = document.createTextNode(String(y));
|
|
792
|
+
S.replaceWith(C);
|
|
793
|
+
}, [e._stateKey, e._path.join("."), e._effect]), Q("span", {
|
|
784
794
|
ref: i,
|
|
785
795
|
style: { display: "none" },
|
|
786
796
|
"data-signal-id": v
|
|
787
797
|
});
|
|
788
798
|
}
|
|
789
|
-
function
|
|
790
|
-
const i =
|
|
799
|
+
function Ye(e) {
|
|
800
|
+
const i = we(
|
|
791
801
|
(v) => {
|
|
792
802
|
const S = r.getState().stateComponents.get(e._stateKey) || {
|
|
793
803
|
components: /* @__PURE__ */ new Map()
|
|
@@ -799,14 +809,14 @@ function Je(e) {
|
|
|
799
809
|
},
|
|
800
810
|
() => r.getState().getNestedState(e._stateKey, e._path)
|
|
801
811
|
);
|
|
802
|
-
return
|
|
812
|
+
return Q("text", {}, String(i));
|
|
803
813
|
}
|
|
804
814
|
export {
|
|
805
815
|
te as $cogsSignal,
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
816
|
+
Ye as $cogsSignalStore,
|
|
817
|
+
Je as addStateOptions,
|
|
818
|
+
Ze as createCogsState,
|
|
819
|
+
He as notifyComponent,
|
|
820
|
+
je as useCogsStateFn
|
|
811
821
|
};
|
|
812
822
|
//# sourceMappingURL=CogsState.jsx.map
|