cogsbox-state 0.5.426 → 0.5.428

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/store.js CHANGED
@@ -1,137 +1,112 @@
1
- import { create as u } from "zustand";
2
- const w = u((s, S) => ({
1
+ import { create as h } from "zustand";
2
+ import { ulid as u } from "ulid";
3
+ const b = h((a, l) => ({
3
4
  formRefs: /* @__PURE__ */ new Map(),
4
- registerFormRef: (e, r) => s((t) => {
5
+ registerFormRef: (e, r) => a((t) => {
5
6
  const n = new Map(t.formRefs);
6
7
  return n.set(e, r), { formRefs: n };
7
8
  }),
8
- getFormRef: (e) => S().formRefs.get(e),
9
- removeFormRef: (e) => s((r) => {
9
+ getFormRef: (e) => l().formRefs.get(e),
10
+ removeFormRef: (e) => a((r) => {
10
11
  const t = new Map(r.formRefs);
11
12
  return t.delete(e), { formRefs: t };
12
13
  }),
13
14
  // Get all refs that start with the stateKey prefix
14
15
  getFormRefsByStateKey: (e) => {
15
- const r = S().formRefs, t = e + ".", n = /* @__PURE__ */ new Map();
16
- return r.forEach((a, o) => {
17
- (o.startsWith(t) || o === e) && n.set(o, a);
16
+ const r = l().formRefs, t = e + ".", n = /* @__PURE__ */ new Map();
17
+ return r.forEach((s, o) => {
18
+ (o.startsWith(t) || o === e) && n.set(o, s);
18
19
  }), n;
19
20
  }
20
- })), h = u((s, S) => ({
21
- shadowStateStore: {},
22
- getShadowMetadata: (e, r) => {
23
- const t = S().shadowStateStore[e];
24
- if (!t) return null;
25
- let n = t;
26
- for (const a of r)
27
- if (n = n?.[a], !n) return null;
28
- return n;
29
- },
21
+ })), I = h((a, l) => ({
22
+ shadowStateStore: /* @__PURE__ */ new Map(),
23
+ shadowStateSubscribers: /* @__PURE__ */ new Map(),
24
+ subscribeToShadowState: (e, r) => (a((t) => {
25
+ const n = new Map(t.shadowStateSubscribers), s = n.get(e) || /* @__PURE__ */ new Set();
26
+ return s.add(r), n.set(e, s), { shadowStateSubscribers: n };
27
+ }), () => {
28
+ a((t) => {
29
+ const n = new Map(t.shadowStateSubscribers), s = n.get(e);
30
+ return s && (s.delete(r), s.size === 0 && n.delete(e)), { shadowStateSubscribers: n };
31
+ });
32
+ }),
30
33
  initializeShadowState: (e, r) => {
31
- const t = (n) => {
32
- if (Array.isArray(n))
33
- return new Array(n.length).fill(null).map((a, o) => t(n[o]));
34
- if (typeof n == "object" && n !== null) {
35
- const a = {};
36
- for (const o in n)
37
- a[o] = t(n[o]);
38
- return a;
39
- }
40
- return {};
34
+ const t = /* @__PURE__ */ new Map(), n = (s, o) => {
35
+ const d = [e, ...o].join(".");
36
+ if (Array.isArray(s)) {
37
+ const i = [];
38
+ s.forEach((c) => {
39
+ typeof c == "object" && c !== null && !c.id && (c.id = u());
40
+ const f = `id:${c.id}`;
41
+ i.push(f);
42
+ const p = [...o, f];
43
+ n(c, p);
44
+ });
45
+ const S = {
46
+ id: u(),
47
+ arrayKeys: i
48
+ };
49
+ t.set(d, S);
50
+ } else typeof s == "object" && s !== null ? (t.set(d, { id: u() }), Object.keys(s).forEach((i) => {
51
+ n(s[i], [...o, i]);
52
+ })) : t.set(d, { id: u() });
41
53
  };
42
- s((n) => ({
43
- shadowStateStore: {
44
- ...n.shadowStateStore,
45
- [e]: t(r)
46
- }
47
- }));
54
+ n(r, []), a({ shadowStateStore: t });
48
55
  },
49
- updateShadowAtPath: (e, r, t) => {
50
- s((n) => {
51
- const a = { ...n.shadowStateStore };
52
- if (!a[e]) return n;
53
- let o = a[e];
54
- const i = [...r], l = i.pop();
55
- for (const c of i)
56
- o[c] || (o[c] = {}), o = o[c];
57
- return l !== void 0 && (Array.isArray(t) ? o[l] = new Array(t.length) : typeof t == "object" && t !== null ? o[l] = {} : o[l] = o[l] || {}), { shadowStateStore: a };
58
- });
56
+ getShadowMetadata: (e, r) => {
57
+ const t = [e, ...r].join(".");
58
+ return l().shadowStateStore.get(t);
59
59
  },
60
- insertShadowArrayElement: (e, r, t) => {
61
- s((n) => {
62
- const a = { ...n.shadowStateStore };
63
- if (!a[e]) return n;
64
- a[e] = JSON.parse(JSON.stringify(a[e]));
65
- let o = a[e];
66
- for (const i of r)
67
- if (o = o[i], !o) return n;
68
- if (Array.isArray(o)) {
69
- const i = (l) => {
70
- if (Array.isArray(l))
71
- return l.map((c) => i(c));
72
- if (typeof l == "object" && l !== null) {
73
- const c = {};
74
- for (const d in l)
75
- c[d] = i(l[d]);
76
- return c;
77
- }
78
- return {};
79
- };
80
- o.push(i(t));
81
- }
82
- return { shadowStateStore: a };
83
- });
60
+ setShadowMetadata: (e, r, t) => {
61
+ const n = [e, ...r].join("."), s = new Map(l().shadowStateStore), o = s.get(n) || { id: u() };
62
+ s.set(n, { ...o, ...t }), a({ shadowStateStore: s }), t.virtualizer?.itemHeight && l().shadowStateSubscribers.get(e)?.forEach((i) => i());
84
63
  },
85
- removeShadowArrayElement: (e, r, t) => {
86
- s((n) => {
87
- const a = { ...n.shadowStateStore };
88
- let o = a[e];
89
- for (const i of r)
90
- o = o?.[i];
91
- return Array.isArray(o) && o.splice(t, 1), { shadowStateStore: a };
92
- });
64
+ insertShadowArrayElement: (e, r, t) => {
65
+ const n = new Map(l().shadowStateStore), s = [e, ...r].join("."), o = n.get(s), d = l().getNestedState(e, r);
66
+ if (!o || !o.arrayKeys) return;
67
+ const i = `id:${t.id}`, S = d.findIndex((p) => p.id === t.id);
68
+ if (S === -1) return;
69
+ const c = [...o.arrayKeys];
70
+ c.splice(S, 0, i), n.set(s, { ...o, arrayKeys: c });
71
+ const f = (p, w) => {
72
+ const g = [e, ...w].join(".");
73
+ typeof p == "object" && p !== null ? (n.set(g, { id: u() }), Object.keys(p).forEach((y) => {
74
+ f(p[y], [...w, y]);
75
+ })) : n.set(g, { id: u() });
76
+ };
77
+ f(t, [...r, i]), a({ shadowStateStore: n });
93
78
  },
94
- shadowStateSubscribers: /* @__PURE__ */ new Map(),
95
- // key -> Set of callbacks
96
- subscribeToShadowState: (e, r) => (s((t) => {
97
- const n = new Map(t.shadowStateSubscribers), a = n.get(e) || /* @__PURE__ */ new Set();
98
- return a.add(r), n.set(e, a), { shadowStateSubscribers: n };
99
- }), () => {
100
- s((t) => {
101
- const n = new Map(t.shadowStateSubscribers), a = n.get(e);
102
- return a && a.delete(r), { shadowStateSubscribers: n };
103
- });
104
- }),
105
- setShadowMetadata: (e, r, t) => {
106
- let n = !1;
107
- if (s((a) => {
108
- const o = { ...a.shadowStateStore };
109
- if (!o[e]) return a;
110
- o[e] = JSON.parse(JSON.stringify(o[e]));
111
- let i = o[e];
112
- for (const d of r)
113
- i[d] || (i[d] = {}), i = i[d];
114
- const l = i.virtualizer?.itemHeight, c = t.virtualizer?.itemHeight;
115
- return c && l !== c && (n = !0, i.virtualizer || (i.virtualizer = {}), i.virtualizer.itemHeight = c), { shadowStateStore: o };
116
- }), n) {
117
- const a = S().shadowStateSubscribers.get(e);
118
- a && a.forEach((o) => o());
79
+ removeShadowArrayElement: (e, r) => {
80
+ const t = new Map(l().shadowStateStore), n = [e, ...r].join("."), s = r[r.length - 1], o = r.slice(0, -1), d = [e, ...o].join("."), i = t.get(d);
81
+ if (i && i.arrayKeys) {
82
+ const c = i.arrayKeys.filter(
83
+ (f) => f !== s
84
+ );
85
+ t.set(d, { ...i, arrayKeys: c });
119
86
  }
87
+ const S = n + ".";
88
+ for (const c of Array.from(t.keys()))
89
+ (c === n || c.startsWith(S)) && t.delete(c);
90
+ a({ shadowStateStore: t });
91
+ },
92
+ updateShadowAtPath: (e, r, t) => {
93
+ const n = [e, ...r].join("."), s = new Map(l().shadowStateStore), o = s.get(n) || { id: u() };
94
+ s.set(n, { ...o, lastUpdated: Date.now() }), a({ shadowStateStore: s });
120
95
  },
121
96
  selectedIndicesMap: /* @__PURE__ */ new Map(),
122
97
  // Add the new methods
123
98
  getSelectedIndex: (e, r) => {
124
- const t = S().selectedIndicesMap.get(e);
99
+ const t = l().selectedIndicesMap.get(e);
125
100
  if (t)
126
101
  return t.get(r);
127
102
  },
128
103
  setSelectedIndex: (e, r, t) => {
129
- s((n) => {
130
- const a = new Map(n.selectedIndicesMap);
131
- let o = a.get(e);
132
- return o || (o = /* @__PURE__ */ new Map(), a.set(e, o)), t === void 0 ? o.delete(r) : o.set(r, t), {
104
+ a((n) => {
105
+ const s = new Map(n.selectedIndicesMap);
106
+ let o = s.get(e);
107
+ return o || (o = /* @__PURE__ */ new Map(), s.set(e, o)), t === void 0 ? o.delete(r) : o.set(r, t), {
133
108
  ...n,
134
- selectedIndicesMap: a
109
+ selectedIndicesMap: s
135
110
  };
136
111
  });
137
112
  },
@@ -139,18 +114,18 @@ const w = u((s, S) => ({
139
114
  stateKey: e,
140
115
  path: r
141
116
  }) => {
142
- s((t) => {
143
- const n = new Map(t.selectedIndicesMap), a = n.get(e);
144
- if (!a) return t;
117
+ a((t) => {
118
+ const n = new Map(t.selectedIndicesMap), s = n.get(e);
119
+ if (!s) return t;
145
120
  const o = r.join(".");
146
- return a.delete(o), {
121
+ return s.delete(o), {
147
122
  ...t,
148
123
  selectedIndicesMap: n
149
124
  };
150
125
  });
151
126
  },
152
127
  clearSelectedIndexesForState: (e) => {
153
- s((r) => {
128
+ a((r) => {
154
129
  const t = new Map(r.selectedIndicesMap);
155
130
  return t.delete(e) ? (console.log(
156
131
  `Cleared selected indices map entry for stateKey: ${e}`
@@ -158,16 +133,16 @@ const w = u((s, S) => ({
158
133
  });
159
134
  },
160
135
  stateComponents: /* @__PURE__ */ new Map(),
161
- subscribe: (e) => S().subscribe(e),
136
+ subscribe: (e) => l().subscribe(e),
162
137
  reactiveDeps: {},
163
- setReactiveDeps: (e, r) => s((t) => ({
138
+ setReactiveDeps: (e, r) => a((t) => ({
164
139
  ...t,
165
140
  reactiveDeps: {
166
141
  ...t.reactiveDeps,
167
142
  [e]: r
168
143
  }
169
144
  })),
170
- deleteReactiveDeps: (e) => s((r) => {
145
+ deleteReactiveDeps: (e) => a((r) => {
171
146
  const { [e]: t, ...n } = r.reactiveDeps;
172
147
  return {
173
148
  ...r,
@@ -177,14 +152,14 @@ const w = u((s, S) => ({
177
152
  reRenderTriggerPrevValue: {},
178
153
  signalDomElements: /* @__PURE__ */ new Map(),
179
154
  addSignalElement: (e, r) => {
180
- const t = S().signalDomElements;
181
- t.has(e) || t.set(e, /* @__PURE__ */ new Set()), t.get(e).add(r), s({ signalDomElements: new Map(t) });
155
+ const t = l().signalDomElements;
156
+ t.has(e) || t.set(e, /* @__PURE__ */ new Set()), t.get(e).add(r), a({ signalDomElements: new Map(t) });
182
157
  },
183
158
  removeSignalElement: (e, r) => {
184
- const t = S().signalDomElements, n = t.get(e);
185
- n && n.forEach((a) => {
186
- a.instanceId === r && n.delete(a);
187
- }), s({ signalDomElements: new Map(t) });
159
+ const t = l().signalDomElements, n = t.get(e);
160
+ n && n.forEach((s) => {
161
+ s.instanceId === r && n.delete(s);
162
+ }), a({ signalDomElements: new Map(t) });
188
163
  },
189
164
  initialStateOptions: {},
190
165
  updaterState: {},
@@ -195,7 +170,7 @@ const w = u((s, S) => ({
195
170
  initialStateGlobal: {},
196
171
  iniitialCreatedState: {},
197
172
  updateInitialCreatedState: (e, r) => {
198
- s((t) => ({
173
+ a((t) => ({
199
174
  iniitialCreatedState: {
200
175
  ...t.iniitialCreatedState,
201
176
  [e]: r
@@ -208,7 +183,7 @@ const w = u((s, S) => ({
208
183
  serverSyncLog: {},
209
184
  serverSideOrNot: {},
210
185
  setServerSyncLog: (e, r) => {
211
- s((t) => ({
186
+ a((t) => ({
212
187
  serverSyncLog: {
213
188
  ...t.serverSyncLog,
214
189
  [e]: [...t.serverSyncLog[e] ?? [], r]
@@ -216,17 +191,17 @@ const w = u((s, S) => ({
216
191
  }));
217
192
  },
218
193
  setServerSideOrNot: (e, r) => {
219
- s((t) => ({
194
+ a((t) => ({
220
195
  serverSideOrNot: {
221
196
  ...t.serverSideOrNot,
222
197
  [e]: r
223
198
  }
224
199
  }));
225
200
  },
226
- getServerSideOrNot: (e) => S().serverSideOrNot[e],
227
- getThisLocalUpdate: (e) => S().stateLog[e],
201
+ getServerSideOrNot: (e) => l().serverSideOrNot[e],
202
+ getThisLocalUpdate: (e) => l().stateLog[e],
228
203
  setServerState: (e, r) => {
229
- s((t) => ({
204
+ a((t) => ({
230
205
  serverState: {
231
206
  ...t.serverState,
232
207
  [e]: r
@@ -234,18 +209,18 @@ const w = u((s, S) => ({
234
209
  }));
235
210
  },
236
211
  setStateLog: (e, r) => {
237
- s((t) => {
238
- const n = t.stateLog[e] ?? [], a = r(n);
212
+ a((t) => {
213
+ const n = t.stateLog[e] ?? [], s = r(n);
239
214
  return {
240
215
  stateLog: {
241
216
  ...t.stateLog,
242
- [e]: a
217
+ [e]: s
243
218
  }
244
219
  };
245
220
  });
246
221
  },
247
222
  setIsLoadingGlobal: (e, r) => {
248
- s((t) => ({
223
+ a((t) => ({
249
224
  isLoadingGlobal: {
250
225
  ...t.isLoadingGlobal,
251
226
  [e]: r
@@ -253,7 +228,7 @@ const w = u((s, S) => ({
253
228
  }));
254
229
  },
255
230
  setServerSyncActions: (e, r) => {
256
- s((t) => ({
231
+ a((t) => ({
257
232
  serverSyncActions: {
258
233
  ...t.serverSyncActions,
259
234
  [e]: r
@@ -261,84 +236,88 @@ const w = u((s, S) => ({
261
236
  }));
262
237
  },
263
238
  addValidationError: (e, r) => {
264
- console.log("addValidationError---"), s((t) => {
265
- const n = new Map(t.validationErrors), a = n.get(e) || [];
266
- return console.log("addValidationError", e, r, a), n.set(e, [...a, r]), { validationErrors: n };
239
+ console.log("addValidationError---"), a((t) => {
240
+ const n = new Map(t.validationErrors), s = n.get(e) || [];
241
+ return console.log("addValidationError", e, r, s), n.set(e, [...s, r]), { validationErrors: n };
267
242
  });
268
243
  },
269
244
  removeValidationError: (e) => {
270
- s((r) => {
245
+ a((r) => {
271
246
  const t = new Map(r.validationErrors);
272
247
  let n = !1;
273
- const a = e.split(".");
248
+ const s = e.split(".");
274
249
  return Array.from(t.keys()).forEach((o) => {
275
- const i = o.split(".");
276
- if (i.length >= a.length) {
277
- let l = !0;
278
- for (let c = 0; c < a.length; c++)
279
- if (i[c] !== a[c]) {
280
- l = !1;
250
+ const d = o.split(".");
251
+ if (d.length >= s.length) {
252
+ let i = !0;
253
+ for (let S = 0; S < s.length; S++)
254
+ if (d[S] !== s[S]) {
255
+ i = !1;
281
256
  break;
282
257
  }
283
- l && (n = !0, t.delete(o));
258
+ i && (n = !0, t.delete(o));
284
259
  }
285
260
  }), n ? { validationErrors: t } : r;
286
261
  });
287
262
  },
288
263
  getValidationErrors: (e) => {
289
- const r = [], t = S().validationErrors, n = e.split("."), a = (o, i) => o === "[*]" ? !0 : Array.isArray(o) ? o.includes(parseInt(i)) : o === i;
264
+ const r = [], t = l().validationErrors, n = e.split("."), s = (o, d) => o === "[*]" ? !0 : Array.isArray(o) ? o.includes(parseInt(d)) : o === d;
290
265
  return Array.from(t.keys()).forEach((o) => {
291
- const i = o.split(".");
292
- if (i.length >= n.length) {
293
- let l = !0;
294
- for (let c = 0; c < n.length; c++) {
295
- const d = n[c], f = i[c];
296
- if (d === "[*]" || Array.isArray(d)) {
266
+ const d = o.split(".");
267
+ if (d.length >= n.length) {
268
+ let i = !0;
269
+ for (let S = 0; S < n.length; S++) {
270
+ const c = n[S], f = d[S];
271
+ if (c === "[*]" || Array.isArray(c)) {
297
272
  const p = parseInt(f);
298
273
  if (isNaN(p)) {
299
- l = !1;
274
+ i = !1;
300
275
  break;
301
276
  }
302
- if (!a(d, f)) {
303
- l = !1;
277
+ if (!s(c, f)) {
278
+ i = !1;
304
279
  break;
305
280
  }
306
- } else if (d !== f) {
307
- l = !1;
281
+ } else if (c !== f) {
282
+ i = !1;
308
283
  break;
309
284
  }
310
285
  }
311
- if (l) {
312
- const c = t.get(o);
313
- c && r.push(...c);
286
+ if (i) {
287
+ const S = t.get(o);
288
+ S && r.push(...S);
314
289
  }
315
290
  }
316
291
  }), r;
317
292
  },
318
- getInitialOptions: (e) => S().initialStateOptions[e],
293
+ getInitialOptions: (e) => l().initialStateOptions[e],
319
294
  getNestedState: (e, r) => {
320
- const t = S().cogsStateStore[e], n = (a, o) => {
321
- if (o.length === 0) return a;
322
- const i = o[0], l = o.slice(1);
323
- if (i === "[*]") {
324
- if (!Array.isArray(a)) {
295
+ const t = l().cogsStateStore[e], n = (s, o) => {
296
+ if (o.length === 0 || s === void 0)
297
+ return s;
298
+ const d = o[0], i = o.slice(1);
299
+ if (Array.isArray(s) && typeof d == "string" && d.startsWith("id:")) {
300
+ const c = d.split(":")[1], f = s.find(
301
+ (p) => p && String(p.id) === c
302
+ );
303
+ return n(f, i);
304
+ }
305
+ if (d === "[*]") {
306
+ if (!Array.isArray(s)) {
325
307
  console.warn("Asterisk notation used on non-array value");
326
308
  return;
327
309
  }
328
- if (l.length === 0) return a;
329
- const d = a.map(
330
- (f) => n(f, l)
331
- );
332
- return Array.isArray(d[0]) ? d.flat() : d;
310
+ if (i.length === 0) return s;
311
+ const c = s.map((f) => n(f, i));
312
+ return Array.isArray(c[0]) ? c.flat() : c;
333
313
  }
334
- const c = a[i];
335
- if (c !== void 0)
336
- return n(c, l);
314
+ const S = s[d];
315
+ return n(S, i);
337
316
  };
338
317
  return n(t, r);
339
318
  },
340
319
  setInitialStateOptions: (e, r) => {
341
- s((t) => ({
320
+ a((t) => ({
342
321
  initialStateOptions: {
343
322
  ...t.initialStateOptions,
344
323
  [e]: r
@@ -346,21 +325,21 @@ const w = u((s, S) => ({
346
325
  }));
347
326
  },
348
327
  updateInitialStateGlobal: (e, r) => {
349
- s((t) => ({
328
+ a((t) => ({
350
329
  initialStateGlobal: {
351
330
  ...t.initialStateGlobal,
352
331
  [e]: r
353
332
  }
354
333
  }));
355
334
  },
356
- getUpdaterState: (e) => S().updaterState[e],
335
+ getUpdaterState: (e) => l().updaterState[e],
357
336
  setUpdaterState: (e, r) => {
358
- const t = S().updaterState;
359
- !e || !r || s({ updaterState: { ...t ?? {}, [e]: r } });
337
+ const t = l().updaterState;
338
+ !e || !r || a({ updaterState: { ...t ?? {}, [e]: r } });
360
339
  },
361
- getKeyState: (e) => S().cogsStateStore[e],
340
+ getKeyState: (e) => l().cogsStateStore[e],
362
341
  setState: (e, r) => {
363
- s((t) => ({
342
+ a((t) => ({
364
343
  cogsStateStore: {
365
344
  ...t.cogsStateStore,
366
345
  [e]: typeof r == "function" ? r(t.cogsStateStore[e]) : r
@@ -368,7 +347,7 @@ const w = u((s, S) => ({
368
347
  }));
369
348
  },
370
349
  setInitialStates: (e) => {
371
- s((r) => ({
350
+ a((r) => ({
372
351
  cogsStateStore: {
373
352
  ...r.cogsStateStore,
374
353
  ...e
@@ -376,7 +355,7 @@ const w = u((s, S) => ({
376
355
  }));
377
356
  },
378
357
  setCreatedState: (e) => {
379
- s((r) => ({
358
+ a((r) => ({
380
359
  iniitialCreatedState: {
381
360
  ...r.cogsStateStore,
382
361
  ...e
@@ -384,14 +363,14 @@ const w = u((s, S) => ({
384
363
  }));
385
364
  },
386
365
  syncInfoStore: /* @__PURE__ */ new Map(),
387
- setSyncInfo: (e, r) => s((t) => {
366
+ setSyncInfo: (e, r) => a((t) => {
388
367
  const n = new Map(t.syncInfoStore);
389
368
  return n.set(e, r), { ...t, syncInfoStore: n };
390
369
  }),
391
- getSyncInfo: (e) => S().syncInfoStore.get(e) || null
370
+ getSyncInfo: (e) => l().syncInfoStore.get(e) || null
392
371
  }));
393
372
  export {
394
- w as formRefStore,
395
- h as getGlobalStore
373
+ b as formRefStore,
374
+ I as getGlobalStore
396
375
  };
397
376
  //# sourceMappingURL=store.js.map