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