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