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