cogsbox-state 0.5.201 → 0.5.202
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 +553 -558
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +53 -81
package/dist/CogsState.jsx
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as Se } from "react/jsx-runtime";
|
|
3
|
-
import { useState as le, useRef as
|
|
4
|
-
import { transformStateFunc as Ne, isDeepEqual as
|
|
5
|
-
import { pushFunc as se, updateFn as
|
|
3
|
+
import { useState as le, useRef as Q, useEffect as de, useLayoutEffect as pe, useMemo as ke, createElement as te, useSyncExternalStore as Ae, startTransition as Te } from "react";
|
|
4
|
+
import { transformStateFunc as Ne, isDeepEqual as M, isFunction as z, getNestedValue as W, getDifferences as he, debounce as Ve } from "./utility.js";
|
|
5
|
+
import { pushFunc as se, updateFn as Y, cutFunc as X, ValidationWrapper as _e, FormControlComponent as be } from "./Functions.jsx";
|
|
6
6
|
import Pe from "superjson";
|
|
7
7
|
import { v4 as ue } from "uuid";
|
|
8
8
|
import "zod";
|
|
9
|
-
import { getGlobalStore as
|
|
10
|
-
import { useCogsConfig as
|
|
11
|
-
import { applyPatch as
|
|
9
|
+
import { getGlobalStore as n, formRefStore as me } from "./store.js";
|
|
10
|
+
import { useCogsConfig as we } from "./CogsStateClient.jsx";
|
|
11
|
+
import { applyPatch as Ce } from "fast-json-patch";
|
|
12
12
|
function ye(e, i) {
|
|
13
|
-
const
|
|
13
|
+
const m = n.getState().getInitialOptions, u = n.getState().setInitialStateOptions, f = m(e) || {};
|
|
14
14
|
u(e, {
|
|
15
15
|
...f,
|
|
16
16
|
...i
|
|
@@ -19,22 +19,22 @@ function ye(e, i) {
|
|
|
19
19
|
function ve({
|
|
20
20
|
stateKey: e,
|
|
21
21
|
options: i,
|
|
22
|
-
initialOptionsPart:
|
|
22
|
+
initialOptionsPart: m
|
|
23
23
|
}) {
|
|
24
|
-
const u =
|
|
24
|
+
const u = Z(e) || {}, f = m[e] || {}, A = n.getState().setInitialStateOptions, p = { ...f, ...u };
|
|
25
25
|
let v = !1;
|
|
26
26
|
if (i)
|
|
27
27
|
for (const s in i)
|
|
28
|
-
|
|
29
|
-
!
|
|
30
|
-
v &&
|
|
28
|
+
p.hasOwnProperty(s) ? (s == "localStorage" && i[s] && p[s].key !== i[s]?.key && (v = !0, p[s] = i[s]), s == "initialState" && i[s] && p[s] !== i[s] && // Different references
|
|
29
|
+
!M(p[s], i[s]) && (v = !0, p[s] = i[s])) : (v = !0, p[s] = i[s]);
|
|
30
|
+
v && A(e, p);
|
|
31
31
|
}
|
|
32
|
-
function Qe(e, { formElements: i, validation:
|
|
33
|
-
return { initialState: e, formElements: i, validation:
|
|
32
|
+
function Qe(e, { formElements: i, validation: m }) {
|
|
33
|
+
return { initialState: e, formElements: i, validation: m };
|
|
34
34
|
}
|
|
35
35
|
const Ke = (e, i) => {
|
|
36
|
-
let
|
|
37
|
-
const [u, f] = Ne(
|
|
36
|
+
let m = e;
|
|
37
|
+
const [u, f] = Ne(m);
|
|
38
38
|
(Object.keys(f).length > 0 || i && Object.keys(i).length > 0) && Object.keys(f).forEach((v) => {
|
|
39
39
|
f[v] = f[v] || {}, f[v].formElements = {
|
|
40
40
|
...i?.formElements,
|
|
@@ -42,21 +42,21 @@ const Ke = (e, i) => {
|
|
|
42
42
|
...i?.validation,
|
|
43
43
|
...f[v].formElements || {}
|
|
44
44
|
// State-specific overrides
|
|
45
|
-
},
|
|
46
|
-
}),
|
|
47
|
-
const
|
|
48
|
-
const [
|
|
45
|
+
}, Z(v) || n.getState().setInitialStateOptions(v, f[v]);
|
|
46
|
+
}), n.getState().setInitialStates(u), n.getState().setCreatedState(u);
|
|
47
|
+
const A = (v, s) => {
|
|
48
|
+
const [y] = le(s?.componentId ?? ue());
|
|
49
49
|
ve({
|
|
50
50
|
stateKey: v,
|
|
51
51
|
options: s,
|
|
52
52
|
initialOptionsPart: f
|
|
53
53
|
});
|
|
54
|
-
const
|
|
54
|
+
const t = n.getState().cogsStateStore[v] || u[v], I = s?.modifyState ? s.modifyState(t) : t, [j, V] = De(
|
|
55
55
|
I,
|
|
56
56
|
{
|
|
57
57
|
stateKey: v,
|
|
58
58
|
syncUpdate: s?.syncUpdate,
|
|
59
|
-
componentId:
|
|
59
|
+
componentId: y,
|
|
60
60
|
localStorage: s?.localStorage,
|
|
61
61
|
middleware: s?.middleware,
|
|
62
62
|
enabledSync: s?.enabledSync,
|
|
@@ -67,49 +67,49 @@ const Ke = (e, i) => {
|
|
|
67
67
|
serverState: s?.serverState
|
|
68
68
|
}
|
|
69
69
|
);
|
|
70
|
-
return
|
|
70
|
+
return V;
|
|
71
71
|
};
|
|
72
|
-
function
|
|
73
|
-
ve({ stateKey: v, options: s, initialOptionsPart: f }), s.localStorage &&
|
|
72
|
+
function p(v, s) {
|
|
73
|
+
ve({ stateKey: v, options: s, initialOptionsPart: f }), s.localStorage && Re(v, s), re(v);
|
|
74
74
|
}
|
|
75
|
-
return { useCogsState:
|
|
75
|
+
return { useCogsState: A, setCogsOptions: p };
|
|
76
76
|
}, {
|
|
77
|
-
setUpdaterState:
|
|
78
|
-
setState:
|
|
79
|
-
getInitialOptions:
|
|
80
|
-
getKeyState:
|
|
77
|
+
setUpdaterState: K,
|
|
78
|
+
setState: J,
|
|
79
|
+
getInitialOptions: Z,
|
|
80
|
+
getKeyState: Ee,
|
|
81
81
|
getValidationErrors: xe,
|
|
82
82
|
setStateLog: Oe,
|
|
83
83
|
updateInitialStateGlobal: ge,
|
|
84
84
|
addValidationError: Fe,
|
|
85
|
-
removeValidationError:
|
|
86
|
-
setServerSyncActions:
|
|
87
|
-
} =
|
|
88
|
-
|
|
85
|
+
removeValidationError: q,
|
|
86
|
+
setServerSyncActions: je
|
|
87
|
+
} = n.getState(), Ie = (e, i, m, u, f) => {
|
|
88
|
+
m?.log && console.log(
|
|
89
89
|
"saving to localstorage",
|
|
90
90
|
i,
|
|
91
|
-
|
|
91
|
+
m.localStorage?.key,
|
|
92
92
|
u
|
|
93
93
|
);
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
96
|
-
const
|
|
94
|
+
const A = z(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
|
|
95
|
+
if (A && u) {
|
|
96
|
+
const p = `${u}-${i}-${A}`;
|
|
97
97
|
let v;
|
|
98
98
|
try {
|
|
99
|
-
v =
|
|
99
|
+
v = ne(p)?.lastSyncedWithServer;
|
|
100
100
|
} catch {
|
|
101
101
|
}
|
|
102
102
|
const s = {
|
|
103
103
|
state: e,
|
|
104
104
|
lastUpdated: Date.now(),
|
|
105
105
|
lastSyncedWithServer: f ?? v
|
|
106
|
-
},
|
|
106
|
+
}, y = Pe.serialize(s);
|
|
107
107
|
window.localStorage.setItem(
|
|
108
|
-
|
|
109
|
-
JSON.stringify(
|
|
108
|
+
p,
|
|
109
|
+
JSON.stringify(y.json)
|
|
110
110
|
);
|
|
111
111
|
}
|
|
112
|
-
},
|
|
112
|
+
}, ne = (e) => {
|
|
113
113
|
if (!e) return null;
|
|
114
114
|
try {
|
|
115
115
|
const i = window.localStorage.getItem(e);
|
|
@@ -117,61 +117,61 @@ const Ke = (e, i) => {
|
|
|
117
117
|
} catch (i) {
|
|
118
118
|
return console.error("Error loading from localStorage:", i), null;
|
|
119
119
|
}
|
|
120
|
-
},
|
|
121
|
-
const
|
|
120
|
+
}, Re = (e, i) => {
|
|
121
|
+
const m = n.getState().cogsStateStore[e], { sessionId: u } = we(), f = z(i?.localStorage?.key) ? i.localStorage.key(m) : i?.localStorage?.key;
|
|
122
122
|
if (f && u) {
|
|
123
|
-
const
|
|
123
|
+
const A = ne(
|
|
124
124
|
`${u}-${e}-${f}`
|
|
125
125
|
);
|
|
126
|
-
if (
|
|
127
|
-
return
|
|
126
|
+
if (A && A.lastUpdated > (A.lastSyncedWithServer || 0))
|
|
127
|
+
return J(e, A.state), re(e), !0;
|
|
128
128
|
}
|
|
129
129
|
return !1;
|
|
130
|
-
},
|
|
131
|
-
const
|
|
130
|
+
}, $e = (e, i, m, u, f, A) => {
|
|
131
|
+
const p = {
|
|
132
132
|
initialState: i,
|
|
133
|
-
updaterState:
|
|
133
|
+
updaterState: ee(
|
|
134
134
|
e,
|
|
135
135
|
u,
|
|
136
136
|
f,
|
|
137
|
-
|
|
137
|
+
A
|
|
138
138
|
),
|
|
139
|
-
state:
|
|
139
|
+
state: m
|
|
140
140
|
};
|
|
141
|
-
ge(e,
|
|
142
|
-
},
|
|
143
|
-
const i =
|
|
141
|
+
ge(e, p.initialState), K(e, p.updaterState), J(e, p.state);
|
|
142
|
+
}, re = (e) => {
|
|
143
|
+
const i = n.getState().stateComponents.get(e);
|
|
144
144
|
if (!i) return;
|
|
145
|
-
const
|
|
145
|
+
const m = /* @__PURE__ */ new Set();
|
|
146
146
|
i.components.forEach((u) => {
|
|
147
|
-
(u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") ||
|
|
147
|
+
(u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") || m.add(() => u.forceUpdate());
|
|
148
148
|
}), queueMicrotask(() => {
|
|
149
|
-
|
|
149
|
+
m.forEach((u) => u());
|
|
150
150
|
});
|
|
151
151
|
}, et = (e, i) => {
|
|
152
|
-
const
|
|
153
|
-
if (
|
|
154
|
-
const u = `${e}////${i}`, f =
|
|
152
|
+
const m = n.getState().stateComponents.get(e);
|
|
153
|
+
if (m) {
|
|
154
|
+
const u = `${e}////${i}`, f = m.components.get(u);
|
|
155
155
|
if ((f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none"))
|
|
156
156
|
return;
|
|
157
157
|
f && f.forceUpdate();
|
|
158
158
|
}
|
|
159
|
-
}, Ue = (e, i,
|
|
159
|
+
}, Ue = (e, i, m, u) => {
|
|
160
160
|
switch (e) {
|
|
161
161
|
case "update":
|
|
162
162
|
return {
|
|
163
|
-
oldValue:
|
|
164
|
-
newValue:
|
|
163
|
+
oldValue: W(i, u),
|
|
164
|
+
newValue: W(m, u)
|
|
165
165
|
};
|
|
166
166
|
case "insert":
|
|
167
167
|
return {
|
|
168
168
|
oldValue: null,
|
|
169
169
|
// or undefined
|
|
170
|
-
newValue:
|
|
170
|
+
newValue: W(m, u)
|
|
171
171
|
};
|
|
172
172
|
case "cut":
|
|
173
173
|
return {
|
|
174
|
-
oldValue:
|
|
174
|
+
oldValue: W(i, u),
|
|
175
175
|
newValue: null
|
|
176
176
|
// or undefined
|
|
177
177
|
};
|
|
@@ -179,830 +179,825 @@ const Ke = (e, i) => {
|
|
|
179
179
|
return { oldValue: null, newValue: null };
|
|
180
180
|
}
|
|
181
181
|
};
|
|
182
|
-
function
|
|
182
|
+
function De(e, {
|
|
183
183
|
stateKey: i,
|
|
184
|
-
serverSync:
|
|
184
|
+
serverSync: m,
|
|
185
185
|
localStorage: u,
|
|
186
186
|
formElements: f,
|
|
187
|
-
reactiveDeps:
|
|
188
|
-
reactiveType:
|
|
187
|
+
reactiveDeps: A,
|
|
188
|
+
reactiveType: p,
|
|
189
189
|
componentId: v,
|
|
190
190
|
initialState: s,
|
|
191
|
-
syncUpdate:
|
|
192
|
-
dependencies:
|
|
191
|
+
syncUpdate: y,
|
|
192
|
+
dependencies: t,
|
|
193
193
|
serverState: I
|
|
194
194
|
} = {}) {
|
|
195
|
-
const [
|
|
196
|
-
let
|
|
197
|
-
const [g] = le(i ?? ue()),
|
|
195
|
+
const [j, V] = le({}), { sessionId: _ } = we();
|
|
196
|
+
let R = !i;
|
|
197
|
+
const [g] = le(i ?? ue()), c = n.getState().stateLog[g], H = Q(/* @__PURE__ */ new Set()), L = Q(v ?? ue()), b = Q(
|
|
198
198
|
null
|
|
199
199
|
);
|
|
200
|
-
|
|
201
|
-
if (
|
|
202
|
-
|
|
200
|
+
b.current = Z(g) ?? null, de(() => {
|
|
201
|
+
if (y && y.stateKey === g && y.path?.[0]) {
|
|
202
|
+
J(g, (a) => ({
|
|
203
203
|
...a,
|
|
204
|
-
[
|
|
204
|
+
[y.path[0]]: y.newValue
|
|
205
205
|
}));
|
|
206
|
-
const o = `${
|
|
207
|
-
|
|
208
|
-
timeStamp:
|
|
209
|
-
userId:
|
|
206
|
+
const o = `${y.stateKey}:${y.path.join(".")}`;
|
|
207
|
+
n.getState().setSyncInfo(o, {
|
|
208
|
+
timeStamp: y.timeStamp,
|
|
209
|
+
userId: y.userId
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
|
-
}, [
|
|
212
|
+
}, [y]), de(() => {
|
|
213
213
|
if (s) {
|
|
214
214
|
ye(g, {
|
|
215
215
|
initialState: s
|
|
216
216
|
});
|
|
217
|
-
const o =
|
|
218
|
-
if (!(
|
|
217
|
+
const o = b.current, d = o?.serverState?.id !== void 0 && o?.serverState?.status === "success" && o?.serverState?.data, w = n.getState().initialStateGlobal[g];
|
|
218
|
+
if (!(w && !M(w, s) || !w) && !d)
|
|
219
219
|
return;
|
|
220
|
-
let
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
let
|
|
224
|
-
const
|
|
225
|
-
|
|
220
|
+
let h = null;
|
|
221
|
+
const E = z(o?.localStorage?.key) ? o?.localStorage?.key(s) : o?.localStorage?.key;
|
|
222
|
+
E && _ && (h = ne(`${_}-${g}-${E}`));
|
|
223
|
+
let $ = s, N = !1;
|
|
224
|
+
const P = d ? Date.now() : 0, B = h?.lastUpdated || 0, ae = h?.lastSyncedWithServer || 0;
|
|
225
|
+
d && P > B ? ($ = o.serverState.data, N = !0) : h && B > ae && ($ = h.state, o?.localStorage?.onChange && o?.localStorage?.onChange($)), $e(
|
|
226
226
|
g,
|
|
227
227
|
s,
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
),
|
|
228
|
+
$,
|
|
229
|
+
l,
|
|
230
|
+
L.current,
|
|
231
|
+
_
|
|
232
|
+
), N && E && _ && Ie($, g, o, _, Date.now()), re(g), (Array.isArray(p) ? p : [p || "component"]).includes("none") || V({});
|
|
233
233
|
}
|
|
234
234
|
}, [
|
|
235
235
|
s,
|
|
236
236
|
I?.status,
|
|
237
237
|
I?.data,
|
|
238
|
-
...
|
|
239
|
-
]),
|
|
240
|
-
|
|
241
|
-
serverSync:
|
|
238
|
+
...t || []
|
|
239
|
+
]), pe(() => {
|
|
240
|
+
R && ye(g, {
|
|
241
|
+
serverSync: m,
|
|
242
242
|
formElements: f,
|
|
243
243
|
initialState: s,
|
|
244
244
|
localStorage: u,
|
|
245
|
-
middleware:
|
|
245
|
+
middleware: b.current?.middleware
|
|
246
246
|
});
|
|
247
|
-
const o = `${g}////${
|
|
247
|
+
const o = `${g}////${L.current}`, a = n.getState().stateComponents.get(g) || {
|
|
248
248
|
components: /* @__PURE__ */ new Map()
|
|
249
249
|
};
|
|
250
250
|
return a.components.set(o, {
|
|
251
|
-
forceUpdate: () =>
|
|
251
|
+
forceUpdate: () => V({}),
|
|
252
252
|
paths: /* @__PURE__ */ new Set(),
|
|
253
253
|
deps: [],
|
|
254
|
-
depsFunction:
|
|
255
|
-
reactiveType:
|
|
256
|
-
}),
|
|
257
|
-
const
|
|
258
|
-
a && (a.components.delete(
|
|
254
|
+
depsFunction: A || void 0,
|
|
255
|
+
reactiveType: p ?? ["component", "deps"]
|
|
256
|
+
}), n.getState().stateComponents.set(g, a), V({}), () => {
|
|
257
|
+
const d = `${g}////${L.current}`;
|
|
258
|
+
a && (a.components.delete(d), a.components.size === 0 && n.getState().stateComponents.delete(g));
|
|
259
259
|
};
|
|
260
260
|
}, []);
|
|
261
|
-
const
|
|
261
|
+
const l = (o, a, d, w) => {
|
|
262
262
|
if (Array.isArray(a)) {
|
|
263
|
-
const
|
|
264
|
-
|
|
263
|
+
const S = `${g}-${a.join(".")}`;
|
|
264
|
+
H.current.add(S);
|
|
265
265
|
}
|
|
266
|
-
|
|
267
|
-
const
|
|
268
|
-
if (
|
|
269
|
-
let
|
|
270
|
-
if ((!
|
|
271
|
-
const
|
|
272
|
-
if (Array.isArray(
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
266
|
+
J(g, (S) => {
|
|
267
|
+
const h = z(o) ? o(S) : o, E = `${g}-${a.join(".")}`;
|
|
268
|
+
if (E) {
|
|
269
|
+
let D = !1, T = n.getState().signalDomElements.get(E);
|
|
270
|
+
if ((!T || T.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
|
|
271
|
+
const O = a.slice(0, -1), U = W(h, O);
|
|
272
|
+
if (Array.isArray(U)) {
|
|
273
|
+
D = !0;
|
|
274
|
+
const k = `${g}-${O.join(".")}`;
|
|
275
|
+
T = n.getState().signalDomElements.get(k);
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
|
-
if (
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
const
|
|
282
|
-
`[data-parent-id="${
|
|
278
|
+
if (T) {
|
|
279
|
+
const O = D ? W(h, a.slice(0, -1)) : W(h, a);
|
|
280
|
+
T.forEach(({ parentId: U, position: k, effect: x }) => {
|
|
281
|
+
const C = document.querySelector(
|
|
282
|
+
`[data-parent-id="${U}"]`
|
|
283
283
|
);
|
|
284
|
-
if (
|
|
285
|
-
const
|
|
286
|
-
if (
|
|
287
|
-
const
|
|
288
|
-
|
|
284
|
+
if (C) {
|
|
285
|
+
const G = Array.from(C.childNodes);
|
|
286
|
+
if (G[k]) {
|
|
287
|
+
const F = x ? new Function("state", `return (${x})(state)`)(O) : O;
|
|
288
|
+
G[k].textContent = String(F);
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
291
|
});
|
|
292
292
|
}
|
|
293
293
|
}
|
|
294
|
-
|
|
295
|
-
(
|
|
294
|
+
d.updateType === "update" && (w || b.current?.validation?.key) && a && q(
|
|
295
|
+
(w || b.current?.validation?.key) + "." + a.join(".")
|
|
296
296
|
);
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
),
|
|
301
|
-
|
|
302
|
-
).filter(([
|
|
303
|
-
let
|
|
304
|
-
if (
|
|
305
|
-
let
|
|
306
|
-
|
|
297
|
+
const $ = a.slice(0, a.length - 1);
|
|
298
|
+
d.updateType === "cut" && b.current?.validation?.key && q(
|
|
299
|
+
b.current?.validation?.key + "." + $.join(".")
|
|
300
|
+
), d.updateType === "insert" && b.current?.validation?.key && xe(
|
|
301
|
+
b.current?.validation?.key + "." + $.join(".")
|
|
302
|
+
).filter(([T, O]) => {
|
|
303
|
+
let U = T?.split(".").length;
|
|
304
|
+
if (T == $.join(".") && U == $.length - 1) {
|
|
305
|
+
let k = T + "." + $;
|
|
306
|
+
q(T), Fe(k, O);
|
|
307
307
|
}
|
|
308
308
|
});
|
|
309
|
-
const
|
|
310
|
-
if (
|
|
311
|
-
const
|
|
309
|
+
const N = n.getState().stateComponents.get(g);
|
|
310
|
+
if (N) {
|
|
311
|
+
const D = he(S, h), T = new Set(D), O = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
|
|
312
312
|
for (const [
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
] of
|
|
316
|
-
let
|
|
317
|
-
const
|
|
318
|
-
if (!
|
|
319
|
-
if (
|
|
320
|
-
|
|
313
|
+
U,
|
|
314
|
+
k
|
|
315
|
+
] of N.components.entries()) {
|
|
316
|
+
let x = !1;
|
|
317
|
+
const C = Array.isArray(k.reactiveType) ? k.reactiveType : [k.reactiveType || "component"];
|
|
318
|
+
if (!C.includes("none")) {
|
|
319
|
+
if (C.includes("all")) {
|
|
320
|
+
k.forceUpdate();
|
|
321
321
|
continue;
|
|
322
322
|
}
|
|
323
|
-
if (
|
|
324
|
-
for (const
|
|
325
|
-
let
|
|
323
|
+
if (C.includes("component") && ((k.paths.has(O) || k.paths.has("")) && (x = !0), !x))
|
|
324
|
+
for (const G of T) {
|
|
325
|
+
let F = G;
|
|
326
326
|
for (; ; ) {
|
|
327
|
-
if (
|
|
328
|
-
|
|
327
|
+
if (k.paths.has(F)) {
|
|
328
|
+
x = !0;
|
|
329
329
|
break;
|
|
330
330
|
}
|
|
331
|
-
const ie =
|
|
331
|
+
const ie = F.lastIndexOf(".");
|
|
332
332
|
if (ie !== -1) {
|
|
333
|
-
const fe =
|
|
333
|
+
const fe = F.substring(
|
|
334
334
|
0,
|
|
335
335
|
ie
|
|
336
336
|
);
|
|
337
337
|
if (!isNaN(
|
|
338
|
-
Number(
|
|
339
|
-
) &&
|
|
340
|
-
|
|
338
|
+
Number(F.substring(ie + 1))
|
|
339
|
+
) && k.paths.has(fe)) {
|
|
340
|
+
x = !0;
|
|
341
341
|
break;
|
|
342
342
|
}
|
|
343
|
-
|
|
343
|
+
F = fe;
|
|
344
344
|
} else
|
|
345
|
-
|
|
346
|
-
if (
|
|
345
|
+
F = "";
|
|
346
|
+
if (F === "")
|
|
347
347
|
break;
|
|
348
348
|
}
|
|
349
|
-
if (
|
|
349
|
+
if (x) break;
|
|
350
350
|
}
|
|
351
|
-
if (!
|
|
352
|
-
const
|
|
353
|
-
let
|
|
354
|
-
typeof
|
|
351
|
+
if (!x && C.includes("deps") && k.depsFunction) {
|
|
352
|
+
const G = k.depsFunction(h);
|
|
353
|
+
let F = !1;
|
|
354
|
+
typeof G == "boolean" ? G && (F = !0) : M(k.deps, G) || (k.deps = G, F = !0), F && (x = !0);
|
|
355
355
|
}
|
|
356
|
-
|
|
356
|
+
x && k.forceUpdate();
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
}
|
|
360
|
-
const
|
|
361
|
-
a = a.map((
|
|
362
|
-
const
|
|
363
|
-
return
|
|
360
|
+
const P = Date.now();
|
|
361
|
+
a = a.map((D, T) => {
|
|
362
|
+
const O = a.slice(0, -1), U = W(h, O);
|
|
363
|
+
return T === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (U.length - 1).toString() : D;
|
|
364
364
|
}), console.log(
|
|
365
365
|
"mmmmmmmmmmmmmmmmm22222222222222",
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
366
|
+
d.updateType,
|
|
367
|
+
S,
|
|
368
|
+
h,
|
|
369
369
|
a
|
|
370
370
|
);
|
|
371
|
-
const { oldValue:
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
371
|
+
const { oldValue: B, newValue: ae } = Ue(
|
|
372
|
+
d.updateType,
|
|
373
|
+
S,
|
|
374
|
+
h,
|
|
375
375
|
a
|
|
376
|
-
),
|
|
377
|
-
timeStamp:
|
|
376
|
+
), oe = {
|
|
377
|
+
timeStamp: P,
|
|
378
378
|
stateKey: g,
|
|
379
379
|
path: a,
|
|
380
|
-
updateType:
|
|
380
|
+
updateType: d.updateType,
|
|
381
381
|
status: "new",
|
|
382
|
-
oldValue:
|
|
383
|
-
newValue:
|
|
382
|
+
oldValue: B,
|
|
383
|
+
newValue: ae
|
|
384
384
|
};
|
|
385
|
-
if (Oe(g, (
|
|
386
|
-
const
|
|
387
|
-
const
|
|
388
|
-
return
|
|
385
|
+
if (Oe(g, (D) => {
|
|
386
|
+
const O = [...D ?? [], oe].reduce((U, k) => {
|
|
387
|
+
const x = `${k.stateKey}:${JSON.stringify(k.path)}`, C = U.get(x);
|
|
388
|
+
return C ? (C.timeStamp = Math.max(C.timeStamp, k.timeStamp), C.newValue = k.newValue, C.oldValue = C.oldValue ?? k.oldValue, C.updateType = k.updateType) : U.set(x, { ...k }), U;
|
|
389
389
|
}, /* @__PURE__ */ new Map());
|
|
390
|
-
return Array.from(
|
|
390
|
+
return Array.from(O.values());
|
|
391
391
|
}), Ie(
|
|
392
|
-
|
|
392
|
+
h,
|
|
393
393
|
g,
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
),
|
|
397
|
-
updateLog:
|
|
398
|
-
update:
|
|
399
|
-
}),
|
|
400
|
-
const
|
|
401
|
-
|
|
402
|
-
syncKey: typeof
|
|
403
|
-
rollBackState:
|
|
404
|
-
actionTimeStamp: Date.now() + (
|
|
394
|
+
b.current,
|
|
395
|
+
_
|
|
396
|
+
), b.current?.middleware && b.current.middleware({
|
|
397
|
+
updateLog: c,
|
|
398
|
+
update: oe
|
|
399
|
+
}), b.current?.serverSync) {
|
|
400
|
+
const D = n.getState().serverState[g], T = b.current?.serverSync;
|
|
401
|
+
je(g, {
|
|
402
|
+
syncKey: typeof T.syncKey == "string" ? T.syncKey : T.syncKey({ state: h }),
|
|
403
|
+
rollBackState: D,
|
|
404
|
+
actionTimeStamp: Date.now() + (T.debounce ?? 3e3),
|
|
405
405
|
status: "waiting"
|
|
406
406
|
});
|
|
407
407
|
}
|
|
408
|
-
return
|
|
408
|
+
return h;
|
|
409
409
|
});
|
|
410
410
|
};
|
|
411
|
-
|
|
411
|
+
n.getState().updaterState[g] || (K(
|
|
412
412
|
g,
|
|
413
|
-
|
|
413
|
+
ee(
|
|
414
414
|
g,
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
415
|
+
l,
|
|
416
|
+
L.current,
|
|
417
|
+
_
|
|
418
418
|
)
|
|
419
|
-
),
|
|
420
|
-
const r =
|
|
419
|
+
), n.getState().cogsStateStore[g] || J(g, e), n.getState().initialStateGlobal[g] || ge(g, e));
|
|
420
|
+
const r = ke(() => ee(
|
|
421
421
|
g,
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
422
|
+
l,
|
|
423
|
+
L.current,
|
|
424
|
+
_
|
|
425
425
|
), [g]);
|
|
426
|
-
return [
|
|
426
|
+
return [Ee(g), r];
|
|
427
427
|
}
|
|
428
|
-
function
|
|
428
|
+
function ee(e, i, m, u) {
|
|
429
429
|
const f = /* @__PURE__ */ new Map();
|
|
430
|
-
let
|
|
431
|
-
const
|
|
432
|
-
const
|
|
430
|
+
let A = 0;
|
|
431
|
+
const p = (y) => {
|
|
432
|
+
const t = y.join(".");
|
|
433
433
|
for (const [I] of f)
|
|
434
|
-
(I ===
|
|
435
|
-
|
|
434
|
+
(I === t || I.startsWith(t + ".")) && f.delete(I);
|
|
435
|
+
A++;
|
|
436
436
|
}, v = {
|
|
437
|
-
removeValidation: (
|
|
438
|
-
|
|
437
|
+
removeValidation: (y) => {
|
|
438
|
+
y?.validationKey && q(y.validationKey);
|
|
439
439
|
},
|
|
440
|
-
revertToInitialState: (
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
const I =
|
|
444
|
-
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
const g =
|
|
448
|
-
return g && g.components.forEach((
|
|
449
|
-
|
|
440
|
+
revertToInitialState: (y) => {
|
|
441
|
+
const t = n.getState().getInitialOptions(e)?.validation;
|
|
442
|
+
t?.key && q(t?.key), y?.validationKey && q(y.validationKey);
|
|
443
|
+
const I = n.getState().initialStateGlobal[e];
|
|
444
|
+
n.getState().clearSelectedIndexesForState(e), f.clear(), A++;
|
|
445
|
+
const j = s(I, []), V = Z(e), _ = z(V?.localStorage?.key) ? V?.localStorage?.key(I) : V?.localStorage?.key, R = `${u}-${e}-${_}`;
|
|
446
|
+
R && localStorage.removeItem(R), K(e, j), J(e, I);
|
|
447
|
+
const g = n.getState().stateComponents.get(e);
|
|
448
|
+
return g && g.components.forEach((c) => {
|
|
449
|
+
c.forceUpdate();
|
|
450
450
|
}), I;
|
|
451
451
|
},
|
|
452
|
-
updateInitialState: (
|
|
453
|
-
f.clear(),
|
|
454
|
-
const
|
|
452
|
+
updateInitialState: (y) => {
|
|
453
|
+
f.clear(), A++;
|
|
454
|
+
const t = ee(
|
|
455
455
|
e,
|
|
456
456
|
i,
|
|
457
|
-
|
|
457
|
+
m,
|
|
458
458
|
u
|
|
459
|
-
), I =
|
|
460
|
-
return console.log("removing storage",
|
|
461
|
-
ge(e,
|
|
462
|
-
const
|
|
463
|
-
|
|
459
|
+
), I = n.getState().initialStateGlobal[e], j = Z(e), V = z(j?.localStorage?.key) ? j?.localStorage?.key(I) : j?.localStorage?.key, _ = `${u}-${e}-${V}`;
|
|
460
|
+
return console.log("removing storage", _), localStorage.getItem(_) && localStorage.removeItem(_), Te(() => {
|
|
461
|
+
ge(e, y), K(e, t), J(e, y);
|
|
462
|
+
const R = n.getState().stateComponents.get(e);
|
|
463
|
+
R && R.components.forEach((g) => {
|
|
464
464
|
g.forceUpdate();
|
|
465
465
|
});
|
|
466
466
|
}), {
|
|
467
|
-
fetchId: (
|
|
467
|
+
fetchId: (R) => t.get()[R]
|
|
468
468
|
};
|
|
469
469
|
},
|
|
470
|
-
_initialState:
|
|
471
|
-
_serverState:
|
|
472
|
-
_isLoading:
|
|
470
|
+
_initialState: n.getState().initialStateGlobal[e],
|
|
471
|
+
_serverState: n.getState().serverState[e],
|
|
472
|
+
_isLoading: n.getState().isLoadingGlobal[e],
|
|
473
473
|
_isServerSynced: () => {
|
|
474
|
-
const
|
|
475
|
-
return !!(
|
|
474
|
+
const y = n.getState().serverState[e];
|
|
475
|
+
return !!(y && M(y, Ee(e)));
|
|
476
476
|
}
|
|
477
477
|
};
|
|
478
|
-
function s(
|
|
479
|
-
const
|
|
480
|
-
f.get(
|
|
481
|
-
const
|
|
482
|
-
return
|
|
478
|
+
function s(y, t = [], I) {
|
|
479
|
+
const j = t.map(String).join(".");
|
|
480
|
+
f.get(j);
|
|
481
|
+
const V = function() {
|
|
482
|
+
return n().getNestedState(e, t);
|
|
483
483
|
};
|
|
484
484
|
Object.keys(v).forEach((g) => {
|
|
485
|
-
|
|
485
|
+
V[g] = v[g];
|
|
486
486
|
});
|
|
487
|
-
const
|
|
488
|
-
apply(g,
|
|
487
|
+
const _ = {
|
|
488
|
+
apply(g, c, H) {
|
|
489
489
|
return console.log(
|
|
490
|
-
`PROXY APPLY TRAP HIT: stateKey=${e}, path=${
|
|
491
|
-
), console.trace("Apply trap stack trace"),
|
|
490
|
+
`PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
|
|
491
|
+
), console.trace("Apply trap stack trace"), n().getNestedState(e, t);
|
|
492
492
|
},
|
|
493
|
-
get(g,
|
|
494
|
-
if (
|
|
495
|
-
const
|
|
493
|
+
get(g, c) {
|
|
494
|
+
if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender") {
|
|
495
|
+
const l = t.join("."), r = `${e}////${m}`, o = n.getState().stateComponents.get(e);
|
|
496
496
|
if (o) {
|
|
497
497
|
const a = o.components.get(r);
|
|
498
|
-
a && (
|
|
498
|
+
a && (t.length > 0 || c === "get") && a.paths.add(l);
|
|
499
499
|
}
|
|
500
500
|
}
|
|
501
|
-
if (
|
|
501
|
+
if (c === "getDifferences")
|
|
502
502
|
return () => he(
|
|
503
|
-
|
|
504
|
-
|
|
503
|
+
n.getState().cogsStateStore[e],
|
|
504
|
+
n.getState().initialStateGlobal[e]
|
|
505
505
|
);
|
|
506
|
-
if (
|
|
506
|
+
if (c === "sync" && t.length === 0)
|
|
507
507
|
return async function() {
|
|
508
|
-
const
|
|
508
|
+
const l = n.getState().getInitialOptions(e), r = l?.sync;
|
|
509
509
|
if (!r)
|
|
510
510
|
return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
|
|
511
|
-
const o =
|
|
511
|
+
const o = n.getState().getNestedState(e, []), a = l?.validation?.key;
|
|
512
512
|
try {
|
|
513
|
-
const
|
|
514
|
-
if (
|
|
515
|
-
|
|
516
|
-
const
|
|
517
|
-
|
|
513
|
+
const d = await r.action(o);
|
|
514
|
+
if (d && !d.success && d.errors && a) {
|
|
515
|
+
n.getState().removeValidationError(a), d.errors.forEach((S) => {
|
|
516
|
+
const h = [a, ...S.path].join(".");
|
|
517
|
+
n.getState().addValidationError(h, S.message);
|
|
518
518
|
});
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
|
|
519
|
+
const w = n.getState().stateComponents.get(e);
|
|
520
|
+
w && w.components.forEach((S) => {
|
|
521
|
+
S.forceUpdate();
|
|
522
522
|
});
|
|
523
523
|
}
|
|
524
|
-
return
|
|
525
|
-
} catch (
|
|
526
|
-
return r.onError && r.onError(
|
|
524
|
+
return d?.success && r.onSuccess ? r.onSuccess(d.data) : !d?.success && r.onError && r.onError(d.error), d;
|
|
525
|
+
} catch (d) {
|
|
526
|
+
return r.onError && r.onError(d), { success: !1, error: d };
|
|
527
527
|
}
|
|
528
528
|
};
|
|
529
|
-
if (
|
|
530
|
-
const
|
|
531
|
-
return
|
|
529
|
+
if (c === "_status") {
|
|
530
|
+
const l = n.getState().getNestedState(e, t), r = n.getState().initialStateGlobal[e], o = W(r, t);
|
|
531
|
+
return M(l, o) ? "fresh" : "stale";
|
|
532
532
|
}
|
|
533
|
-
if (
|
|
533
|
+
if (c === "getStatus")
|
|
534
534
|
return function() {
|
|
535
|
-
const
|
|
535
|
+
const l = n().getNestedState(
|
|
536
536
|
e,
|
|
537
|
-
|
|
538
|
-
), r =
|
|
539
|
-
return
|
|
537
|
+
t
|
|
538
|
+
), r = n.getState().initialStateGlobal[e], o = W(r, t);
|
|
539
|
+
return M(l, o) ? "fresh" : "stale";
|
|
540
540
|
};
|
|
541
|
-
if (
|
|
541
|
+
if (c === "removeStorage")
|
|
542
542
|
return () => {
|
|
543
|
-
const
|
|
543
|
+
const l = n.getState().initialStateGlobal[e], r = Z(e), o = z(r?.localStorage?.key) ? r?.localStorage?.key(l) : r?.localStorage?.key, a = `${u}-${e}-${o}`;
|
|
544
544
|
console.log("removing storage", a), a && localStorage.removeItem(a);
|
|
545
545
|
};
|
|
546
|
-
if (
|
|
546
|
+
if (c === "showValidationErrors")
|
|
547
547
|
return () => {
|
|
548
|
-
const
|
|
549
|
-
if (!
|
|
548
|
+
const l = n.getState().getInitialOptions(e)?.validation;
|
|
549
|
+
if (!l?.key)
|
|
550
550
|
throw new Error("Validation key not found");
|
|
551
|
-
return
|
|
551
|
+
return n.getState().getValidationErrors(l.key + "." + t.join("."));
|
|
552
552
|
};
|
|
553
|
-
if (Array.isArray(
|
|
554
|
-
const
|
|
553
|
+
if (Array.isArray(y)) {
|
|
554
|
+
const l = () => I?.validIndices ? y.map((o, a) => ({
|
|
555
555
|
item: o,
|
|
556
556
|
originalIndex: I.validIndices[a]
|
|
557
|
-
})) :
|
|
557
|
+
})) : n.getState().getNestedState(e, t).map((o, a) => ({
|
|
558
558
|
item: o,
|
|
559
559
|
originalIndex: a
|
|
560
560
|
}));
|
|
561
|
-
if (
|
|
561
|
+
if (c === "getSelected")
|
|
562
562
|
return () => {
|
|
563
|
-
const r =
|
|
563
|
+
const r = n.getState().getSelectedIndex(e, t.join("."));
|
|
564
564
|
if (r !== void 0)
|
|
565
565
|
return s(
|
|
566
|
-
|
|
567
|
-
[...
|
|
566
|
+
y[r],
|
|
567
|
+
[...t, r.toString()],
|
|
568
568
|
I
|
|
569
569
|
);
|
|
570
570
|
};
|
|
571
|
-
if (
|
|
571
|
+
if (c === "clearSelected")
|
|
572
572
|
return () => {
|
|
573
|
-
|
|
573
|
+
n.getState().clearSelectedIndex({ stateKey: e, path: t });
|
|
574
574
|
};
|
|
575
|
-
if (
|
|
576
|
-
return () =>
|
|
577
|
-
if (
|
|
575
|
+
if (c === "getSelectedIndex")
|
|
576
|
+
return () => n.getState().getSelectedIndex(e, t.join(".")) ?? -1;
|
|
577
|
+
if (c === "stateSort")
|
|
578
578
|
return (r) => {
|
|
579
|
-
const a = [...
|
|
580
|
-
(
|
|
581
|
-
),
|
|
579
|
+
const a = [...l()].sort(
|
|
580
|
+
(S, h) => r(S.item, h.item)
|
|
581
|
+
), d = a.map(({ item: S }) => S), w = {
|
|
582
582
|
...I,
|
|
583
583
|
validIndices: a.map(
|
|
584
|
-
({ originalIndex:
|
|
584
|
+
({ originalIndex: S }) => S
|
|
585
585
|
)
|
|
586
586
|
};
|
|
587
|
-
return s(
|
|
587
|
+
return s(d, t, w);
|
|
588
588
|
};
|
|
589
|
-
if (
|
|
589
|
+
if (c === "stateFilter")
|
|
590
590
|
return (r) => {
|
|
591
|
-
const a =
|
|
592
|
-
({ item:
|
|
593
|
-
),
|
|
591
|
+
const a = l().filter(
|
|
592
|
+
({ item: S }, h) => r(S, h)
|
|
593
|
+
), d = a.map(({ item: S }) => S), w = {
|
|
594
594
|
...I,
|
|
595
595
|
validIndices: a.map(
|
|
596
|
-
({ originalIndex:
|
|
596
|
+
({ originalIndex: S }) => S
|
|
597
597
|
)
|
|
598
598
|
};
|
|
599
|
-
return s(
|
|
599
|
+
return s(d, t, w);
|
|
600
600
|
};
|
|
601
|
-
if (
|
|
602
|
-
return (r) =>
|
|
603
|
-
let
|
|
604
|
-
I?.validIndices && I.validIndices[
|
|
605
|
-
const
|
|
601
|
+
if (c === "stateMap" || c === "stateMapNoRender")
|
|
602
|
+
return (r) => y.map((a, d) => {
|
|
603
|
+
let w;
|
|
604
|
+
I?.validIndices && I.validIndices[d] !== void 0 ? w = I.validIndices[d] : w = d;
|
|
605
|
+
const S = [...t, w.toString()], h = s(a, S, I);
|
|
606
606
|
return r(
|
|
607
607
|
a,
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
s(
|
|
608
|
+
h,
|
|
609
|
+
d,
|
|
610
|
+
y,
|
|
611
|
+
s(y, t, I)
|
|
612
612
|
);
|
|
613
613
|
});
|
|
614
|
-
if (
|
|
615
|
-
return (r) =>
|
|
614
|
+
if (c === "$stateMap")
|
|
615
|
+
return (r) => te(Me, {
|
|
616
616
|
proxy: {
|
|
617
617
|
_stateKey: e,
|
|
618
|
-
_path:
|
|
618
|
+
_path: t,
|
|
619
619
|
_mapFn: r
|
|
620
620
|
// Pass the actual function, not string
|
|
621
621
|
},
|
|
622
622
|
rebuildStateShape: s
|
|
623
623
|
});
|
|
624
|
-
if (
|
|
624
|
+
if (c === "stateFlattenOn")
|
|
625
625
|
return (r) => {
|
|
626
|
-
const o =
|
|
627
|
-
f.clear(),
|
|
626
|
+
const o = y;
|
|
627
|
+
f.clear(), A++;
|
|
628
628
|
const a = o.flatMap(
|
|
629
|
-
(
|
|
629
|
+
(d) => d[r] ?? []
|
|
630
630
|
);
|
|
631
631
|
return s(
|
|
632
632
|
a,
|
|
633
|
-
[...
|
|
633
|
+
[...t, "[*]", r],
|
|
634
634
|
I
|
|
635
635
|
);
|
|
636
636
|
};
|
|
637
|
-
if (
|
|
637
|
+
if (c === "index")
|
|
638
638
|
return (r) => {
|
|
639
|
-
const o =
|
|
640
|
-
return s(o, [...
|
|
639
|
+
const o = y[r];
|
|
640
|
+
return s(o, [...t, r.toString()]);
|
|
641
641
|
};
|
|
642
|
-
if (
|
|
642
|
+
if (c === "last")
|
|
643
643
|
return () => {
|
|
644
|
-
const r =
|
|
644
|
+
const r = n.getState().getNestedState(e, t);
|
|
645
645
|
if (r.length === 0) return;
|
|
646
|
-
const o = r.length - 1, a = r[o],
|
|
647
|
-
return s(a,
|
|
646
|
+
const o = r.length - 1, a = r[o], d = [...t, o.toString()];
|
|
647
|
+
return s(a, d);
|
|
648
648
|
};
|
|
649
|
-
if (
|
|
650
|
-
return (r) => (
|
|
651
|
-
|
|
652
|
-
|
|
649
|
+
if (c === "insert")
|
|
650
|
+
return (r) => (p(t), se(i, r, t, e), s(
|
|
651
|
+
n.getState().getNestedState(e, t),
|
|
652
|
+
t
|
|
653
653
|
));
|
|
654
|
-
if (
|
|
654
|
+
if (c === "uniqueInsert")
|
|
655
655
|
return (r, o, a) => {
|
|
656
|
-
const
|
|
657
|
-
let
|
|
658
|
-
if (!
|
|
656
|
+
const d = n.getState().getNestedState(e, t), w = z(r) ? r(d) : r;
|
|
657
|
+
let S = null;
|
|
658
|
+
if (!d.some((E) => {
|
|
659
659
|
if (o) {
|
|
660
|
-
const
|
|
661
|
-
(
|
|
660
|
+
const N = o.every(
|
|
661
|
+
(P) => M(E[P], w[P])
|
|
662
662
|
);
|
|
663
|
-
return
|
|
663
|
+
return N && (S = E), N;
|
|
664
664
|
}
|
|
665
|
-
const
|
|
666
|
-
return
|
|
665
|
+
const $ = M(E, w);
|
|
666
|
+
return $ && (S = E), $;
|
|
667
667
|
}))
|
|
668
|
-
|
|
669
|
-
else if (a &&
|
|
670
|
-
const
|
|
671
|
-
(
|
|
668
|
+
p(t), se(i, w, t, e);
|
|
669
|
+
else if (a && S) {
|
|
670
|
+
const E = a(S), $ = d.map(
|
|
671
|
+
(N) => M(N, S) ? E : N
|
|
672
672
|
);
|
|
673
|
-
|
|
673
|
+
p(t), Y(i, $, t);
|
|
674
674
|
}
|
|
675
675
|
};
|
|
676
|
-
if (
|
|
676
|
+
if (c === "cut")
|
|
677
677
|
return (r, o) => {
|
|
678
678
|
if (!o?.waitForSync)
|
|
679
|
-
return
|
|
680
|
-
|
|
681
|
-
|
|
679
|
+
return p(t), X(i, t, e, r), s(
|
|
680
|
+
n.getState().getNestedState(e, t),
|
|
681
|
+
t
|
|
682
682
|
);
|
|
683
683
|
};
|
|
684
|
-
if (
|
|
684
|
+
if (c === "cutByValue")
|
|
685
685
|
return (r) => {
|
|
686
|
-
for (let o = 0; o <
|
|
687
|
-
|
|
686
|
+
for (let o = 0; o < y.length; o++)
|
|
687
|
+
y[o] === r && X(i, t, e, o);
|
|
688
688
|
};
|
|
689
|
-
if (
|
|
689
|
+
if (c === "toggleByValue")
|
|
690
690
|
return (r) => {
|
|
691
|
-
const o =
|
|
692
|
-
o > -1 ?
|
|
691
|
+
const o = y.findIndex((a) => a === r);
|
|
692
|
+
o > -1 ? X(i, t, e, o) : se(i, r, t, e);
|
|
693
693
|
};
|
|
694
|
-
if (
|
|
694
|
+
if (c === "stateFind")
|
|
695
695
|
return (r) => {
|
|
696
|
-
const a =
|
|
697
|
-
({ item:
|
|
696
|
+
const a = l().find(
|
|
697
|
+
({ item: w }, S) => r(w, S)
|
|
698
698
|
);
|
|
699
699
|
if (!a) return;
|
|
700
|
-
const
|
|
701
|
-
return s(a.item,
|
|
700
|
+
const d = [...t, a.originalIndex.toString()];
|
|
701
|
+
return s(a.item, d, I);
|
|
702
702
|
};
|
|
703
|
-
if (
|
|
703
|
+
if (c === "findWith")
|
|
704
704
|
return (r, o) => {
|
|
705
|
-
const
|
|
706
|
-
({ item:
|
|
705
|
+
const d = l().find(
|
|
706
|
+
({ item: S }) => S[r] === o
|
|
707
707
|
);
|
|
708
|
-
if (!
|
|
709
|
-
const
|
|
710
|
-
return s(
|
|
708
|
+
if (!d) return;
|
|
709
|
+
const w = [...t, d.originalIndex.toString()];
|
|
710
|
+
return s(d.item, w, I);
|
|
711
711
|
};
|
|
712
712
|
}
|
|
713
|
-
const
|
|
714
|
-
if (!isNaN(Number(
|
|
715
|
-
const
|
|
716
|
-
if (Array.isArray(r) &&
|
|
717
|
-
return () =>
|
|
713
|
+
const H = t[t.length - 1];
|
|
714
|
+
if (!isNaN(Number(H))) {
|
|
715
|
+
const l = t.slice(0, -1), r = n.getState().getNestedState(e, l);
|
|
716
|
+
if (Array.isArray(r) && c === "cut")
|
|
717
|
+
return () => X(
|
|
718
718
|
i,
|
|
719
|
-
|
|
719
|
+
l,
|
|
720
720
|
e,
|
|
721
|
-
Number(
|
|
721
|
+
Number(H)
|
|
722
722
|
);
|
|
723
723
|
}
|
|
724
|
-
if (
|
|
725
|
-
return () =>
|
|
726
|
-
if (
|
|
727
|
-
return (
|
|
724
|
+
if (c === "get")
|
|
725
|
+
return () => n.getState().getNestedState(e, t);
|
|
726
|
+
if (c === "$derive")
|
|
727
|
+
return (l) => ce({
|
|
728
728
|
_stateKey: e,
|
|
729
|
-
_path:
|
|
730
|
-
_effect:
|
|
729
|
+
_path: t,
|
|
730
|
+
_effect: l.toString()
|
|
731
731
|
});
|
|
732
|
-
if (
|
|
733
|
-
return (
|
|
732
|
+
if (c === "$derive")
|
|
733
|
+
return (l) => ce({
|
|
734
734
|
_stateKey: e,
|
|
735
|
-
_path:
|
|
736
|
-
_effect:
|
|
735
|
+
_path: t,
|
|
736
|
+
_effect: l.toString()
|
|
737
737
|
});
|
|
738
|
-
if (
|
|
738
|
+
if (c === "$get")
|
|
739
739
|
return () => ce({
|
|
740
740
|
_stateKey: e,
|
|
741
|
-
_path:
|
|
741
|
+
_path: t
|
|
742
742
|
});
|
|
743
|
-
if (
|
|
744
|
-
const
|
|
745
|
-
return
|
|
743
|
+
if (c === "lastSynced") {
|
|
744
|
+
const l = `${e}:${t.join(".")}`;
|
|
745
|
+
return n.getState().getSyncInfo(l);
|
|
746
746
|
}
|
|
747
|
-
if (
|
|
748
|
-
return (
|
|
749
|
-
if (
|
|
750
|
-
const
|
|
751
|
-
return Array.isArray(o) ? Number(
|
|
747
|
+
if (c == "getLocalStorage")
|
|
748
|
+
return (l) => ne(u + "-" + e + "-" + l);
|
|
749
|
+
if (c === "_selected") {
|
|
750
|
+
const l = t.slice(0, -1), r = l.join("."), o = n.getState().getNestedState(e, l);
|
|
751
|
+
return Array.isArray(o) ? Number(t[t.length - 1]) === n.getState().getSelectedIndex(e, r) : void 0;
|
|
752
752
|
}
|
|
753
|
-
if (
|
|
754
|
-
return (
|
|
755
|
-
const r =
|
|
756
|
-
|
|
757
|
-
const
|
|
758
|
-
|
|
753
|
+
if (c === "setSelected")
|
|
754
|
+
return (l) => {
|
|
755
|
+
const r = t.slice(0, -1), o = Number(t[t.length - 1]), a = r.join(".");
|
|
756
|
+
l ? n.getState().setSelectedIndex(e, a, o) : n.getState().setSelectedIndex(e, a, void 0);
|
|
757
|
+
const d = n.getState().getNestedState(e, [...r]);
|
|
758
|
+
Y(i, d, r), p(r);
|
|
759
759
|
};
|
|
760
|
-
if (
|
|
760
|
+
if (c === "toggleSelected")
|
|
761
761
|
return () => {
|
|
762
|
-
const
|
|
763
|
-
|
|
762
|
+
const l = t.slice(0, -1), r = Number(t[t.length - 1]), o = l.join("."), a = n.getState().getSelectedIndex(e, o);
|
|
763
|
+
n.getState().setSelectedIndex(
|
|
764
764
|
e,
|
|
765
765
|
o,
|
|
766
766
|
a === r ? void 0 : r
|
|
767
767
|
);
|
|
768
|
-
const
|
|
769
|
-
|
|
768
|
+
const d = n.getState().getNestedState(e, [...l]);
|
|
769
|
+
Y(i, d, l), p(l);
|
|
770
770
|
};
|
|
771
|
-
if (
|
|
772
|
-
if (
|
|
773
|
-
return (
|
|
774
|
-
const r =
|
|
775
|
-
|
|
776
|
-
...c,
|
|
777
|
-
middleware: void 0
|
|
778
|
-
}), Ee(
|
|
771
|
+
if (t.length == 0) {
|
|
772
|
+
if (c === "applyJsonPatch")
|
|
773
|
+
return (l) => {
|
|
774
|
+
const r = n.getState().cogsStateStore[e], a = Ce(r, l).newDocument;
|
|
775
|
+
$e(
|
|
779
776
|
e,
|
|
780
|
-
|
|
777
|
+
n.getState().initialStateGlobal[e],
|
|
781
778
|
a,
|
|
782
779
|
i,
|
|
783
|
-
|
|
780
|
+
m,
|
|
784
781
|
u
|
|
785
|
-
)
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
782
|
+
);
|
|
783
|
+
const d = n.getState().stateComponents.get(e);
|
|
784
|
+
if (!d) return;
|
|
785
|
+
const w = /* @__PURE__ */ new Set(), S = (h, E) => {
|
|
786
|
+
w.add(h), Array.isArray(E) ? E.forEach(($, N) => {
|
|
787
|
+
S(`${h}.${N}`, $);
|
|
788
|
+
}) : typeof E == "object" && E !== null && Object.keys(E).forEach(($) => {
|
|
789
|
+
S(`${h}.${$}`, E[$]);
|
|
790
|
+
});
|
|
791
|
+
};
|
|
792
|
+
l.forEach((h) => {
|
|
793
|
+
const E = h.path.slice(1).replace(/\//g, ".");
|
|
794
|
+
w.add(E);
|
|
795
|
+
let $ = E;
|
|
796
|
+
for (; $.includes("."); )
|
|
797
|
+
$ = $.substring(
|
|
798
|
+
0,
|
|
799
|
+
$.lastIndexOf(".")
|
|
800
|
+
), w.add($);
|
|
801
|
+
w.add(""), (h.op === "add" || h.op === "replace") && h.value && S(E, h.value);
|
|
805
802
|
});
|
|
806
803
|
for (const [
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
] of
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
T.forceUpdate();
|
|
804
|
+
h,
|
|
805
|
+
E
|
|
806
|
+
] of d.components.entries()) {
|
|
807
|
+
let $ = !1;
|
|
808
|
+
const N = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
|
|
809
|
+
if (!N.includes("none")) {
|
|
810
|
+
if (N.includes("all")) {
|
|
811
|
+
E.forceUpdate();
|
|
816
812
|
continue;
|
|
817
813
|
}
|
|
818
|
-
if (
|
|
819
|
-
for (const
|
|
820
|
-
if (
|
|
821
|
-
|
|
814
|
+
if (N.includes("component"))
|
|
815
|
+
for (const P of E.paths) {
|
|
816
|
+
if (w.has(P)) {
|
|
817
|
+
$ = !0;
|
|
822
818
|
break;
|
|
823
819
|
}
|
|
824
|
-
for (const
|
|
825
|
-
if (
|
|
826
|
-
|
|
820
|
+
for (const B of w)
|
|
821
|
+
if (P.startsWith(B + ".")) {
|
|
822
|
+
$ = !0;
|
|
827
823
|
break;
|
|
828
824
|
}
|
|
829
|
-
if (
|
|
825
|
+
if ($) break;
|
|
830
826
|
}
|
|
831
|
-
if (
|
|
832
|
-
const
|
|
833
|
-
|
|
834
|
-
typeof b == "boolean" ? b && (x = !0) : L(T.deps, b) || (T.deps = b, x = !0), x && (V = !0);
|
|
827
|
+
if (!$ && N.includes("deps") && E.depsFunction) {
|
|
828
|
+
const P = E.depsFunction(a);
|
|
829
|
+
(typeof P == "boolean" ? P : !M(E.deps, P)) && (E.deps = Array.isArray(P) ? P : [], $ = !0);
|
|
835
830
|
}
|
|
836
|
-
|
|
831
|
+
$ && E.forceUpdate();
|
|
837
832
|
}
|
|
838
833
|
}
|
|
839
834
|
};
|
|
840
|
-
if (
|
|
835
|
+
if (c === "validateZodSchema")
|
|
841
836
|
return () => {
|
|
842
|
-
const
|
|
843
|
-
if (!
|
|
837
|
+
const l = n.getState().getInitialOptions(e)?.validation, r = n.getState().addValidationError;
|
|
838
|
+
if (!l?.zodSchema)
|
|
844
839
|
throw new Error("Zod schema not found");
|
|
845
|
-
if (!
|
|
840
|
+
if (!l?.key)
|
|
846
841
|
throw new Error("Validation key not found");
|
|
847
|
-
|
|
848
|
-
const o =
|
|
842
|
+
q(l.key);
|
|
843
|
+
const o = n.getState().cogsStateStore[e];
|
|
849
844
|
try {
|
|
850
|
-
const a =
|
|
851
|
-
a && a.length > 0 && a.forEach(([
|
|
852
|
-
|
|
845
|
+
const a = n.getState().getValidationErrors(l.key);
|
|
846
|
+
a && a.length > 0 && a.forEach(([w]) => {
|
|
847
|
+
w && w.startsWith(l.key) && q(w);
|
|
853
848
|
});
|
|
854
|
-
const
|
|
855
|
-
return
|
|
856
|
-
const
|
|
857
|
-
r(
|
|
858
|
-
}),
|
|
849
|
+
const d = l.zodSchema.safeParse(o);
|
|
850
|
+
return d.success ? !0 : (d.error.errors.forEach((S) => {
|
|
851
|
+
const h = S.path, E = S.message, $ = [l.key, ...h].join(".");
|
|
852
|
+
r($, E);
|
|
853
|
+
}), re(e), !1);
|
|
859
854
|
} catch (a) {
|
|
860
855
|
return console.error("Zod schema validation failed", a), !1;
|
|
861
856
|
}
|
|
862
857
|
};
|
|
863
|
-
if (
|
|
864
|
-
if (
|
|
865
|
-
return () =>
|
|
866
|
-
if (
|
|
858
|
+
if (c === "_componentId") return m;
|
|
859
|
+
if (c === "getComponents")
|
|
860
|
+
return () => n().stateComponents.get(e);
|
|
861
|
+
if (c === "getAllFormRefs")
|
|
867
862
|
return () => me.getState().getFormRefsByStateKey(e);
|
|
868
|
-
if (
|
|
869
|
-
return
|
|
870
|
-
if (
|
|
871
|
-
return
|
|
872
|
-
if (
|
|
873
|
-
return
|
|
874
|
-
if (
|
|
863
|
+
if (c === "_initialState")
|
|
864
|
+
return n.getState().initialStateGlobal[e];
|
|
865
|
+
if (c === "_serverState")
|
|
866
|
+
return n.getState().serverState[e];
|
|
867
|
+
if (c === "_isLoading")
|
|
868
|
+
return n.getState().isLoadingGlobal[e];
|
|
869
|
+
if (c === "revertToInitialState")
|
|
875
870
|
return v.revertToInitialState;
|
|
876
|
-
if (
|
|
877
|
-
if (
|
|
871
|
+
if (c === "updateInitialState") return v.updateInitialState;
|
|
872
|
+
if (c === "removeValidation") return v.removeValidation;
|
|
878
873
|
}
|
|
879
|
-
if (
|
|
880
|
-
return () => me.getState().getFormRef(e + "." +
|
|
881
|
-
if (
|
|
874
|
+
if (c === "getFormRef")
|
|
875
|
+
return () => me.getState().getFormRef(e + "." + t.join("."));
|
|
876
|
+
if (c === "validationWrapper")
|
|
882
877
|
return ({
|
|
883
|
-
children:
|
|
878
|
+
children: l,
|
|
884
879
|
hideMessage: r
|
|
885
880
|
}) => /* @__PURE__ */ Se(
|
|
886
881
|
_e,
|
|
887
882
|
{
|
|
888
883
|
formOpts: r ? { validation: { message: "" } } : void 0,
|
|
889
|
-
path:
|
|
890
|
-
validationKey:
|
|
884
|
+
path: t,
|
|
885
|
+
validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
|
|
891
886
|
stateKey: e,
|
|
892
887
|
validIndices: I?.validIndices,
|
|
893
|
-
children:
|
|
888
|
+
children: l
|
|
894
889
|
}
|
|
895
890
|
);
|
|
896
|
-
if (
|
|
897
|
-
if (
|
|
898
|
-
if (
|
|
899
|
-
if (
|
|
900
|
-
return (
|
|
891
|
+
if (c === "_stateKey") return e;
|
|
892
|
+
if (c === "_path") return t;
|
|
893
|
+
if (c === "_isServerSynced") return v._isServerSynced;
|
|
894
|
+
if (c === "update")
|
|
895
|
+
return (l, r) => {
|
|
901
896
|
if (r?.debounce)
|
|
902
897
|
Ve(() => {
|
|
903
|
-
|
|
904
|
-
const o =
|
|
898
|
+
Y(i, l, t, "");
|
|
899
|
+
const o = n.getState().getNestedState(e, t);
|
|
905
900
|
r?.afterUpdate && r.afterUpdate(o);
|
|
906
901
|
}, r.debounce);
|
|
907
902
|
else {
|
|
908
|
-
|
|
909
|
-
const o =
|
|
903
|
+
Y(i, l, t, "");
|
|
904
|
+
const o = n.getState().getNestedState(e, t);
|
|
910
905
|
r?.afterUpdate && r.afterUpdate(o);
|
|
911
906
|
}
|
|
912
|
-
|
|
907
|
+
p(t);
|
|
913
908
|
};
|
|
914
|
-
if (
|
|
915
|
-
return (
|
|
916
|
-
|
|
909
|
+
if (c === "formElement")
|
|
910
|
+
return (l, r) => /* @__PURE__ */ Se(
|
|
911
|
+
be,
|
|
917
912
|
{
|
|
918
913
|
setState: i,
|
|
919
914
|
stateKey: e,
|
|
920
|
-
path:
|
|
921
|
-
child:
|
|
915
|
+
path: t,
|
|
916
|
+
child: l,
|
|
922
917
|
formOpts: r
|
|
923
918
|
}
|
|
924
919
|
);
|
|
925
|
-
const
|
|
926
|
-
return s(
|
|
920
|
+
const L = [...t, c], b = n.getState().getNestedState(e, L);
|
|
921
|
+
return s(b, L, I);
|
|
927
922
|
}
|
|
928
|
-
},
|
|
929
|
-
return f.set(
|
|
930
|
-
proxy:
|
|
931
|
-
stateVersion:
|
|
932
|
-
}),
|
|
923
|
+
}, R = new Proxy(V, _);
|
|
924
|
+
return f.set(j, {
|
|
925
|
+
proxy: R,
|
|
926
|
+
stateVersion: A
|
|
927
|
+
}), R;
|
|
933
928
|
}
|
|
934
929
|
return s(
|
|
935
|
-
|
|
930
|
+
n.getState().getNestedState(e, [])
|
|
936
931
|
);
|
|
937
932
|
}
|
|
938
933
|
function ce(e) {
|
|
939
|
-
return
|
|
934
|
+
return te(We, { proxy: e });
|
|
940
935
|
}
|
|
941
|
-
function
|
|
936
|
+
function Me({
|
|
942
937
|
proxy: e,
|
|
943
938
|
rebuildStateShape: i
|
|
944
939
|
}) {
|
|
945
|
-
const
|
|
946
|
-
return Array.isArray(
|
|
947
|
-
|
|
940
|
+
const m = n().getNestedState(e._stateKey, e._path);
|
|
941
|
+
return Array.isArray(m) ? i(
|
|
942
|
+
m,
|
|
948
943
|
e._path
|
|
949
944
|
).stateMapNoRender(
|
|
950
|
-
(f,
|
|
945
|
+
(f, A, p, v, s) => e._mapFn(f, A, p, v, s)
|
|
951
946
|
) : null;
|
|
952
947
|
}
|
|
953
948
|
function We({
|
|
954
949
|
proxy: e
|
|
955
950
|
}) {
|
|
956
|
-
const i =
|
|
951
|
+
const i = Q(null), m = `${e._stateKey}-${e._path.join(".")}`;
|
|
957
952
|
return de(() => {
|
|
958
953
|
const u = i.current;
|
|
959
954
|
if (!u || !u.parentElement) return;
|
|
960
|
-
const f = u.parentElement,
|
|
955
|
+
const f = u.parentElement, p = Array.from(f.childNodes).indexOf(u);
|
|
961
956
|
let v = f.getAttribute("data-parent-id");
|
|
962
957
|
v || (v = `parent-${crypto.randomUUID()}`, f.setAttribute("data-parent-id", v));
|
|
963
|
-
const
|
|
958
|
+
const y = {
|
|
964
959
|
instanceId: `instance-${crypto.randomUUID()}`,
|
|
965
960
|
parentId: v,
|
|
966
|
-
position:
|
|
961
|
+
position: p,
|
|
967
962
|
effect: e._effect
|
|
968
963
|
};
|
|
969
|
-
|
|
970
|
-
const
|
|
964
|
+
n.getState().addSignalElement(m, y);
|
|
965
|
+
const t = n.getState().getNestedState(e._stateKey, e._path);
|
|
971
966
|
let I;
|
|
972
967
|
if (e._effect)
|
|
973
968
|
try {
|
|
974
969
|
I = new Function(
|
|
975
970
|
"state",
|
|
976
971
|
`return (${e._effect})(state)`
|
|
977
|
-
)(
|
|
978
|
-
} catch (
|
|
979
|
-
console.error("Error evaluating effect function during mount:",
|
|
972
|
+
)(t);
|
|
973
|
+
} catch (V) {
|
|
974
|
+
console.error("Error evaluating effect function during mount:", V), I = t;
|
|
980
975
|
}
|
|
981
976
|
else
|
|
982
|
-
I =
|
|
977
|
+
I = t;
|
|
983
978
|
I !== null && typeof I == "object" && (I = JSON.stringify(I));
|
|
984
|
-
const
|
|
985
|
-
u.replaceWith(
|
|
986
|
-
}, [e._stateKey, e._path.join("."), e._effect]),
|
|
979
|
+
const j = document.createTextNode(String(I));
|
|
980
|
+
u.replaceWith(j);
|
|
981
|
+
}, [e._stateKey, e._path.join("."), e._effect]), te("span", {
|
|
987
982
|
ref: i,
|
|
988
983
|
style: { display: "none" },
|
|
989
|
-
"data-signal-id":
|
|
984
|
+
"data-signal-id": m
|
|
990
985
|
});
|
|
991
986
|
}
|
|
992
987
|
function tt(e) {
|
|
993
|
-
const i =
|
|
994
|
-
(
|
|
995
|
-
const u =
|
|
988
|
+
const i = Ae(
|
|
989
|
+
(m) => {
|
|
990
|
+
const u = n.getState().stateComponents.get(e._stateKey) || {
|
|
996
991
|
components: /* @__PURE__ */ new Map()
|
|
997
992
|
};
|
|
998
993
|
return u.components.set(e._stateKey, {
|
|
999
|
-
forceUpdate:
|
|
994
|
+
forceUpdate: m,
|
|
1000
995
|
paths: /* @__PURE__ */ new Set([e._path.join(".")])
|
|
1001
996
|
}), () => u.components.delete(e._stateKey);
|
|
1002
997
|
},
|
|
1003
|
-
() =>
|
|
998
|
+
() => n.getState().getNestedState(e._stateKey, e._path)
|
|
1004
999
|
);
|
|
1005
|
-
return
|
|
1000
|
+
return te("text", {}, String(i));
|
|
1006
1001
|
}
|
|
1007
1002
|
export {
|
|
1008
1003
|
ce as $cogsSignal,
|
|
@@ -1010,6 +1005,6 @@ export {
|
|
|
1010
1005
|
Qe as addStateOptions,
|
|
1011
1006
|
Ke as createCogsState,
|
|
1012
1007
|
et as notifyComponent,
|
|
1013
|
-
|
|
1008
|
+
De as useCogsStateFn
|
|
1014
1009
|
};
|
|
1015
1010
|
//# sourceMappingURL=CogsState.jsx.map
|