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