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