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