cogsbox-state 0.5.69 → 0.5.71
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.d.ts +1 -0
- package/dist/CogsState.jsx +197 -190
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
package/dist/CogsState.jsx
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as ct } from "react/jsx-runtime";
|
|
3
|
-
import { useState as et, useRef as
|
|
4
|
-
import { transformStateFunc as wt, isFunction as
|
|
5
|
-
import { pushFunc as K, updateFn as
|
|
3
|
+
import { useState as et, useRef as Z, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Q, useSyncExternalStore as Et, startTransition as H } from "react";
|
|
4
|
+
import { transformStateFunc as wt, isFunction as G, getNestedValue as U, isDeepEqual as M, debounce as Nt } from "./utility.js";
|
|
5
|
+
import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Vt, FormControlComponent as $t } from "./Functions.jsx";
|
|
6
6
|
import "zod";
|
|
7
7
|
import { getGlobalStore as r, formRefStore as lt } from "./store.js";
|
|
8
8
|
import { useCogsConfig as At } from "./CogsStateClient.jsx";
|
|
9
9
|
import rt from "./node_modules/uuid/dist/esm-browser/v4.js";
|
|
10
|
-
function dt(t,
|
|
10
|
+
function dt(t, i) {
|
|
11
11
|
const S = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, g = S(t) || {};
|
|
12
12
|
y(t, {
|
|
13
13
|
...g,
|
|
14
|
-
...
|
|
14
|
+
...i
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
function ut({
|
|
18
18
|
stateKey: t,
|
|
19
|
-
options:
|
|
19
|
+
options: i,
|
|
20
20
|
initialOptionsPart: S
|
|
21
21
|
}) {
|
|
22
|
-
const y =
|
|
22
|
+
const y = q(t) || {}, g = S[t] || {}, w = r.getState().setInitialStateOptions, _ = { ...g, ...y };
|
|
23
23
|
let v = !1;
|
|
24
|
-
if (
|
|
25
|
-
for (const l in
|
|
26
|
-
_.hasOwnProperty(l) ? l == "localStorage" &&
|
|
24
|
+
if (i)
|
|
25
|
+
for (const l in i)
|
|
26
|
+
_.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (v = !0, _[l] = i[l]) : (v = !0, _[l] = i[l]);
|
|
27
27
|
v && w(t, _);
|
|
28
28
|
}
|
|
29
|
-
function qt(t, { formElements:
|
|
30
|
-
return { initialState: t, formElements:
|
|
29
|
+
function qt(t, { formElements: i, validation: S }) {
|
|
30
|
+
return { initialState: t, formElements: i, validation: S };
|
|
31
31
|
}
|
|
32
|
-
const zt = (t,
|
|
32
|
+
const zt = (t, i) => {
|
|
33
33
|
let S = t;
|
|
34
34
|
const [y, g] = wt(S);
|
|
35
|
-
(
|
|
35
|
+
(i?.formElements || i?.validation) && Object.keys(g).forEach((v) => {
|
|
36
36
|
g[v] = g[v] || {}, g[v].formElements = {
|
|
37
|
-
...
|
|
37
|
+
...i.formElements,
|
|
38
38
|
// Global defaults first
|
|
39
|
-
...
|
|
39
|
+
...i?.validation,
|
|
40
40
|
...g[v].formElements || {}
|
|
41
41
|
// State-specific overrides
|
|
42
42
|
};
|
|
@@ -48,7 +48,7 @@ const zt = (t, a) => {
|
|
|
48
48
|
options: l,
|
|
49
49
|
initialOptionsPart: g
|
|
50
50
|
});
|
|
51
|
-
const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [
|
|
51
|
+
const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [h, V] = Ft(
|
|
52
52
|
m,
|
|
53
53
|
{
|
|
54
54
|
stateKey: v,
|
|
@@ -70,44 +70,44 @@ const zt = (t, a) => {
|
|
|
70
70
|
}
|
|
71
71
|
return { useCogsState: w, setCogsOptions: _ };
|
|
72
72
|
}, {
|
|
73
|
-
setUpdaterState:
|
|
74
|
-
setState:
|
|
75
|
-
getInitialOptions:
|
|
73
|
+
setUpdaterState: Y,
|
|
74
|
+
setState: L,
|
|
75
|
+
getInitialOptions: q,
|
|
76
76
|
getKeyState: gt,
|
|
77
77
|
getValidationErrors: kt,
|
|
78
78
|
setStateLog: pt,
|
|
79
79
|
updateInitialStateGlobal: ot,
|
|
80
80
|
addValidationError: Ct,
|
|
81
|
-
removeValidationError:
|
|
82
|
-
setServerSyncActions:
|
|
81
|
+
removeValidationError: R,
|
|
82
|
+
setServerSyncActions: Tt
|
|
83
83
|
} = r.getState(), ft = (t) => {
|
|
84
84
|
if (!t) return null;
|
|
85
85
|
try {
|
|
86
|
-
const
|
|
87
|
-
return
|
|
88
|
-
} catch (
|
|
89
|
-
return console.error("Error loading from localStorage:",
|
|
86
|
+
const i = window.localStorage.getItem(t);
|
|
87
|
+
return i ? JSON.parse(i) : null;
|
|
88
|
+
} catch (i) {
|
|
89
|
+
return console.error("Error loading from localStorage:", i), null;
|
|
90
90
|
}
|
|
91
|
-
},
|
|
91
|
+
}, ht = (t, i, S, y) => {
|
|
92
92
|
S.log && console.log(
|
|
93
93
|
"saving to localstorage",
|
|
94
|
-
|
|
94
|
+
i,
|
|
95
95
|
S.localStorage?.key,
|
|
96
96
|
y
|
|
97
97
|
);
|
|
98
|
-
const g =
|
|
98
|
+
const g = G(S.localStorage?.key) ? S.localStorage?.key(t) : S.localStorage?.key;
|
|
99
99
|
if (g && y) {
|
|
100
100
|
const w = {
|
|
101
101
|
state: t,
|
|
102
102
|
lastUpdated: Date.now(),
|
|
103
|
-
lastSyncedWithServer: r.getState().serverSyncLog[
|
|
104
|
-
baseServerState: r.getState().serverState[
|
|
105
|
-
}, _ = `${y}-${
|
|
103
|
+
lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
|
|
104
|
+
baseServerState: r.getState().serverState[i]
|
|
105
|
+
}, _ = `${y}-${i}-${g}`;
|
|
106
106
|
window.localStorage.setItem(_, JSON.stringify(w));
|
|
107
107
|
}
|
|
108
|
-
},
|
|
108
|
+
}, bt = (t, i, S, y, g, w) => {
|
|
109
109
|
const _ = {
|
|
110
|
-
initialState:
|
|
110
|
+
initialState: i,
|
|
111
111
|
updaterState: X(
|
|
112
112
|
t,
|
|
113
113
|
y,
|
|
@@ -116,29 +116,29 @@ const zt = (t, a) => {
|
|
|
116
116
|
),
|
|
117
117
|
state: S
|
|
118
118
|
};
|
|
119
|
-
|
|
120
|
-
ot(t, _.initialState),
|
|
119
|
+
H(() => {
|
|
120
|
+
ot(t, _.initialState), Y(t, _.updaterState), L(t, _.state);
|
|
121
121
|
});
|
|
122
122
|
}, St = (t) => {
|
|
123
|
-
const
|
|
124
|
-
if (!
|
|
123
|
+
const i = r.getState().stateComponents.get(t);
|
|
124
|
+
if (!i) return;
|
|
125
125
|
const S = /* @__PURE__ */ new Set();
|
|
126
|
-
|
|
126
|
+
i.components.forEach((y) => {
|
|
127
127
|
S.add(() => y.forceUpdate());
|
|
128
128
|
}), queueMicrotask(() => {
|
|
129
|
-
|
|
129
|
+
H(() => {
|
|
130
130
|
S.forEach((y) => y());
|
|
131
131
|
});
|
|
132
132
|
});
|
|
133
|
-
}, Bt = (t,
|
|
133
|
+
}, Bt = (t, i) => {
|
|
134
134
|
const S = r.getState().stateComponents.get(t);
|
|
135
135
|
if (S) {
|
|
136
|
-
const y = `${t}////${
|
|
136
|
+
const y = `${t}////${i}`, g = S.components.get(y);
|
|
137
137
|
g && g.forceUpdate();
|
|
138
138
|
}
|
|
139
139
|
};
|
|
140
|
-
function
|
|
141
|
-
stateKey:
|
|
140
|
+
function Ft(t, {
|
|
141
|
+
stateKey: i,
|
|
142
142
|
serverSync: S,
|
|
143
143
|
localStorage: y,
|
|
144
144
|
formElements: g,
|
|
@@ -150,12 +150,12 @@ function bt(t, {
|
|
|
150
150
|
syncUpdate: e,
|
|
151
151
|
dependencies: m
|
|
152
152
|
} = {}) {
|
|
153
|
-
const [
|
|
154
|
-
let j = !
|
|
155
|
-
const [u] = et(
|
|
156
|
-
C.current =
|
|
153
|
+
const [h, V] = et({}), { sessionId: A } = At();
|
|
154
|
+
let j = !i;
|
|
155
|
+
const [u] = et(i ?? rt()), s = r.getState().stateLog[u], D = Z(/* @__PURE__ */ new Set()), x = Z(l ?? rt()), C = Z(null);
|
|
156
|
+
C.current = q(u), nt(() => {
|
|
157
157
|
if (e && e.stateKey === u && e.path?.[0]) {
|
|
158
|
-
|
|
158
|
+
L(u, (o) => ({
|
|
159
159
|
...o,
|
|
160
160
|
[e.path[0]]: e.newValue
|
|
161
161
|
}));
|
|
@@ -172,17 +172,17 @@ function bt(t, {
|
|
|
172
172
|
const c = C.current;
|
|
173
173
|
let o = null;
|
|
174
174
|
c.log && console.log("newoptions", c);
|
|
175
|
-
const d =
|
|
175
|
+
const d = G(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
|
|
176
176
|
d && A && (o = ft(
|
|
177
177
|
A + "-" + u + "-" + d
|
|
178
178
|
));
|
|
179
179
|
let E = null;
|
|
180
|
-
f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state),
|
|
180
|
+
f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), bt(
|
|
181
181
|
u,
|
|
182
182
|
f,
|
|
183
183
|
E,
|
|
184
184
|
n,
|
|
185
|
-
|
|
185
|
+
x.current,
|
|
186
186
|
A
|
|
187
187
|
), St(u), V({}));
|
|
188
188
|
}, [f, ...m || []]), It(() => {
|
|
@@ -193,7 +193,7 @@ function bt(t, {
|
|
|
193
193
|
localStorage: y,
|
|
194
194
|
middleware: w
|
|
195
195
|
});
|
|
196
|
-
const c = `${u}////${
|
|
196
|
+
const c = `${u}////${x.current}`, o = r.getState().stateComponents.get(u) || {
|
|
197
197
|
components: /* @__PURE__ */ new Map()
|
|
198
198
|
};
|
|
199
199
|
return o.components.set(c, {
|
|
@@ -203,35 +203,35 @@ function bt(t, {
|
|
|
203
203
|
depsFunction: _ || void 0,
|
|
204
204
|
reactiveType: v ?? ["component", "deps"]
|
|
205
205
|
}), r.getState().stateComponents.set(u, o), V({}), () => {
|
|
206
|
-
const d = `${u}////${
|
|
206
|
+
const d = `${u}////${x.current}`;
|
|
207
207
|
o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(u));
|
|
208
208
|
};
|
|
209
209
|
}, []);
|
|
210
210
|
const n = (c, o, d, E) => {
|
|
211
211
|
if (Array.isArray(o)) {
|
|
212
|
-
const
|
|
213
|
-
|
|
212
|
+
const b = `${u}-${o.join(".")}`;
|
|
213
|
+
D.current.add(b);
|
|
214
214
|
}
|
|
215
|
-
|
|
216
|
-
const $ =
|
|
217
|
-
if (
|
|
218
|
-
let
|
|
215
|
+
L(u, (b) => {
|
|
216
|
+
const $ = G(c) ? c(b) : c, O = `${u}-${o.join(".")}`;
|
|
217
|
+
if (O) {
|
|
218
|
+
let P = !1, I = r.getState().signalDomElements.get(O);
|
|
219
219
|
if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
|
|
220
220
|
const k = o.slice(0, -1), p = U($, k);
|
|
221
221
|
if (Array.isArray(p)) {
|
|
222
|
-
|
|
222
|
+
P = !0;
|
|
223
223
|
const N = `${u}-${k.join(".")}`;
|
|
224
224
|
I = r.getState().signalDomElements.get(N);
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
if (I) {
|
|
228
|
-
const k =
|
|
228
|
+
const k = P ? U($, o.slice(0, -1)) : U($, o);
|
|
229
229
|
I.forEach(({ parentId: p, position: N, effect: W }) => {
|
|
230
|
-
const
|
|
230
|
+
const F = document.querySelector(
|
|
231
231
|
`[data-parent-id="${p}"]`
|
|
232
232
|
);
|
|
233
|
-
if (
|
|
234
|
-
const st = Array.from(
|
|
233
|
+
if (F) {
|
|
234
|
+
const st = Array.from(F.childNodes);
|
|
235
235
|
if (st[N]) {
|
|
236
236
|
const vt = W ? new Function("state", `return (${W})(state)`)(k) : k;
|
|
237
237
|
st[N].textContent = String(vt);
|
|
@@ -240,24 +240,24 @@ function bt(t, {
|
|
|
240
240
|
});
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
|
-
d.updateType === "update" && (E || C.current?.validationKey) && o &&
|
|
243
|
+
d.updateType === "update" && (E || C.current?.validationKey) && o && R(
|
|
244
244
|
(E || C.current?.validationKey) + "." + o.join(".")
|
|
245
245
|
);
|
|
246
|
-
const
|
|
247
|
-
d.updateType === "cut" && C.current?.validationKey &&
|
|
248
|
-
C.current?.validationKey + "." +
|
|
246
|
+
const T = o.slice(0, o.length - 1);
|
|
247
|
+
d.updateType === "cut" && C.current?.validationKey && R(
|
|
248
|
+
C.current?.validationKey + "." + T.join(".")
|
|
249
249
|
), d.updateType === "insert" && C.current?.validationKey && kt(
|
|
250
|
-
C.current?.validationKey + "." +
|
|
250
|
+
C.current?.validationKey + "." + T.join(".")
|
|
251
251
|
).filter(([I, k]) => {
|
|
252
252
|
let p = I?.split(".").length;
|
|
253
|
-
if (I ==
|
|
254
|
-
let N = I + "." +
|
|
255
|
-
|
|
253
|
+
if (I == T.join(".") && p == T.length - 1) {
|
|
254
|
+
let N = I + "." + T;
|
|
255
|
+
R(I), Ct(N, k);
|
|
256
256
|
}
|
|
257
257
|
});
|
|
258
|
-
const
|
|
258
|
+
const z = U(b, o), mt = U($, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
|
|
259
259
|
if (at)
|
|
260
|
-
for (const [
|
|
260
|
+
for (const [P, I] of at.components.entries()) {
|
|
261
261
|
let k = !1;
|
|
262
262
|
const p = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
|
|
263
263
|
if (!p.includes("none")) {
|
|
@@ -267,7 +267,7 @@ function bt(t, {
|
|
|
267
267
|
}
|
|
268
268
|
if (p.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (k = !0), !k && p.includes("deps") && I.depsFunction) {
|
|
269
269
|
const N = I.depsFunction($);
|
|
270
|
-
typeof N == "boolean" ? N && (k = !0) :
|
|
270
|
+
typeof N == "boolean" ? N && (k = !0) : M(I.deps, N) || (I.deps = N, k = !0);
|
|
271
271
|
}
|
|
272
272
|
k && I.forceUpdate();
|
|
273
273
|
}
|
|
@@ -278,16 +278,16 @@ function bt(t, {
|
|
|
278
278
|
path: o,
|
|
279
279
|
updateType: d.updateType,
|
|
280
280
|
status: "new",
|
|
281
|
-
oldValue:
|
|
281
|
+
oldValue: z,
|
|
282
282
|
newValue: mt
|
|
283
283
|
};
|
|
284
|
-
if (pt(u, (
|
|
285
|
-
const k = [...
|
|
286
|
-
const W = `${N.stateKey}:${JSON.stringify(N.path)}`,
|
|
287
|
-
return
|
|
284
|
+
if (pt(u, (P) => {
|
|
285
|
+
const k = [...P ?? [], it].reduce((p, N) => {
|
|
286
|
+
const W = `${N.stateKey}:${JSON.stringify(N.path)}`, F = p.get(W);
|
|
287
|
+
return F ? (F.timeStamp = Math.max(F.timeStamp, N.timeStamp), F.newValue = N.newValue, F.oldValue = F.oldValue ?? N.oldValue, F.updateType = N.updateType) : p.set(W, { ...N }), p;
|
|
288
288
|
}, /* @__PURE__ */ new Map());
|
|
289
289
|
return Array.from(k.values());
|
|
290
|
-
}),
|
|
290
|
+
}), ht(
|
|
291
291
|
$,
|
|
292
292
|
u,
|
|
293
293
|
C.current,
|
|
@@ -296,10 +296,10 @@ function bt(t, {
|
|
|
296
296
|
updateLog: s,
|
|
297
297
|
update: it
|
|
298
298
|
}), C.current?.serverSync) {
|
|
299
|
-
const
|
|
300
|
-
|
|
299
|
+
const P = r.getState().serverState[u], I = C.current?.serverSync;
|
|
300
|
+
Tt(u, {
|
|
301
301
|
syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: $ }),
|
|
302
|
-
rollBackState:
|
|
302
|
+
rollBackState: P,
|
|
303
303
|
actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
|
|
304
304
|
status: "waiting"
|
|
305
305
|
});
|
|
@@ -307,24 +307,24 @@ function bt(t, {
|
|
|
307
307
|
return $;
|
|
308
308
|
});
|
|
309
309
|
};
|
|
310
|
-
r.getState().updaterState[u] || (
|
|
310
|
+
r.getState().updaterState[u] || (Y(
|
|
311
311
|
u,
|
|
312
312
|
X(
|
|
313
313
|
u,
|
|
314
314
|
n,
|
|
315
|
-
|
|
315
|
+
x.current,
|
|
316
316
|
A
|
|
317
317
|
)
|
|
318
|
-
), r.getState().cogsStateStore[u] ||
|
|
319
|
-
const
|
|
318
|
+
), r.getState().cogsStateStore[u] || L(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
|
|
319
|
+
const a = _t(() => X(
|
|
320
320
|
u,
|
|
321
321
|
n,
|
|
322
|
-
|
|
322
|
+
x.current,
|
|
323
323
|
A
|
|
324
324
|
), [u]);
|
|
325
|
-
return [gt(u),
|
|
325
|
+
return [gt(u), a];
|
|
326
326
|
}
|
|
327
|
-
function X(t,
|
|
327
|
+
function X(t, i, S, y) {
|
|
328
328
|
const g = /* @__PURE__ */ new Map();
|
|
329
329
|
let w = 0;
|
|
330
330
|
const _ = (f) => {
|
|
@@ -334,16 +334,16 @@ function X(t, a, S, y) {
|
|
|
334
334
|
w++;
|
|
335
335
|
}, v = {
|
|
336
336
|
removeValidation: (f) => {
|
|
337
|
-
f?.validationKey &&
|
|
337
|
+
f?.validationKey && R(f.validationKey);
|
|
338
338
|
},
|
|
339
339
|
revertToInitialState: (f) => {
|
|
340
340
|
const e = r.getState().getInitialOptions(t)?.validation;
|
|
341
|
-
e?.key &&
|
|
341
|
+
e?.key && R(e?.key), f?.validationKey && R(f.validationKey);
|
|
342
342
|
const m = r.getState().initialStateGlobal[t];
|
|
343
343
|
g.clear(), w++;
|
|
344
|
-
const
|
|
345
|
-
return A && localStorage.removeItem(A),
|
|
346
|
-
|
|
344
|
+
const h = l(m, []), V = q(t), A = G(V?.localStorage?.key) ? V?.localStorage?.key(m) : V?.localStorage?.key;
|
|
345
|
+
return A && localStorage.removeItem(A), H(() => {
|
|
346
|
+
Y(t, h), L(t, m);
|
|
347
347
|
const j = r.getState().stateComponents.get(t);
|
|
348
348
|
j && j.components.forEach((u) => {
|
|
349
349
|
u.forceUpdate();
|
|
@@ -352,16 +352,16 @@ function X(t, a, S, y) {
|
|
|
352
352
|
},
|
|
353
353
|
updateInitialState: (f) => {
|
|
354
354
|
g.clear(), w++;
|
|
355
|
-
const e = r.getState().initialStateGlobal[t], m =
|
|
356
|
-
|
|
355
|
+
const e = r.getState().initialStateGlobal[t], m = q(t), h = G(m?.localStorage?.key) ? m?.localStorage?.key(e) : m?.localStorage?.key;
|
|
356
|
+
h && localStorage.removeItem(h);
|
|
357
357
|
const V = X(
|
|
358
358
|
t,
|
|
359
|
-
|
|
359
|
+
i,
|
|
360
360
|
S,
|
|
361
361
|
y
|
|
362
362
|
);
|
|
363
|
-
return
|
|
364
|
-
ot(t, f),
|
|
363
|
+
return H(() => {
|
|
364
|
+
ot(t, f), Y(t, V), L(t, f);
|
|
365
365
|
const A = r.getState().stateComponents.get(t);
|
|
366
366
|
A && A.components.forEach((j) => {
|
|
367
367
|
j.forceUpdate();
|
|
@@ -375,12 +375,12 @@ function X(t, a, S, y) {
|
|
|
375
375
|
_isLoading: r.getState().isLoadingGlobal[t],
|
|
376
376
|
_isServerSynced: () => {
|
|
377
377
|
const f = r.getState().serverState[t];
|
|
378
|
-
return !!(f &&
|
|
378
|
+
return !!(f && M(f, gt(t)));
|
|
379
379
|
}
|
|
380
380
|
};
|
|
381
381
|
function l(f, e = [], m) {
|
|
382
|
-
const
|
|
383
|
-
g.get(
|
|
382
|
+
const h = e.map(String).join(".");
|
|
383
|
+
g.get(h);
|
|
384
384
|
const V = function() {
|
|
385
385
|
return r().getNestedState(t, e);
|
|
386
386
|
};
|
|
@@ -388,28 +388,35 @@ function X(t, a, S, y) {
|
|
|
388
388
|
V[u] = v[u];
|
|
389
389
|
});
|
|
390
390
|
const A = {
|
|
391
|
-
apply(u, s,
|
|
392
|
-
return
|
|
391
|
+
apply(u, s, D) {
|
|
392
|
+
return console.log(
|
|
393
|
+
`PROXY APPLY TRAP HIT: stateKey=${t}, path=${e.join(".")}`
|
|
394
|
+
), console.trace("Apply trap stack trace"), r().getNestedState(t, e);
|
|
393
395
|
},
|
|
394
396
|
get(u, s) {
|
|
395
397
|
if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
|
|
396
|
-
const n = e.join("."),
|
|
398
|
+
const n = e.join("."), a = `${t}////${S}`, c = r.getState().stateComponents.get(t);
|
|
397
399
|
if (c) {
|
|
398
|
-
const o = c.components.get(
|
|
400
|
+
const o = c.components.get(a);
|
|
399
401
|
o && (e.length > 0 || s === "get") && o.paths.add(n);
|
|
400
402
|
}
|
|
401
403
|
}
|
|
402
404
|
if (s === "_status") {
|
|
403
|
-
const n = r.getState().getNestedState(t, e),
|
|
404
|
-
return
|
|
405
|
+
const n = r.getState().getNestedState(t, e), a = r.getState().initialStateGlobal[t], c = U(a, e);
|
|
406
|
+
return M(n, c) ? "fresh" : "stale";
|
|
405
407
|
}
|
|
406
408
|
if (s === "getStatus")
|
|
407
409
|
return function() {
|
|
408
410
|
const n = r().getNestedState(
|
|
409
411
|
t,
|
|
410
412
|
e
|
|
411
|
-
),
|
|
412
|
-
return
|
|
413
|
+
), a = r.getState().initialStateGlobal[t], c = U(a, e);
|
|
414
|
+
return M(n, c) ? "fresh" : "stale";
|
|
415
|
+
};
|
|
416
|
+
if (s === "removeStorage")
|
|
417
|
+
return () => {
|
|
418
|
+
const n = r.getState().initialStateGlobal[t], a = q(t), c = G(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key;
|
|
419
|
+
c && localStorage.removeItem(c);
|
|
413
420
|
};
|
|
414
421
|
if (s === "showValidationErrors")
|
|
415
422
|
return () => {
|
|
@@ -444,18 +451,18 @@ function X(t, a, S, y) {
|
|
|
444
451
|
};
|
|
445
452
|
if (s === "stateMap" || s === "stateMapNoRender")
|
|
446
453
|
return (n) => {
|
|
447
|
-
const
|
|
454
|
+
const a = m?.filtered?.some(
|
|
448
455
|
(o) => o.join(".") === e.join(".")
|
|
449
|
-
), c =
|
|
456
|
+
), c = a ? f : r.getState().getNestedState(t, e);
|
|
450
457
|
return s !== "stateMapNoRender" && (g.clear(), w++), c.map((o, d) => {
|
|
451
|
-
const E =
|
|
458
|
+
const E = a && o.__origIndex ? o.__origIndex : d, b = l(
|
|
452
459
|
o,
|
|
453
460
|
[...e, E.toString()],
|
|
454
461
|
m
|
|
455
462
|
);
|
|
456
463
|
return n(
|
|
457
464
|
o,
|
|
458
|
-
|
|
465
|
+
b,
|
|
459
466
|
d,
|
|
460
467
|
f,
|
|
461
468
|
l(f, e, m)
|
|
@@ -463,7 +470,7 @@ function X(t, a, S, y) {
|
|
|
463
470
|
});
|
|
464
471
|
};
|
|
465
472
|
if (s === "$stateMap")
|
|
466
|
-
return (n) =>
|
|
473
|
+
return (n) => Q(jt, {
|
|
467
474
|
proxy: {
|
|
468
475
|
_stateKey: t,
|
|
469
476
|
_path: e,
|
|
@@ -488,9 +495,9 @@ function X(t, a, S, y) {
|
|
|
488
495
|
);
|
|
489
496
|
};
|
|
490
497
|
if (s === "findWith")
|
|
491
|
-
return (n,
|
|
498
|
+
return (n, a) => {
|
|
492
499
|
const c = f.findIndex(
|
|
493
|
-
(E) => E[n] ===
|
|
500
|
+
(E) => E[n] === a
|
|
494
501
|
);
|
|
495
502
|
if (c === -1) return;
|
|
496
503
|
const o = f[c], d = [...e, c.toString()];
|
|
@@ -498,58 +505,58 @@ function X(t, a, S, y) {
|
|
|
498
505
|
};
|
|
499
506
|
if (s === "index")
|
|
500
507
|
return (n) => {
|
|
501
|
-
const
|
|
502
|
-
return l(
|
|
508
|
+
const a = f[n];
|
|
509
|
+
return l(a, [...e, n.toString()]);
|
|
503
510
|
};
|
|
504
511
|
if (s === "insert")
|
|
505
|
-
return (n) => (_(e), K(
|
|
512
|
+
return (n) => (_(e), K(i, n, e, t), l(
|
|
506
513
|
r.getState().cogsStateStore[t],
|
|
507
514
|
[]
|
|
508
515
|
));
|
|
509
516
|
if (s === "uniqueInsert")
|
|
510
|
-
return (n,
|
|
511
|
-
const o = r.getState().getNestedState(t, e), d =
|
|
517
|
+
return (n, a, c) => {
|
|
518
|
+
const o = r.getState().getNestedState(t, e), d = G(n) ? n(o) : n;
|
|
512
519
|
let E = null;
|
|
513
520
|
if (!o.some(($) => {
|
|
514
|
-
if (
|
|
515
|
-
const
|
|
516
|
-
(
|
|
521
|
+
if (a) {
|
|
522
|
+
const T = a.every(
|
|
523
|
+
(z) => M($[z], d[z])
|
|
517
524
|
);
|
|
518
|
-
return
|
|
525
|
+
return T && (E = $), T;
|
|
519
526
|
}
|
|
520
|
-
const
|
|
521
|
-
return
|
|
527
|
+
const O = M($, d);
|
|
528
|
+
return O && (E = $), O;
|
|
522
529
|
}))
|
|
523
|
-
_(e), K(
|
|
530
|
+
_(e), K(i, d, e, t);
|
|
524
531
|
else if (c && E) {
|
|
525
|
-
const $ = c(E),
|
|
526
|
-
(
|
|
532
|
+
const $ = c(E), O = o.map(
|
|
533
|
+
(T) => M(T, E) ? $ : T
|
|
527
534
|
);
|
|
528
|
-
_(e),
|
|
535
|
+
_(e), B(i, O, e);
|
|
529
536
|
}
|
|
530
537
|
};
|
|
531
538
|
if (s === "cut")
|
|
532
|
-
return (n,
|
|
533
|
-
|
|
539
|
+
return (n, a) => {
|
|
540
|
+
a?.waitForSync || (_(e), J(i, e, t, n));
|
|
534
541
|
};
|
|
535
542
|
if (s === "cutByValue")
|
|
536
543
|
return (n) => {
|
|
537
|
-
for (let
|
|
538
|
-
f[
|
|
544
|
+
for (let a = 0; a < f.length; a++)
|
|
545
|
+
f[a] === n && J(i, e, t, a);
|
|
539
546
|
};
|
|
540
547
|
if (s === "toggleByValue")
|
|
541
548
|
return (n) => {
|
|
542
|
-
const
|
|
543
|
-
|
|
549
|
+
const a = f.findIndex((c) => c === n);
|
|
550
|
+
a > -1 ? J(i, e, t, a) : K(i, n, e, t);
|
|
544
551
|
};
|
|
545
552
|
if (s === "stateFilter")
|
|
546
553
|
return (n) => {
|
|
547
|
-
const
|
|
554
|
+
const a = f.map((d, E) => ({
|
|
548
555
|
...d,
|
|
549
556
|
__origIndex: E.toString()
|
|
550
557
|
})), c = [], o = [];
|
|
551
|
-
for (let d = 0; d <
|
|
552
|
-
n(
|
|
558
|
+
for (let d = 0; d < a.length; d++)
|
|
559
|
+
n(a[d], d) && (c.push(d), o.push(a[d]));
|
|
553
560
|
return g.clear(), w++, l(o, e, {
|
|
554
561
|
filtered: [...m?.filtered || [], e],
|
|
555
562
|
validIndices: c
|
|
@@ -557,15 +564,15 @@ function X(t, a, S, y) {
|
|
|
557
564
|
});
|
|
558
565
|
};
|
|
559
566
|
}
|
|
560
|
-
const
|
|
561
|
-
if (!isNaN(Number(
|
|
562
|
-
const n = e.slice(0, -1),
|
|
563
|
-
if (Array.isArray(
|
|
564
|
-
return () =>
|
|
565
|
-
|
|
567
|
+
const D = e[e.length - 1];
|
|
568
|
+
if (!isNaN(Number(D))) {
|
|
569
|
+
const n = e.slice(0, -1), a = r.getState().getNestedState(t, n);
|
|
570
|
+
if (Array.isArray(a) && s === "cut")
|
|
571
|
+
return () => J(
|
|
572
|
+
i,
|
|
566
573
|
n,
|
|
567
574
|
t,
|
|
568
|
-
Number(
|
|
575
|
+
Number(D)
|
|
569
576
|
);
|
|
570
577
|
}
|
|
571
578
|
if (s === "get")
|
|
@@ -594,35 +601,35 @@ function X(t, a, S, y) {
|
|
|
594
601
|
if (s == "getLocalStorage")
|
|
595
602
|
return (n) => ft(y + "-" + t + "-" + n);
|
|
596
603
|
if (s === "_selected") {
|
|
597
|
-
const n = e.slice(0, -1),
|
|
598
|
-
return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t,
|
|
604
|
+
const n = e.slice(0, -1), a = n.join("."), c = r.getState().getNestedState(t, n);
|
|
605
|
+
return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, a) : void 0;
|
|
599
606
|
}
|
|
600
607
|
if (s === "setSelected")
|
|
601
608
|
return (n) => {
|
|
602
|
-
const
|
|
609
|
+
const a = e.slice(0, -1), c = Number(e[e.length - 1]), o = a.join(".");
|
|
603
610
|
n ? r.getState().setSelectedIndex(t, o, c) : r.getState().setSelectedIndex(t, o, void 0);
|
|
604
|
-
const d = r.getState().getNestedState(t, [...
|
|
605
|
-
|
|
611
|
+
const d = r.getState().getNestedState(t, [...a]);
|
|
612
|
+
B(i, d, a), _(a);
|
|
606
613
|
};
|
|
607
614
|
if (e.length == 0) {
|
|
608
615
|
if (s === "validateZodSchema")
|
|
609
616
|
return () => {
|
|
610
|
-
const n = r.getState().getInitialOptions(t)?.validation,
|
|
617
|
+
const n = r.getState().getInitialOptions(t)?.validation, a = r.getState().addValidationError;
|
|
611
618
|
if (!n?.zodSchema)
|
|
612
619
|
throw new Error("Zod schema not found");
|
|
613
620
|
if (!n?.key)
|
|
614
621
|
throw new Error("Validation key not found");
|
|
615
|
-
|
|
622
|
+
R(n.key);
|
|
616
623
|
const c = r.getState().cogsStateStore[t];
|
|
617
624
|
try {
|
|
618
625
|
const o = r.getState().getValidationErrors(n.key);
|
|
619
626
|
o && o.length > 0 && o.forEach(([E]) => {
|
|
620
|
-
E && E.startsWith(n.key) &&
|
|
627
|
+
E && E.startsWith(n.key) && R(E);
|
|
621
628
|
});
|
|
622
629
|
const d = n.zodSchema.safeParse(c);
|
|
623
|
-
return d.success ? !0 : (d.error.errors.forEach((
|
|
624
|
-
const $ =
|
|
625
|
-
|
|
630
|
+
return d.success ? !0 : (d.error.errors.forEach((b) => {
|
|
631
|
+
const $ = b.path, O = b.message, T = [n.key, ...$].join(".");
|
|
632
|
+
a(T, O);
|
|
626
633
|
}), St(t), !1);
|
|
627
634
|
} catch (o) {
|
|
628
635
|
return console.error("Zod schema validation failed", o), !1;
|
|
@@ -649,11 +656,11 @@ function X(t, a, S, y) {
|
|
|
649
656
|
if (s === "validationWrapper")
|
|
650
657
|
return ({
|
|
651
658
|
children: n,
|
|
652
|
-
hideMessage:
|
|
659
|
+
hideMessage: a
|
|
653
660
|
}) => /* @__PURE__ */ ct(
|
|
654
661
|
Vt,
|
|
655
662
|
{
|
|
656
|
-
formOpts:
|
|
663
|
+
formOpts: a ? { validation: { message: "" } } : void 0,
|
|
657
664
|
path: e,
|
|
658
665
|
validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
|
|
659
666
|
stateKey: t,
|
|
@@ -665,36 +672,36 @@ function X(t, a, S, y) {
|
|
|
665
672
|
if (s === "_path") return e;
|
|
666
673
|
if (s === "_isServerSynced") return v._isServerSynced;
|
|
667
674
|
if (s === "update")
|
|
668
|
-
return (n,
|
|
669
|
-
if (
|
|
675
|
+
return (n, a) => {
|
|
676
|
+
if (a?.debounce)
|
|
670
677
|
Nt(() => {
|
|
671
|
-
|
|
678
|
+
B(i, n, e, "");
|
|
672
679
|
const c = r.getState().getNestedState(t, e);
|
|
673
|
-
|
|
674
|
-
},
|
|
680
|
+
a?.afterUpdate && a.afterUpdate(c);
|
|
681
|
+
}, a.debounce);
|
|
675
682
|
else {
|
|
676
|
-
|
|
683
|
+
B(i, n, e, "");
|
|
677
684
|
const c = r.getState().getNestedState(t, e);
|
|
678
|
-
|
|
685
|
+
a?.afterUpdate && a.afterUpdate(c);
|
|
679
686
|
}
|
|
680
687
|
_(e);
|
|
681
688
|
};
|
|
682
689
|
if (s === "formElement")
|
|
683
|
-
return (n,
|
|
690
|
+
return (n, a) => /* @__PURE__ */ ct(
|
|
684
691
|
$t,
|
|
685
692
|
{
|
|
686
|
-
setState:
|
|
693
|
+
setState: i,
|
|
687
694
|
stateKey: t,
|
|
688
695
|
path: e,
|
|
689
696
|
child: n,
|
|
690
|
-
formOpts:
|
|
697
|
+
formOpts: a
|
|
691
698
|
}
|
|
692
699
|
);
|
|
693
|
-
const
|
|
694
|
-
return l(C,
|
|
700
|
+
const x = [...e, s], C = r.getState().getNestedState(t, x);
|
|
701
|
+
return l(C, x, m);
|
|
695
702
|
}
|
|
696
703
|
}, j = new Proxy(V, A);
|
|
697
|
-
return g.set(
|
|
704
|
+
return g.set(h, {
|
|
698
705
|
proxy: j,
|
|
699
706
|
stateVersion: w
|
|
700
707
|
}), j;
|
|
@@ -704,26 +711,26 @@ function X(t, a, S, y) {
|
|
|
704
711
|
);
|
|
705
712
|
}
|
|
706
713
|
function tt(t) {
|
|
707
|
-
return
|
|
714
|
+
return Q(Ot, { proxy: t });
|
|
708
715
|
}
|
|
709
716
|
function jt({
|
|
710
717
|
proxy: t,
|
|
711
|
-
rebuildStateShape:
|
|
718
|
+
rebuildStateShape: i
|
|
712
719
|
}) {
|
|
713
720
|
const S = r().getNestedState(t._stateKey, t._path);
|
|
714
|
-
return Array.isArray(S) ?
|
|
721
|
+
return Array.isArray(S) ? i(
|
|
715
722
|
S,
|
|
716
723
|
t._path
|
|
717
724
|
).stateMapNoRender(
|
|
718
725
|
(g, w, _, v, l) => t._mapFn(g, w, _, v, l)
|
|
719
726
|
) : null;
|
|
720
727
|
}
|
|
721
|
-
function
|
|
728
|
+
function Ot({
|
|
722
729
|
proxy: t
|
|
723
730
|
}) {
|
|
724
|
-
const
|
|
731
|
+
const i = Z(null), S = `${t._stateKey}-${t._path.join(".")}`;
|
|
725
732
|
return nt(() => {
|
|
726
|
-
const y =
|
|
733
|
+
const y = i.current;
|
|
727
734
|
if (!y || !y.parentElement) return;
|
|
728
735
|
const g = y.parentElement, _ = Array.from(g.childNodes).indexOf(y);
|
|
729
736
|
let v = g.getAttribute("data-parent-id");
|
|
@@ -749,16 +756,16 @@ function xt({
|
|
|
749
756
|
else
|
|
750
757
|
m = e;
|
|
751
758
|
m !== null && typeof m == "object" && (m = JSON.stringify(m));
|
|
752
|
-
const
|
|
753
|
-
y.replaceWith(
|
|
754
|
-
}, [t._stateKey, t._path.join("."), t._effect]),
|
|
755
|
-
ref:
|
|
759
|
+
const h = document.createTextNode(String(m));
|
|
760
|
+
y.replaceWith(h);
|
|
761
|
+
}, [t._stateKey, t._path.join("."), t._effect]), Q("span", {
|
|
762
|
+
ref: i,
|
|
756
763
|
style: { display: "none" },
|
|
757
764
|
"data-signal-id": S
|
|
758
765
|
});
|
|
759
766
|
}
|
|
760
767
|
function Jt(t) {
|
|
761
|
-
const
|
|
768
|
+
const i = Et(
|
|
762
769
|
(S) => {
|
|
763
770
|
const y = r.getState().stateComponents.get(t._stateKey) || {
|
|
764
771
|
components: /* @__PURE__ */ new Map()
|
|
@@ -770,7 +777,7 @@ function Jt(t) {
|
|
|
770
777
|
},
|
|
771
778
|
() => r.getState().getNestedState(t._stateKey, t._path)
|
|
772
779
|
);
|
|
773
|
-
return
|
|
780
|
+
return Q("text", {}, String(i));
|
|
774
781
|
}
|
|
775
782
|
export {
|
|
776
783
|
tt as $cogsSignal,
|
|
@@ -778,6 +785,6 @@ export {
|
|
|
778
785
|
qt as addStateOptions,
|
|
779
786
|
zt as createCogsState,
|
|
780
787
|
Bt as notifyComponent,
|
|
781
|
-
|
|
788
|
+
Ft as useCogsStateFn
|
|
782
789
|
};
|
|
783
790
|
//# sourceMappingURL=CogsState.jsx.map
|