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