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