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