cogsbox-state 0.5.72 → 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 +269 -271
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
package/dist/CogsState.jsx
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
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 $t, isFunction 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
|
|
12
|
-
|
|
11
|
+
const m = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, g = m(t) || {};
|
|
12
|
+
y(t, {
|
|
13
13
|
...g,
|
|
14
14
|
...i
|
|
15
15
|
});
|
|
@@ -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
|
|
22
|
+
const y = Y(t) || {}, g = m[t] || {}, $ = r.getState().setInitialStateOptions, _ = { ...g, ...y };
|
|
23
23
|
let v = !1;
|
|
24
24
|
if (i)
|
|
25
|
-
for (const
|
|
26
|
-
_.hasOwnProperty(
|
|
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 && $(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 [
|
|
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,
|
|
@@ -40,45 +40,45 @@ const zt = (t, i) => {
|
|
|
40
40
|
...g[v].formElements || {}
|
|
41
41
|
// State-specific overrides
|
|
42
42
|
};
|
|
43
|
-
}), r.getState().setInitialStates(
|
|
44
|
-
const $ = (v,
|
|
45
|
-
const [f] = et(
|
|
43
|
+
}), r.getState().setInitialStates(y);
|
|
44
|
+
const $ = (v, l) => {
|
|
45
|
+
const [f] = et(l?.componentId ?? rt());
|
|
46
46
|
ut({
|
|
47
47
|
stateKey: v,
|
|
48
|
-
options:
|
|
48
|
+
options: l,
|
|
49
49
|
initialOptionsPart: g
|
|
50
50
|
});
|
|
51
|
-
const e = r.getState().cogsStateStore[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
|
-
syncUpdate:
|
|
55
|
+
syncUpdate: l?.syncUpdate,
|
|
56
56
|
componentId: f,
|
|
57
|
-
localStorage:
|
|
58
|
-
middleware:
|
|
59
|
-
enabledSync:
|
|
60
|
-
reactiveType:
|
|
61
|
-
reactiveDeps:
|
|
62
|
-
initialState:
|
|
63
|
-
dependencies:
|
|
57
|
+
localStorage: l?.localStorage,
|
|
58
|
+
middleware: l?.middleware,
|
|
59
|
+
enabledSync: l?.enabledSync,
|
|
60
|
+
reactiveType: l?.reactiveType,
|
|
61
|
+
reactiveDeps: l?.reactiveDeps,
|
|
62
|
+
initialState: l?.initialState,
|
|
63
|
+
dependencies: l?.dependencies
|
|
64
64
|
}
|
|
65
65
|
);
|
|
66
|
-
return
|
|
66
|
+
return A;
|
|
67
67
|
};
|
|
68
|
-
function _(v,
|
|
69
|
-
ut({ stateKey: v, options:
|
|
68
|
+
function _(v, l) {
|
|
69
|
+
ut({ stateKey: v, options: l, initialOptionsPart: g });
|
|
70
70
|
}
|
|
71
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
|
-
|
|
96
|
-
|
|
95
|
+
m.localStorage?.key,
|
|
96
|
+
y
|
|
97
97
|
);
|
|
98
|
-
const g =
|
|
99
|
-
if (g &&
|
|
98
|
+
const g = L(m.localStorage?.key) ? m.localStorage?.key(t) : m.localStorage?.key;
|
|
99
|
+
if (g && y) {
|
|
100
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
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
|
|
126
|
-
i.components.forEach((
|
|
127
|
-
|
|
125
|
+
const m = /* @__PURE__ */ new Set();
|
|
126
|
+
i.components.forEach((y) => {
|
|
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
|
|
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:
|
|
143
|
-
localStorage:
|
|
142
|
+
serverSync: m,
|
|
143
|
+
localStorage: y,
|
|
144
144
|
formElements: g,
|
|
145
145
|
middleware: $,
|
|
146
146
|
reactiveDeps: _,
|
|
147
147
|
reactiveType: v,
|
|
148
|
-
componentId:
|
|
148
|
+
componentId: l,
|
|
149
149
|
initialState: f,
|
|
150
150
|
syncUpdate: e,
|
|
151
|
-
dependencies:
|
|
151
|
+
dependencies: S
|
|
152
152
|
} = {}) {
|
|
153
|
-
const [
|
|
154
|
-
let
|
|
155
|
-
const [
|
|
156
|
-
|
|
157
|
-
if (e && e.stateKey ===
|
|
158
|
-
|
|
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
|
+
if (e && e.stateKey === u && e.path?.[0]) {
|
|
158
|
+
D(u, (o) => ({
|
|
159
159
|
...o,
|
|
160
160
|
[e.path[0]]: e.newValue
|
|
161
161
|
}));
|
|
@@ -166,208 +166,206 @@ function Ft(t, {
|
|
|
166
166
|
});
|
|
167
167
|
}
|
|
168
168
|
}, [e]), nt(() => {
|
|
169
|
-
dt(
|
|
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
|
|
176
|
-
|
|
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),
|
|
181
|
-
|
|
180
|
+
f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), Ft(
|
|
181
|
+
u,
|
|
182
182
|
f,
|
|
183
183
|
E,
|
|
184
184
|
n,
|
|
185
185
|
x.current,
|
|
186
|
-
|
|
187
|
-
), St(
|
|
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
|
-
localStorage:
|
|
193
|
+
localStorage: y,
|
|
194
194
|
middleware: $
|
|
195
195
|
});
|
|
196
|
-
const c = `${
|
|
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(
|
|
206
|
-
const
|
|
207
|
-
o && (o.components.delete(
|
|
205
|
+
}), r.getState().stateComponents.set(u, o), A({}), () => {
|
|
206
|
+
const d = `${u}////${x.current}`;
|
|
207
|
+
o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(u));
|
|
208
208
|
};
|
|
209
209
|
}, []);
|
|
210
|
-
const n = (c, o,
|
|
210
|
+
const n = (c, o, d, E) => {
|
|
211
211
|
if (Array.isArray(o)) {
|
|
212
|
-
const F = `${
|
|
213
|
-
|
|
212
|
+
const F = `${u}-${o.join(".")}`;
|
|
213
|
+
G.current.add(F);
|
|
214
214
|
}
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
if (
|
|
218
|
-
let
|
|
219
|
-
if ((!I || I.size === 0) && (
|
|
220
|
-
const
|
|
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
|
+
if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
|
|
220
|
+
const V = o.slice(0, -1), k = U(N, V);
|
|
221
221
|
if (Array.isArray(k)) {
|
|
222
|
-
|
|
223
|
-
const w = `${
|
|
222
|
+
O = !0;
|
|
223
|
+
const w = `${u}-${V.join(".")}`;
|
|
224
224
|
I = r.getState().signalDomElements.get(w);
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
if (I) {
|
|
228
|
-
const
|
|
228
|
+
const V = O ? U(N, o.slice(0, -1)) : U(N, o);
|
|
229
229
|
I.forEach(({ parentId: k, position: w, effect: W }) => {
|
|
230
|
-
const
|
|
230
|
+
const b = document.querySelector(
|
|
231
231
|
`[data-parent-id="${k}"]`
|
|
232
232
|
);
|
|
233
|
-
if (
|
|
234
|
-
const st = Array.from(
|
|
233
|
+
if (b) {
|
|
234
|
+
const st = Array.from(b.childNodes);
|
|
235
235
|
if (st[w]) {
|
|
236
|
-
const vt = W ? new Function("state", `return (${W})(state)`)(
|
|
236
|
+
const vt = W ? new Function("state", `return (${W})(state)`)(V) : V;
|
|
237
237
|
st[w].textContent = String(vt);
|
|
238
238
|
}
|
|
239
239
|
}
|
|
240
240
|
});
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
|
-
|
|
244
|
-
(E ||
|
|
243
|
+
d.updateType === "update" && (E || p.current?.validationKey) && o && P(
|
|
244
|
+
(E || p.current?.validationKey) + "." + o.join(".")
|
|
245
245
|
);
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
),
|
|
250
|
-
|
|
251
|
-
).filter(([I,
|
|
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
252
|
let k = I?.split(".").length;
|
|
253
|
-
if (I ==
|
|
254
|
-
let w = I + "." +
|
|
255
|
-
|
|
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
|
|
260
|
+
for (const [O, I] of at.components.entries()) {
|
|
261
|
+
let V = !1;
|
|
262
262
|
const k = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
|
|
263
263
|
if (!k.includes("none")) {
|
|
264
264
|
if (k.includes("all")) {
|
|
265
265
|
I.forceUpdate();
|
|
266
266
|
continue;
|
|
267
267
|
}
|
|
268
|
-
if (k.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (
|
|
269
|
-
const w = I.depsFunction(
|
|
270
|
-
typeof w == "boolean" ? w && (
|
|
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 = {
|
|
276
276
|
timeStamp: Date.now(),
|
|
277
|
-
stateKey:
|
|
277
|
+
stateKey: u,
|
|
278
278
|
path: o,
|
|
279
|
-
updateType:
|
|
279
|
+
updateType: d.updateType,
|
|
280
280
|
status: "new",
|
|
281
|
-
oldValue:
|
|
281
|
+
oldValue: q,
|
|
282
282
|
newValue: mt
|
|
283
283
|
};
|
|
284
|
-
if (pt(
|
|
285
|
-
const
|
|
286
|
-
const W = `${w.stateKey}:${JSON.stringify(w.path)}`,
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
291
|
+
N,
|
|
292
|
+
u,
|
|
293
|
+
p.current,
|
|
294
|
+
h
|
|
295
295
|
), $ && $({
|
|
296
296
|
updateLog: s,
|
|
297
297
|
update: it
|
|
298
|
-
}),
|
|
299
|
-
const
|
|
300
|
-
Tt(
|
|
301
|
-
syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state:
|
|
302
|
-
rollBackState:
|
|
298
|
+
}), p.current?.serverSync) {
|
|
299
|
+
const O = r.getState().serverState[u], I = p.current?.serverSync;
|
|
300
|
+
Tt(u, {
|
|
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[
|
|
311
|
-
|
|
310
|
+
r.getState().updaterState[u] || (H(
|
|
311
|
+
u,
|
|
312
312
|
X(
|
|
313
|
-
|
|
313
|
+
u,
|
|
314
314
|
n,
|
|
315
315
|
x.current,
|
|
316
|
-
|
|
316
|
+
h
|
|
317
317
|
)
|
|
318
|
-
), r.getState().cogsStateStore[
|
|
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
|
-
|
|
324
|
-
), [
|
|
325
|
-
return [gt(
|
|
323
|
+
h
|
|
324
|
+
), [u]);
|
|
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
329
|
let $ = 0;
|
|
330
330
|
const _ = (f) => {
|
|
331
331
|
const e = f.join(".");
|
|
332
|
-
for (const [
|
|
333
|
-
(
|
|
332
|
+
for (const [S] of g)
|
|
333
|
+
(S === e || S.startsWith(e + ".")) && g.delete(S);
|
|
334
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
|
|
341
|
+
e?.key && P(e?.key), f?.validationKey && P(f.validationKey);
|
|
342
|
+
const S = r.getState().initialStateGlobal[t];
|
|
343
343
|
g.clear(), $++;
|
|
344
|
-
const
|
|
345
|
-
return
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
|
|
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
349
|
s.forceUpdate();
|
|
350
350
|
});
|
|
351
|
-
}),
|
|
351
|
+
}), S;
|
|
352
352
|
},
|
|
353
353
|
updateInitialState: (f) => {
|
|
354
354
|
g.clear(), $++;
|
|
355
|
-
const e =
|
|
356
|
-
N && localStorage.removeItem(N);
|
|
357
|
-
const p = X(
|
|
355
|
+
const e = X(
|
|
358
356
|
t,
|
|
359
357
|
i,
|
|
360
|
-
|
|
361
|
-
|
|
358
|
+
m,
|
|
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, m) {
|
|
|
378
376
|
return !!(f && M(f, gt(t)));
|
|
379
377
|
}
|
|
380
378
|
};
|
|
381
|
-
function
|
|
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
|
-
Object.keys(v).forEach((
|
|
388
|
-
|
|
385
|
+
Object.keys(v).forEach((u) => {
|
|
386
|
+
A[u] = v[u];
|
|
389
387
|
});
|
|
390
|
-
const
|
|
391
|
-
apply(
|
|
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
|
-
get(
|
|
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,7 +413,7 @@ function X(t, i, S, m) {
|
|
|
415
413
|
};
|
|
416
414
|
if (s === "removeStorage")
|
|
417
415
|
return () => {
|
|
418
|
-
const n = r.getState().initialStateGlobal[t], a =
|
|
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}`;
|
|
419
417
|
o && localStorage.removeItem(o);
|
|
420
418
|
};
|
|
421
419
|
if (s === "showValidationErrors")
|
|
@@ -430,42 +428,42 @@ function X(t, i, S, m) {
|
|
|
430
428
|
return () => {
|
|
431
429
|
const n = r.getState().getSelectedIndex(t, e.join("."));
|
|
432
430
|
if (n !== void 0)
|
|
433
|
-
return
|
|
431
|
+
return l(
|
|
434
432
|
f[n],
|
|
435
433
|
[...e, n.toString()],
|
|
436
|
-
|
|
434
|
+
S
|
|
437
435
|
);
|
|
438
436
|
};
|
|
439
437
|
if (s === "stateSort")
|
|
440
438
|
return (n) => {
|
|
441
|
-
const o = [...r.getState().getNestedState(t, e).map((
|
|
442
|
-
...
|
|
439
|
+
const o = [...r.getState().getNestedState(t, e).map((d, E) => ({
|
|
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
|
)
|
|
450
448
|
});
|
|
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(), $++), c.map((o,
|
|
458
|
-
const E = a && o.__origIndex ? o.__origIndex :
|
|
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
|
-
|
|
466
|
+
l(f, e, S)
|
|
469
467
|
);
|
|
470
468
|
});
|
|
471
469
|
};
|
|
@@ -477,21 +475,21 @@ function X(t, i, S, m) {
|
|
|
477
475
|
_mapFn: n
|
|
478
476
|
// Pass the actual function, not string
|
|
479
477
|
},
|
|
480
|
-
rebuildStateShape:
|
|
478
|
+
rebuildStateShape: l
|
|
481
479
|
});
|
|
482
480
|
if (s === "stateFlattenOn")
|
|
483
481
|
return (n) => {
|
|
484
|
-
const c =
|
|
485
|
-
(
|
|
482
|
+
const c = S?.filtered?.some(
|
|
483
|
+
(d) => d.join(".") === e.join(".")
|
|
486
484
|
) ? f : r.getState().getNestedState(t, e);
|
|
487
485
|
g.clear(), $++;
|
|
488
486
|
const o = c.flatMap(
|
|
489
|
-
(
|
|
487
|
+
(d, E) => d[n] ?? []
|
|
490
488
|
);
|
|
491
|
-
return
|
|
489
|
+
return l(
|
|
492
490
|
o,
|
|
493
491
|
[...e, "[*]", n],
|
|
494
|
-
|
|
492
|
+
S
|
|
495
493
|
);
|
|
496
494
|
};
|
|
497
495
|
if (s === "findWith")
|
|
@@ -500,79 +498,79 @@ function X(t, i, S, m) {
|
|
|
500
498
|
(E) => E[n] === a
|
|
501
499
|
);
|
|
502
500
|
if (c === -1) return;
|
|
503
|
-
const o = f[c],
|
|
504
|
-
return g.clear(), $++, g.clear(), $++,
|
|
501
|
+
const o = f[c], d = [...e, c.toString()];
|
|
502
|
+
return g.clear(), $++, g.clear(), $++, l(o, d);
|
|
505
503
|
};
|
|
506
504
|
if (s === "index")
|
|
507
505
|
return (n) => {
|
|
508
506
|
const a = f[n];
|
|
509
|
-
return
|
|
507
|
+
return l(a, [...e, n.toString()]);
|
|
510
508
|
};
|
|
511
509
|
if (s === "insert")
|
|
512
|
-
return (n) => (_(e), K(i, n, e, t),
|
|
510
|
+
return (n) => (_(e), K(i, n, e, t), l(
|
|
513
511
|
r.getState().cogsStateStore[t],
|
|
514
512
|
[]
|
|
515
513
|
));
|
|
516
514
|
if (s === "uniqueInsert")
|
|
517
515
|
return (n, a, c) => {
|
|
518
|
-
const o = r.getState().getNestedState(t, e),
|
|
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
|
-
_(e), K(i,
|
|
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) => {
|
|
554
|
-
const a = f.map((
|
|
555
|
-
...
|
|
552
|
+
const a = f.map((d, E) => ({
|
|
553
|
+
...d,
|
|
556
554
|
__origIndex: E.toString()
|
|
557
555
|
})), c = [], o = [];
|
|
558
|
-
for (let
|
|
559
|
-
n(a[
|
|
560
|
-
return g.clear(), $++,
|
|
561
|
-
filtered: [...
|
|
556
|
+
for (let d = 0; d < a.length; d++)
|
|
557
|
+
n(a[d], d) && (c.push(d), o.push(a[d]));
|
|
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")
|
|
@@ -599,7 +597,7 @@ function X(t, i, S, m) {
|
|
|
599
597
|
return r.getState().getSyncInfo(n);
|
|
600
598
|
}
|
|
601
599
|
if (s == "getLocalStorage")
|
|
602
|
-
return (n) => ft(
|
|
600
|
+
return (n) => ft(y + "-" + t + "-" + n);
|
|
603
601
|
if (s === "_selected") {
|
|
604
602
|
const n = e.slice(0, -1), a = n.join("."), c = r.getState().getNestedState(t, n);
|
|
605
603
|
return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, a) : void 0;
|
|
@@ -608,8 +606,8 @@ function X(t, i, S, m) {
|
|
|
608
606
|
return (n) => {
|
|
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
|
-
const
|
|
612
|
-
|
|
609
|
+
const d = r.getState().getNestedState(t, [...a]);
|
|
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, m) {
|
|
|
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
|
-
const
|
|
630
|
-
return
|
|
631
|
-
const
|
|
632
|
-
a(
|
|
627
|
+
const d = n.zodSchema.safeParse(c);
|
|
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")
|
|
@@ -664,7 +662,7 @@ function X(t, i, S, m) {
|
|
|
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
|
);
|
|
@@ -675,12 +673,12 @@ function X(t, i, S, m) {
|
|
|
675
673
|
return (n, a) => {
|
|
676
674
|
if (a?.debounce)
|
|
677
675
|
wt(() => {
|
|
678
|
-
|
|
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
|
}
|
|
@@ -697,42 +695,42 @@ function X(t, i, S, m) {
|
|
|
697
695
|
formOpts: a
|
|
698
696
|
}
|
|
699
697
|
);
|
|
700
|
-
const x = [...e, s],
|
|
701
|
-
return
|
|
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:
|
|
701
|
+
}, R = new Proxy(A, h);
|
|
702
|
+
return g.set(T, {
|
|
703
|
+
proxy: R,
|
|
706
704
|
stateVersion: $
|
|
707
|
-
}),
|
|
705
|
+
}), R;
|
|
708
706
|
}
|
|
709
|
-
return
|
|
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, $, _, v,
|
|
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
|
-
const
|
|
734
|
-
if (!
|
|
735
|
-
const g =
|
|
731
|
+
const y = i.current;
|
|
732
|
+
if (!y || !y.parentElement) return;
|
|
733
|
+
const g = y.parentElement, _ = Array.from(g.childNodes).indexOf(y);
|
|
736
734
|
let v = g.getAttribute("data-parent-id");
|
|
737
735
|
v || (v = `parent-${crypto.randomUUID()}`, g.setAttribute("data-parent-id", v));
|
|
738
736
|
const f = {
|
|
@@ -741,39 +739,39 @@ 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
|
-
|
|
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
|
-
(
|
|
770
|
-
const
|
|
767
|
+
(m) => {
|
|
768
|
+
const y = r.getState().stateComponents.get(t._stateKey) || {
|
|
771
769
|
components: /* @__PURE__ */ new Map()
|
|
772
770
|
};
|
|
773
|
-
return
|
|
774
|
-
forceUpdate:
|
|
771
|
+
return y.components.set(t._stateKey, {
|
|
772
|
+
forceUpdate: m,
|
|
775
773
|
paths: /* @__PURE__ */ new Set([t._path.join(".")])
|
|
776
|
-
}), () =>
|
|
774
|
+
}), () => y.components.delete(t._stateKey);
|
|
777
775
|
},
|
|
778
776
|
() => r.getState().getNestedState(t._stateKey, t._path)
|
|
779
777
|
);
|
|
@@ -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
|