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