atomaric 0.0.69 → 0.0.71

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/build/atomaric.js CHANGED
@@ -1,10 +1,10 @@
1
1
  let k = () => {
2
2
  throw "call configureAtomaric() before all!";
3
3
  };
4
- const L = {}, Ae = (t) => {
5
- L.useSyncExternalStore = k = t.useSyncExternalStore, L.keyPathSeparator = t.keyPathSeparator, L.securifyKeyLevel = t.securifyKeyLevel, L.securifyValueLevel = t.securifyValueLevel;
6
- }, ne = (t) => k(t.subscribe, t.get), oe = (t) => t.set, Se = (t) => t.setDeferred, xe = (t) => t.get, be = (t) => t.do, De = (t) => [ne(t), oe(t)], $e = (t, e) => new de(t, e);
7
- class E {
4
+ const T = {}, Se = (t) => {
5
+ T.useSyncExternalStore = k = t.useSyncExternalStore, T.keyPathSeparator = t.keyPathSeparator, T.securifyKeyLevel = t.securifyKeyLevel, T.securifyValueLevel = t.securifyValueLevel;
6
+ }, ne = (t) => k(t.subscribe, t.get), oe = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), oe(t)], Te = (t, e) => new pe(t, e);
7
+ class _ {
8
8
  constructor(e) {
9
9
  if (e)
10
10
  return new Proxy(this, {
@@ -14,33 +14,33 @@ class E {
14
14
  }
15
15
  const ce = (t, e) => {
16
16
  const n = (s, o, i) => new Proxy(s, {
17
- get: (u, f, y) => {
18
- const d = i === 0 && e.get != null ? e.get(u, f, y) : u[f];
17
+ get: (l, h, p) => {
18
+ const d = i === 0 && e.get != null ? e.get(l, h, p) : l[h];
19
19
  return typeof d == "object" && d !== null ? n(
20
20
  Array.isArray(d) ? d.slice(0) : { ...d },
21
- o.concat(Array.isArray(u) ? +f : f),
21
+ o.concat(Array.isArray(l) ? +h : h),
22
22
  i + 1
23
23
  ) : d;
24
24
  },
25
- set: (u, f, y) => (e.onSet(u, o, f, y, u[f]) && (u[f] = y), !0)
25
+ set: (l, h, p) => (e.onSet(l, o, h, p, l[h]) && (l[h] = p), !0)
26
26
  });
27
27
  return n(t, [], 0);
28
28
  };
29
- class O extends E {
29
+ class O extends _ {
30
30
  constructor() {
31
31
  super(...arguments), this.updateValue = (e, n) => {
32
32
  const s = Array.isArray(e) ? e.slice(0) : { ...e };
33
33
  let o = !1;
34
34
  const i = ce(e, {
35
- onSet: (u, f, y, d, g) => {
35
+ onSet: (l, h, p, d, g) => {
36
36
  if (d === g) return !0;
37
37
  let A = s;
38
38
  o = !0;
39
- for (const l of f) {
40
- const h = A[l];
41
- A = A[l] = Array.isArray(h) ? h.slice(0) : { ...h };
39
+ for (const f of h) {
40
+ const m = A[f];
41
+ A = A[f] = Array.isArray(m) ? m.slice(0) : { ...m };
42
42
  }
43
- return A[y] = d, !0;
43
+ return A[p] = d, !0;
44
44
  }
45
45
  });
46
46
  return n(i), o ? s : e;
@@ -66,27 +66,43 @@ class ie extends O {
66
66
  const i = this.atom.get().slice(0);
67
67
  i.splice(o, 1), this.atom.set(i);
68
68
  }, this.toggle = (s, o) => {
69
- const i = this.atom.get().slice(), u = i.indexOf(s);
70
- u < 0 ? o ? i.unshift(s) : i.push(s) : i.splice(u, 1), this.atom.set(i);
69
+ const i = this.atom.get().slice(), l = i.indexOf(s);
70
+ l < 0 ? o ? i.unshift(s) : i.push(s) : i.splice(l, 1), this.atom.set(i);
71
71
  };
72
72
  }
73
73
  }
74
74
  const ae = (t) => t;
75
- class ue extends E {
75
+ class ue extends _ {
76
76
  constructor(e, n) {
77
77
  super(n), this.atom = e, this.toggle = () => {
78
78
  this.atom.set(!this.atom.get());
79
79
  };
80
80
  }
81
81
  }
82
- class le extends E {
82
+ class le extends _ {
83
+ constructor(e, n) {
84
+ super(n), this.atom = e, this.setValue = (s, o) => {
85
+ const i = new Map(this.atom.get());
86
+ i.set(s, o), this.atom.set(i);
87
+ }, this.delete = (s) => {
88
+ const o = new Map(this.atom.get());
89
+ o.delete(s), this.atom.set(o);
90
+ }, this.toggle = (s, o) => {
91
+ const i = new Map(this.atom.get());
92
+ i.has(s) ? i.delete(s) : i.set(s, o), this.atom.set(i);
93
+ }, this.clear = () => {
94
+ this.atom.set(/* @__PURE__ */ new Map());
95
+ }, this.atom = e;
96
+ }
97
+ }
98
+ class fe extends _ {
83
99
  constructor(e, n) {
84
100
  super(n), this.atom = e, this.increment = (s) => {
85
101
  this.atom.set(+this.atom.get() + (s ?? 1));
86
102
  };
87
103
  }
88
104
  }
89
- class fe extends O {
105
+ class he extends O {
90
106
  constructor(e, n) {
91
107
  super(n), this.atom = e, this.setPartial = (s) => this.atom.set((o) => ({
92
108
  ...o,
@@ -94,36 +110,36 @@ class fe extends O {
94
110
  })), this.update = (s) => {
95
111
  const o = this.atom.get(), i = this.updateValue(o, s);
96
112
  i !== o && this.atom.set(i);
97
- }, this.setDeepPartial = (s, o, i, u = L.keyPathSeparator || ".") => {
98
- if (!u) return;
99
- if (s.includes(u)) {
100
- let d = s.split(u);
113
+ }, this.setDeepPartial = (s, o, i, l = T.keyPathSeparator || ".") => {
114
+ if (!l) return;
115
+ if (s.includes(l)) {
116
+ let d = s.split(l);
101
117
  const g = d[d.length - 1];
102
118
  d = d.slice(0, -1);
103
119
  const A = { ...this.atom.get() };
104
- let l = A, h = i;
105
- for (const p of d) {
106
- h = h == null ? void 0 : h[Array.isArray(h) ? "0" : p];
107
- const x = l[Z(l, p)] ?? (Array.isArray(h) ? [] : {});
108
- if (x == null || typeof x != "object") {
120
+ let f = A, m = i;
121
+ for (const w of d) {
122
+ m = m == null ? void 0 : m[Array.isArray(m) ? "0" : w];
123
+ const D = f[Z(f, w)] ?? (Array.isArray(m) ? [] : {});
124
+ if (D == null || typeof D != "object") {
109
125
  if (i == null) throw "Incorrect path for setDeepPartial";
110
126
  const S = typeof o == "function" ? o(void 0) : o;
111
127
  this.atom.get()[s] !== S && this.setPartial({ [s]: S });
112
128
  return;
113
129
  }
114
- l = l[Z(l, p)] = Array.isArray(x) ? [...x] : { ...x };
130
+ f = f[Z(f, w)] = Array.isArray(D) ? [...D] : { ...D };
115
131
  }
116
- const z = l[g];
117
- l[g] = typeof o == "function" ? o(l[g]) : o, z !== l[g] && this.atom.set(A);
132
+ const E = f[g];
133
+ f[g] = typeof o == "function" ? o(f[g]) : o, E !== f[g] && this.atom.set(A);
118
134
  return;
119
135
  }
120
- const f = this.atom.get()[s], y = typeof o == "function" ? o(f) : o;
121
- y !== f && this.setPartial({ [s]: y });
136
+ const h = this.atom.get()[s], p = typeof o == "function" ? o(h) : o;
137
+ p !== h && this.setPartial({ [s]: p });
122
138
  };
123
139
  }
124
140
  }
125
141
  const Z = (t, e) => Array.isArray(t) ? `${+e}` : e;
126
- class he extends E {
142
+ class me extends _ {
127
143
  constructor(e, n) {
128
144
  super(n), this.atom = e, this.add = (s) => {
129
145
  this.atom.set(new Set(this.atom.get()).add(s));
@@ -138,96 +154,102 @@ class he extends E {
138
154
  }, this.atom = e;
139
155
  }
140
156
  }
141
- const me = (t, e, n) => {
157
+ const de = (t, e, n) => {
142
158
  const s = typeof n == "object" && n != null && "do" in n ? n.do(
143
- (i, u) => e.set(i, u),
159
+ (i, l) => e.set(i, l),
144
160
  () => e.get(),
145
161
  e,
146
- (i, u, f) => e.setDeferred(i, u, f)
162
+ (i, l, h) => e.setDeferred(i, l, h)
147
163
  ) : null, o = {};
148
- return s && Object.keys(s).forEach((i) => Object.defineProperty(o, i, { get: () => s[i] })), typeof t == "number" ? new le(e, s) : typeof t == "boolean" ? new ue(e, s) : Array.isArray(t) ? new ie(e, s) : t instanceof Set ? new he(e, s) : t instanceof Object ? new fe(e, s) : new E(s);
164
+ return s && Object.keys(s).forEach((i) => Object.defineProperty(o, i, { get: () => s[i] })), typeof t == "number" ? new fe(e, s) : typeof t == "boolean" ? new ue(e, s) : Array.isArray(t) ? new ie(e, s) : t instanceof Set ? new me(e, s) : t instanceof Map ? new le(e, s) : t instanceof Object ? new he(e, s) : new _(s);
149
165
  };
150
- class de {
166
+ class pe {
151
167
  constructor(e, n) {
152
168
  e = typeof e == "function" ? e() : e;
153
- const s = (r) => d = r, o = () => d, i = /* @__PURE__ */ new Set(), u = (r) => r(l());
154
- let f = !0, y = !1, d = e, g, A = () => {
155
- }, l = () => o(), h = null, z = () => {
156
- const r = me(e, p, n);
157
- return z = () => r, r;
169
+ const s = (r) => d = r, o = () => d, i = /* @__PURE__ */ new Set(), l = (r) => r(f());
170
+ let h = !0, p = !1, d = e, g, A = () => {
171
+ }, f = () => o(), m = null, E = () => {
172
+ const r = de(e, w, n);
173
+ return E = () => r, r;
158
174
  };
159
- const p = new Proxy(this, {
160
- get: (r, m) => m === "do" ? z() : r[m],
161
- set: ye
162
- }), x = () => {
163
- f = !0, y !== !0 && A(l()), y = !1;
175
+ const w = new Proxy(this, {
176
+ get: (r, u) => u === "do" ? E() : r[u],
177
+ set: ge
178
+ }), D = () => {
179
+ h = !0, p !== !0 && A(f()), p = !1;
164
180
  try {
165
- W.postMessage({ key: a, value: o() });
181
+ v.postMessage({ key: a, value: o() });
166
182
  } catch {
167
183
  }
168
- }, S = (r, m) => {
169
- const D = typeof r == "function" ? r(l()) : r;
170
- D !== l() && (s(D), y = m, f && (f = !1, i.forEach(u), queueMicrotask(x)));
184
+ }, S = (r, u) => {
185
+ const x = typeof r == "function" ? r(f()) : r;
186
+ x !== f() && (s(x), p = u, h && (h = !1, i.forEach(l), queueMicrotask(D)));
171
187
  };
172
- this.set = (r, m) => S(r, m), this.get = () => l(), this.initialValue = e, this.isInitialValue = () => e === o(), this.subscribe = (r) => (i.add(r), () => {
188
+ this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === o(), this.subscribe = (r) => (i.add(r), () => {
173
189
  i.delete(r);
174
190
  }), this.reset = () => {
175
191
  S(e, !0);
176
192
  };
177
- const re = (r, m) => {
178
- S(r, m), g = void 0;
193
+ const re = (r, u) => {
194
+ S(r, u), g = void 0;
179
195
  };
180
- if (this.setDeferred = (r, m = 500, D, $ = !0) => {
181
- $ && g === void 0 && S(r, D), clearTimeout(g), g = setTimeout(re, m, r, D);
182
- }, n == null) return p;
183
- let w = null, j = null, M = !0, v = !0, B = !1, b = 0, T = 0, Q = -1, V = e instanceof Set ? (r) => new Set(r) : (r) => r, C = e instanceof Set ? (r) => {
196
+ if (this.setDeferred = (r, u = 500, x, b = !0) => {
197
+ b && g === void 0 && S(r, x), clearTimeout(g), g = setTimeout(re, u, r, x);
198
+ }, n == null) return w;
199
+ let y = null, j = null, q = !0, B = !0, F = !1, $ = 0, L = 0, Q = -1, M = e instanceof Set || e instanceof Map ? (r) => new Set(r) : (r) => r, C = e instanceof Set ? (r) => {
184
200
  if (r instanceof Set) return Array.from(r);
185
201
  throw console.error(r), "The value is not Set instance";
202
+ } : e instanceof Map ? (r) => {
203
+ if (r instanceof Map) {
204
+ const u = [];
205
+ return r.forEach((x, b) => u.push([b, x])), u;
206
+ }
207
+ throw console.error(r), "The value is not Set instance";
186
208
  } : (r) => r;
187
209
  if (typeof n == "string")
188
- w = n;
210
+ y = n;
189
211
  else if ("storeKey" in n)
190
- M = n.warnOnDuplicateStoreKey ?? M, v = n.listenStorageChanges ?? v, w = n.storeKey, V = n.unzipValue ?? V, C = n.zipValue ?? C, B = n.unchangable ?? B, b = n.securifyKeyLevel ?? L.securifyKeyLevel ?? b, T = n.securifyValueLevel ?? L.securifyValueLevel ?? T, j = n.exp ?? j;
191
- else return p;
192
- const F = b ? I(w, b) : w, a = `${T ? H : P}${F}`;
193
- if (b) {
194
- const r = `${P}${w}`;
212
+ q = n.warnOnDuplicateStoreKey ?? q, B = n.listenStorageChanges ?? B, y = n.storeKey, M = n.unzipValue ?? M, C = n.zipValue ?? C, F = n.unchangable ?? F, $ = n.securifyKeyLevel ?? T.securifyKeyLevel ?? $, L = n.securifyValueLevel ?? T.securifyValueLevel ?? L, j = n.exp ?? j;
213
+ else return w;
214
+ const N = $ ? I(y, $) : y, a = `${L ? H : P}${N}`;
215
+ if ($) {
216
+ const r = `${P}${y}`;
195
217
  r in c && (c[a] = c[r], delete c[r]);
196
218
  } else {
197
- const r = `${P}${I(w, b)}`;
219
+ const r = `${P}${I(y, $)}`;
198
220
  r in c && (c[a] = c[r], delete c[r]);
199
221
  }
200
- const N = j === null || !(j(p, a in c) instanceof Date) ? (r) => I([C(r)], 0) : (r) => (h ?? (h = {}), h.exp = j(p, a in c).getTime(), h.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(Q), clearTimeout(ee[a]), Q = setTimeout(() => this.reset(), h.exp - Date.now())), h.exp = Math.trunc(h.exp / 1e3), I([C(r), h], 0));
201
- if (T) {
202
- const r = `${P}${F}`, m = C, D = V;
203
- if (C = ($) => {
222
+ const W = j === null || !(j(w, a in c) instanceof Date) ? (r) => I([C(r)], 0) : (r) => (m ?? (m = {}), m.exp = j(w, a in c).getTime(), m.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(Q), clearTimeout(ee[a]), Q = setTimeout(() => this.reset(), m.exp - Date.now())), m.exp = Math.trunc(m.exp / 1e3), I([C(r), m], 0));
223
+ if (L) {
224
+ const r = `${P}${N}`, u = C, x = M;
225
+ if (C = (b) => {
204
226
  try {
205
- return I([m($)], T);
227
+ return I([u(b)], L);
206
228
  } catch {
207
229
  return delete c[a], "";
208
230
  }
209
231
  }, r in c) {
210
- const $ = `${P}${I(w, b)}`;
232
+ const b = `${P}${I(y, $)}`;
211
233
  try {
212
- c[$] = N(V(R(c[r], 0)[0])), delete c[r];
234
+ c[b] = W(M(z(c[r], 0)[0])), delete c[r];
213
235
  } catch {
214
236
  }
215
237
  }
216
- V = ($) => {
238
+ M = (b) => {
217
239
  try {
218
- return D(R($, T)[0]);
240
+ return x(z(b, L)[0]);
219
241
  } catch {
220
242
  return delete c[a], "";
221
243
  }
222
244
  };
223
- } else delete c[`${H}${F}`];
245
+ } else delete c[`${H}${N}`];
224
246
  const K = (r) => {
225
- const m = R(r, 0);
226
- return h = m[1], V(m[0]);
247
+ const u = z(r, 0);
248
+ return m = u[1], M(u[0]);
227
249
  };
228
250
  let X = !0;
229
- if (J[a] = p, c[`atom/${w}`] && (c[a] || (c[a] = `[${c[`atom/${w}`]}]`), delete c[`atom/${w}`]), l = () => {
230
- if (l = o, X) {
251
+ if (J[a] = w, c[`atom/${y}`] && (c[a] || (c[a] = `[${c[`atom/${y}`]}]`), delete c[`atom/${y}`]), f = () => {
252
+ if (f = o, X) {
231
253
  X = !1;
232
254
  try {
233
255
  s(a in c ? K(c[a]) : e);
@@ -241,17 +263,17 @@ class de {
241
263
  this.reset();
242
264
  return;
243
265
  }
244
- c[a] = N(r);
266
+ c[a] = W(r);
245
267
  }, this.reset = () => {
246
268
  delete c[a], S(e, !0);
247
- }, M && _[a] !== void 0 && console.warn("Duplicate Atom key", w), v)
248
- if (B) {
249
- let r = !1, m;
250
- q[a] = this, _[a] = () => {
251
- clearTimeout(m), m = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = N(o()));
269
+ }, q && V[a] !== void 0 && console.warn("Duplicate Atom key", y), B)
270
+ if (F) {
271
+ let r = !1, u;
272
+ U[a] = this, V[a] = () => {
273
+ clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = W(o()));
252
274
  };
253
275
  } else
254
- _[a] = (r) => {
276
+ V[a] = (r) => {
255
277
  if (r.newValue === null) {
256
278
  this.reset();
257
279
  return;
@@ -262,32 +284,32 @@ class de {
262
284
  console.warn("Invalid json value", r.newValue);
263
285
  }
264
286
  };
265
- return p;
287
+ return w;
266
288
  }
267
289
  }
268
- let W;
290
+ let v;
269
291
  try {
270
- W = new BroadcastChannel("updateHere"), W.addEventListener("message", (t) => {
292
+ v = new BroadcastChannel("updateHere"), v.addEventListener("message", (t) => {
271
293
  var e;
272
- (e = q[t.data.key]) == null || e.set(t.data.value, !0);
294
+ (e = U[t.data.key]) == null || e.set(t.data.value, !0);
273
295
  });
274
296
  } catch {
275
297
  }
276
- const c = localStorage, _ = {}, q = {}, ye = (t, e) => {
298
+ const c = localStorage, V = {}, U = {}, ge = (t, e) => {
277
299
  throw `${e} is readonly property`;
278
300
  };
279
301
  window.addEventListener("storage", (t) => {
280
302
  var e;
281
- t.key === null || t.newValue === t.oldValue || (e = _[t.key]) == null || e.call(_, t);
303
+ t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
282
304
  });
283
- const ge = c.setItem.bind(c), pe = c.removeItem.bind(c);
305
+ const we = c.setItem.bind(c), ye = c.removeItem.bind(c);
284
306
  c.setItem = (t, e) => {
285
- q[t] === void 0 && ge.call(c, t, e);
307
+ U[t] === void 0 && we.call(c, t, e);
286
308
  };
287
309
  c.removeItem = (t) => {
288
- q[t] === void 0 && pe.call(c, t);
310
+ U[t] === void 0 && ye.call(c, t);
289
311
  };
290
- const we = /"exp":\s*(\d+)/, P = "atom\\", H = "atom`s\\", J = {}, ee = {}, te = /[a-z]/gi, U = 5, G = {};
312
+ const Ae = /"exp":\s*(\d+)/, P = "atom\\", H = "atom`s\\", J = {}, ee = {}, te = /[a-z]/gi, R = 5, G = {};
291
313
  for (let t = 54; t < 80; t++) {
292
314
  if (t === 68 || t === 72 || t === 55) continue;
293
315
  const e = String.fromCharCode(t + 43).toUpperCase(), n = String.fromCharCode(t + 43).toLowerCase();
@@ -299,7 +321,7 @@ const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
299
321
  (e) => btoa(encodeURI(t[0](e))),
300
322
  (e) => {
301
323
  const n = t[1](e);
302
- return `${n.slice(0, U)}${n.slice(U).replace(te, se)}`;
324
+ return `${n.slice(0, R)}${n.slice(R).replace(te, se)}`;
303
325
  },
304
326
  (e) => btoa(t[2](e))
305
327
  ];
@@ -311,12 +333,12 @@ const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
311
333
  return t[0](e);
312
334
  }
313
335
  };
314
- })(), R = /* @__PURE__ */ (() => {
336
+ })(), z = /* @__PURE__ */ (() => {
315
337
  const t = [
316
338
  (e) => JSON.parse(e),
317
339
  (e) => t[0](decodeURI(atob(e))),
318
340
  (e) => t[1](
319
- `${e.slice(0, U)}${e.slice(U).replace(te, se)}`
341
+ `${e.slice(0, R)}${e.slice(R).replace(te, se)}`
320
342
  ),
321
343
  (e) => t[2](atob(e))
322
344
  ];
@@ -333,28 +355,29 @@ setTimeout(() => {
333
355
  Object.keys(c).forEach((t) => {
334
356
  var s;
335
357
  if (typeof c[t] != "string" || !t.startsWith(P) && !t.startsWith(H)) return;
336
- const e = +((s = c[t].match(we)) == null ? void 0 : s[1]);
358
+ const e = +((s = c[t].match(Ae)) == null ? void 0 : s[1]);
337
359
  if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
338
- const n = R(c[t], 0);
360
+ const n = z(c[t], 0);
339
361
  !Array.isArray(n) || n[1] == null || !("exp" in n[1]) || n[1].exp !== e || (ee[t] = setTimeout(() => {
340
362
  J[t] ? J[t].reset() : delete c[t];
341
363
  }, e * 1e3 - Date.now()));
342
364
  });
343
365
  }, 1e3);
344
366
  export {
345
- de as Atom,
367
+ pe as Atom,
346
368
  ie as AtomArrayDoActions,
347
369
  ue as AtomBooleanDoActions,
348
- le as AtomNumberDoActions,
349
- fe as AtomObjectDoActions,
350
- he as AtomSetDoActions,
351
- $e as atom,
352
- Ae as configureAtomaric,
353
- L as configuredOptions,
354
- De as useAtom,
355
- be as useAtomDo,
356
- xe as useAtomGet,
370
+ le as AtomMapDoActions,
371
+ fe as AtomNumberDoActions,
372
+ he as AtomObjectDoActions,
373
+ me as AtomSetDoActions,
374
+ Te as atom,
375
+ Se as configureAtomaric,
376
+ T as configuredOptions,
377
+ $e as useAtom,
378
+ De as useAtomDo,
379
+ be as useAtomGet,
357
380
  oe as useAtomSet,
358
- Se as useAtomSetDeferred,
381
+ xe as useAtomSetDeferred,
359
382
  ne as useAtomValue
360
383
  };
@@ -1 +1 @@
1
- (function(l,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(l=typeof globalThis<"u"?globalThis:l||self,j(l.atomaric={}))})(this,function(l){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const D={},he=t=>{D.useSyncExternalStore=j=t.useSyncExternalStore,D.keyPathSeparator=t.keyPathSeparator,D.securifyKeyLevel=t.securifyKeyLevel,D.securifyValueLevel=t.securifyValueLevel},X=t=>j(t.subscribe,t.get),Y=t=>t.set,me=t=>t.setDeferred,de=t=>t.get,Ae=t=>t.do,ye=t=>[X(t),Y(t)],ge=(t,e)=>new ne(t,e);class z{constructor(e){if(e)return new Proxy(this,{get:(n,s)=>s in this?n[s]:e[s]})}}const we=(t,e)=>{const n=(s,o,c)=>new Proxy(s,{get:(a,h,y)=>{const A=c===0&&e.get!=null?e.get(a,h,y):a[h];return typeof A=="object"&&A!==null?n(Array.isArray(A)?A.slice(0):{...A},o.concat(Array.isArray(a)?+h:h),c+1):A},set:(a,h,y)=>(e.onSet(a,o,h,y,a[h])&&(a[h]=y),!0)});return n(t,[],0)};class Z extends z{constructor(){super(...arguments),this.updateValue=(e,n)=>{const s=Array.isArray(e)?e.slice(0):{...e};let o=!1;const c=we(e,{onSet:(a,h,y,A,g)=>{if(A===g)return!0;let S=s;o=!0;for(const f of h){const m=S[f];S=S[f]=Array.isArray(m)?m.slice(0):{...m}}return S[y]=A,!0}});return n(c),o?s:e}}}class k extends Z{constructor(e,n){super(n),this.atom=e,this.push=(...s)=>{this.atom.set(this.atom.get().concat(s))},this.unshift=(...s)=>{this.atom.set(s.concat(this.atom.get()))},this.update=s=>{const o=this.atom.get(),c=this.updateValue(o,s);c!==o&&this.atom.set(c)},this.filter=s=>{this.atom.set(this.atom.get().filter(s??pe))},this.add=s=>{this.atom.get().includes(s)||this.atom.set(this.atom.get().concat([s]))},this.remove=s=>{const o=this.atom.get().indexOf(s);if(o<0)return;const c=this.atom.get().slice(0);c.splice(o,1),this.atom.set(c)},this.toggle=(s,o)=>{const c=this.atom.get().slice(),a=c.indexOf(s);a<0?o?c.unshift(s):c.push(s):c.splice(a,1),this.atom.set(c)}}}const pe=t=>t;class O extends z{constructor(e,n){super(n),this.atom=e,this.toggle=()=>{this.atom.set(!this.atom.get())}}}class ee extends z{constructor(e,n){super(n),this.atom=e,this.increment=s=>{this.atom.set(+this.atom.get()+(s??1))}}}class te extends Z{constructor(e,n){super(n),this.atom=e,this.setPartial=s=>this.atom.set(o=>({...o,...typeof s=="function"?s(this.atom.get()):s})),this.update=s=>{const o=this.atom.get(),c=this.updateValue(o,s);c!==o&&this.atom.set(c)},this.setDeepPartial=(s,o,c,a=D.keyPathSeparator||".")=>{if(!a)return;if(s.includes(a)){let A=s.split(a);const g=A[A.length-1];A=A.slice(0,-1);const S={...this.atom.get()};let f=S,m=c;for(const w of A){m=m==null?void 0:m[Array.isArray(m)?"0":w];const x=f[se(f,w)]??(Array.isArray(m)?[]:{});if(x==null||typeof x!="object"){if(c==null)throw"Incorrect path for setDeepPartial";const b=typeof o=="function"?o(void 0):o;this.atom.get()[s]!==b&&this.setPartial({[s]:b});return}f=f[se(f,w)]=Array.isArray(x)?[...x]:{...x}}const v=f[g];f[g]=typeof o=="function"?o(f[g]):o,v!==f[g]&&this.atom.set(S);return}const h=this.atom.get()[s],y=typeof o=="function"?o(h):o;y!==h&&this.setPartial({[s]:y})}}}const se=(t,e)=>Array.isArray(t)?`${+e}`:e;class re extends z{constructor(e,n){super(n),this.atom=e,this.add=s=>{this.atom.set(new Set(this.atom.get()).add(s))},this.delete=s=>{const o=new Set(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=s=>{const o=new Set(this.atom.get());o.has(s)?o.delete(s):o.add(s),this.atom.set(o)},this.clear=()=>{this.atom.set(new Set)},this.atom=e}}const Se=(t,e,n)=>{const s=typeof n=="object"&&n!=null&&"do"in n?n.do((c,a)=>e.set(c,a),()=>e.get(),e,(c,a,h)=>e.setDeferred(c,a,h)):null,o={};return s&&Object.keys(s).forEach(c=>Object.defineProperty(o,c,{get:()=>s[c]})),typeof t=="number"?new ee(e,s):typeof t=="boolean"?new O(e,s):Array.isArray(t)?new k(e,s):t instanceof Set?new re(e,s):t instanceof Object?new te(e,s):new z(s)};class ne{constructor(e,n){e=typeof e=="function"?e():e;const s=r=>A=r,o=()=>A,c=new Set,a=r=>r(f());let h=!0,y=!1,A=e,g,S=()=>{},f=()=>o(),m=null,v=()=>{const r=Se(e,w,n);return v=()=>r,r};const w=new Proxy(this,{get:(r,d)=>d==="do"?v():r[d],set:be}),x=()=>{h=!0,y!==!0&&S(f()),y=!1;try{B.postMessage({key:u,value:o()})}catch{}},b=(r,d)=>{const T=typeof r=="function"?r(f()):r;T!==f()&&(s(T),y=d,h&&(h=!1,c.forEach(a),queueMicrotask(x)))};this.set=(r,d)=>b(r,d),this.get=()=>f(),this.initialValue=e,this.isInitialValue=()=>e===o(),this.subscribe=r=>(c.add(r),()=>{c.delete(r)}),this.reset=()=>{b(e,!0)};const Te=(r,d)=>{b(r,d),g=void 0};if(this.setDeferred=(r,d=500,T,V=!0)=>{V&&g===void 0&&b(r,T),clearTimeout(g),g=setTimeout(Te,d,r,T)},n==null)return w;let p=null,R=null,G=!0,H=!0,J=!1,$=0,I=0,ue=-1,_=e instanceof Set?r=>new Set(r):r=>r,E=e instanceof Set?r=>{if(r instanceof Set)return Array.from(r);throw console.error(r),"The value is not Set instance"}:r=>r;if(typeof n=="string")p=n;else if("storeKey"in n)G=n.warnOnDuplicateStoreKey??G,H=n.listenStorageChanges??H,p=n.storeKey,_=n.unzipValue??_,E=n.zipValue??E,J=n.unchangable??J,$=n.securifyKeyLevel??D.securifyKeyLevel??$,I=n.securifyValueLevel??D.securifyValueLevel??I,R=n.exp??R;else return w;const Q=$?C(p,$):p,u=`${I?N:P}${Q}`;if($){const r=`${P}${p}`;r in i&&(i[u]=i[r],delete i[r])}else{const r=`${P}${C(p,$)}`;r in i&&(i[u]=i[r],delete i[r])}const K=R===null||!(R(w,u in i)instanceof Date)?r=>C([E(r)],0):r=>(m??(m={}),m.exp=R(w,u in i).getTime(),m.exp-Date.now()<24*60*60*1e3&&(clearTimeout(ue),clearTimeout(oe[u]),ue=setTimeout(()=>this.reset(),m.exp-Date.now())),m.exp=Math.trunc(m.exp/1e3),C([E(r),m],0));if(I){const r=`${P}${Q}`,d=E,T=_;if(E=V=>{try{return C([d(V)],I)}catch{return delete i[u],""}},r in i){const V=`${P}${C(p,$)}`;try{i[V]=K(_(q(i[r],0)[0])),delete i[r]}catch{}}_=V=>{try{return T(q(V,I)[0])}catch{return delete i[u],""}}}else delete i[`${N}${Q}`];const ae=r=>{const d=q(r,0);return m=d[1],_(d[0])};let le=!0;if(F[u]=w,i[`atom/${p}`]&&(i[u]||(i[u]=`[${i[`atom/${p}`]}]`),delete i[`atom/${p}`]),f=()=>{if(f=o,le){le=!1;try{s(u in i?ae(i[u]):e)}catch{console.warn("Invalid json value",i[u])}}return o()},S=r=>{if(r===e){this.reset();return}i[u]=K(r)},this.reset=()=>{delete i[u],b(e,!0)},G&&L[u]!==void 0&&console.warn("Duplicate Atom key",p),H)if(J){let r=!1,d;M[u]=this,L[u]=()=>{clearTimeout(d),d=setTimeout(()=>r=!1,10),!r&&(r=!0,i[u]=K(o()))}}else L[u]=r=>{if(r.newValue===null){this.reset();return}try{b(ae(r.newValue))}catch{console.warn("Invalid json value",r.newValue)}};return w}}let B;try{B=new BroadcastChannel("updateHere"),B.addEventListener("message",t=>{var e;(e=M[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const i=localStorage,L={},M={},be=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=L[t.key])==null||e.call(L,t)});const De=i.setItem.bind(i),xe=i.removeItem.bind(i);i.setItem=(t,e)=>{M[t]===void 0&&De.call(i,t,e)},i.removeItem=t=>{M[t]===void 0&&xe.call(i,t)};const $e=/"exp":\s*(\d+)/,P="atom\\",N="atom`s\\",F={},oe={},ie=/[a-z]/gi,U=5,W={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),n=String.fromCharCode(t+43).toLowerCase();W[e]=n,W[n]=e}const ce=t=>W[t]??t,C=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const n=t[1](e);return`${n.slice(0,U)}${n.slice(U).replace(ie,ce)}`},e=>btoa(t[2](e))];return(e,n)=>{try{return t[n](e)}catch(s){if(n===0)throw s;return t[0](e)}}})(),q=(()=>{const t=[e=>JSON.parse(e),e=>t[0](decodeURI(atob(e))),e=>t[1](`${e.slice(0,U)}${e.slice(U).replace(ie,ce)}`),e=>t[2](atob(e))];return(e,n)=>{try{return t[n](e)}catch(s){if(n===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(i).forEach(t=>{var s;if(typeof i[t]!="string"||!t.startsWith(P)&&!t.startsWith(N))return;const e=+((s=i[t].match($e))==null?void 0:s[1]);if(!e||e*1e3-Date.now()>24*60*60*1e3)return;const n=q(i[t],0);!Array.isArray(n)||n[1]==null||!("exp"in n[1])||n[1].exp!==e||(oe[t]=setTimeout(()=>{F[t]?F[t].reset():delete i[t]},e*1e3-Date.now()))})},1e3),l.Atom=ne,l.AtomArrayDoActions=k,l.AtomBooleanDoActions=O,l.AtomNumberDoActions=ee,l.AtomObjectDoActions=te,l.AtomSetDoActions=re,l.atom=ge,l.configureAtomaric=he,l.configuredOptions=D,l.useAtom=ye,l.useAtomDo=Ae,l.useAtomGet=de,l.useAtomSet=Y,l.useAtomSetDeferred=me,l.useAtomValue=X,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(l,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(l=typeof globalThis<"u"?globalThis:l||self,j(l.atomaric={}))})(this,function(l){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const $={},me=t=>{$.useSyncExternalStore=j=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},X=t=>j(t.subscribe,t.get),Y=t=>t.set,de=t=>t.setDeferred,Ae=t=>t.get,ge=t=>t.do,ye=t=>[X(t),Y(t)],pe=(t,e)=>new oe(t,e);class L{constructor(e){if(e)return new Proxy(this,{get:(r,s)=>s in this?r[s]:e[s]})}}const we=(t,e)=>{const r=(s,o,i)=>new Proxy(s,{get:(f,m,g)=>{const A=i===0&&e.get!=null?e.get(f,m,g):f[m];return typeof A=="object"&&A!==null?r(Array.isArray(A)?A.slice(0):{...A},o.concat(Array.isArray(f)?+m:m),i+1):A},set:(f,m,g)=>(e.onSet(f,o,m,g,f[m])&&(f[m]=g),!0)});return r(t,[],0)};class Z extends L{constructor(){super(...arguments),this.updateValue=(e,r)=>{const s=Array.isArray(e)?e.slice(0):{...e};let o=!1;const i=we(e,{onSet:(f,m,g,A,y)=>{if(A===y)return!0;let S=s;o=!0;for(const h of m){const d=S[h];S=S[h]=Array.isArray(d)?d.slice(0):{...d}}return S[g]=A,!0}});return r(i),o?s:e}}}class k extends Z{constructor(e,r){super(r),this.atom=e,this.push=(...s)=>{this.atom.set(this.atom.get().concat(s))},this.unshift=(...s)=>{this.atom.set(s.concat(this.atom.get()))},this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.filter=s=>{this.atom.set(this.atom.get().filter(s??Se))},this.add=s=>{this.atom.get().includes(s)||this.atom.set(this.atom.get().concat([s]))},this.remove=s=>{const o=this.atom.get().indexOf(s);if(o<0)return;const i=this.atom.get().slice(0);i.splice(o,1),this.atom.set(i)},this.toggle=(s,o)=>{const i=this.atom.get().slice(),f=i.indexOf(s);f<0?o?i.unshift(s):i.push(s):i.splice(f,1),this.atom.set(i)}}}const Se=t=>t;class O extends L{constructor(e,r){super(r),this.atom=e,this.toggle=()=>{this.atom.set(!this.atom.get())}}}class ee extends L{constructor(e,r){super(r),this.atom=e,this.setValue=(s,o)=>{const i=new Map(this.atom.get());i.set(s,o),this.atom.set(i)},this.delete=s=>{const o=new Map(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=(s,o)=>{const i=new Map(this.atom.get());i.has(s)?i.delete(s):i.set(s,o),this.atom.set(i)},this.clear=()=>{this.atom.set(new Map)},this.atom=e}}class te extends L{constructor(e,r){super(r),this.atom=e,this.increment=s=>{this.atom.set(+this.atom.get()+(s??1))}}}class se extends Z{constructor(e,r){super(r),this.atom=e,this.setPartial=s=>this.atom.set(o=>({...o,...typeof s=="function"?s(this.atom.get()):s})),this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.setDeepPartial=(s,o,i,f=$.keyPathSeparator||".")=>{if(!f)return;if(s.includes(f)){let A=s.split(f);const y=A[A.length-1];A=A.slice(0,-1);const S={...this.atom.get()};let h=S,d=i;for(const p of A){d=d==null?void 0:d[Array.isArray(d)?"0":p];const T=h[ne(h,p)]??(Array.isArray(d)?[]:{});if(T==null||typeof T!="object"){if(i==null)throw"Incorrect path for setDeepPartial";const b=typeof o=="function"?o(void 0):o;this.atom.get()[s]!==b&&this.setPartial({[s]:b});return}h=h[ne(h,p)]=Array.isArray(T)?[...T]:{...T}}const B=h[y];h[y]=typeof o=="function"?o(h[y]):o,B!==h[y]&&this.atom.set(S);return}const m=this.atom.get()[s],g=typeof o=="function"?o(m):o;g!==m&&this.setPartial({[s]:g})}}}const ne=(t,e)=>Array.isArray(t)?`${+e}`:e;class re extends L{constructor(e,r){super(r),this.atom=e,this.add=s=>{this.atom.set(new Set(this.atom.get()).add(s))},this.delete=s=>{const o=new Set(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=s=>{const o=new Set(this.atom.get());o.has(s)?o.delete(s):o.add(s),this.atom.set(o)},this.clear=()=>{this.atom.set(new Set)},this.atom=e}}const be=(t,e,r)=>{const s=typeof r=="object"&&r!=null&&"do"in r?r.do((i,f)=>e.set(i,f),()=>e.get(),e,(i,f,m)=>e.setDeferred(i,f,m)):null,o={};return s&&Object.keys(s).forEach(i=>Object.defineProperty(o,i,{get:()=>s[i]})),typeof t=="number"?new te(e,s):typeof t=="boolean"?new O(e,s):Array.isArray(t)?new k(e,s):t instanceof Set?new re(e,s):t instanceof Map?new ee(e,s):t instanceof Object?new se(e,s):new L(s)};class oe{constructor(e,r){e=typeof e=="function"?e():e;const s=n=>A=n,o=()=>A,i=new Set,f=n=>n(h());let m=!0,g=!1,A=e,y,S=()=>{},h=()=>o(),d=null,B=()=>{const n=be(e,p,r);return B=()=>n,n};const p=new Proxy(this,{get:(n,u)=>u==="do"?B():n[u],set:De}),T=()=>{m=!0,g!==!0&&S(h()),g=!1;try{N.postMessage({key:a,value:o()})}catch{}},b=(n,u)=>{const D=typeof n=="function"?n(h()):n;D!==h()&&(s(D),g=u,m&&(m=!1,i.forEach(f),queueMicrotask(T)))};this.set=(n,u)=>b(n,u),this.get=()=>h(),this.initialValue=e,this.isInitialValue=()=>e===o(),this.subscribe=n=>(i.add(n),()=>{i.delete(n)}),this.reset=()=>{b(e,!0)};const Me=(n,u)=>{b(n,u),y=void 0};if(this.setDeferred=(n,u=500,D,x=!0)=>{x&&y===void 0&&b(n,D),clearTimeout(y),y=setTimeout(Me,u,n,D)},r==null)return p;let w=null,z=null,G=!0,H=!0,J=!1,M=0,V=0,ue=-1,_=e instanceof Set||e instanceof Map?n=>new Set(n):n=>n,E=e instanceof Set?n=>{if(n instanceof Set)return Array.from(n);throw console.error(n),"The value is not Set instance"}:e instanceof Map?n=>{if(n instanceof Map){const u=[];return n.forEach((D,x)=>u.push([x,D])),u}throw console.error(n),"The value is not Set instance"}:n=>n;if(typeof r=="string")w=r;else if("storeKey"in r)G=r.warnOnDuplicateStoreKey??G,H=r.listenStorageChanges??H,w=r.storeKey,_=r.unzipValue??_,E=r.zipValue??E,J=r.unchangable??J,M=r.securifyKeyLevel??$.securifyKeyLevel??M,V=r.securifyValueLevel??$.securifyValueLevel??V,z=r.exp??z;else return p;const Q=M?I(w,M):w,a=`${V?v:C}${Q}`;if(M){const n=`${C}${w}`;n in c&&(c[a]=c[n],delete c[n])}else{const n=`${C}${I(w,M)}`;n in c&&(c[a]=c[n],delete c[n])}const K=z===null||!(z(p,a in c)instanceof Date)?n=>I([E(n)],0):n=>(d??(d={}),d.exp=z(p,a in c).getTime(),d.exp-Date.now()<24*60*60*1e3&&(clearTimeout(ue),clearTimeout(ce[a]),ue=setTimeout(()=>this.reset(),d.exp-Date.now())),d.exp=Math.trunc(d.exp/1e3),I([E(n),d],0));if(V){const n=`${C}${Q}`,u=E,D=_;if(E=x=>{try{return I([u(x)],V)}catch{return delete c[a],""}},n in c){const x=`${C}${I(w,M)}`;try{c[x]=K(_(q(c[n],0)[0])),delete c[n]}catch{}}_=x=>{try{return D(q(x,V)[0])}catch{return delete c[a],""}}}else delete c[`${v}${Q}`];const le=n=>{const u=q(n,0);return d=u[1],_(u[0])};let fe=!0;if(F[a]=p,c[`atom/${w}`]&&(c[a]||(c[a]=`[${c[`atom/${w}`]}]`),delete c[`atom/${w}`]),h=()=>{if(h=o,fe){fe=!1;try{s(a in c?le(c[a]):e)}catch{console.warn("Invalid json value",c[a])}}return o()},S=n=>{if(n===e){this.reset();return}c[a]=K(n)},this.reset=()=>{delete c[a],b(e,!0)},G&&P[a]!==void 0&&console.warn("Duplicate Atom key",w),H)if(J){let n=!1,u;R[a]=this,P[a]=()=>{clearTimeout(u),u=setTimeout(()=>n=!1,10),!n&&(n=!0,c[a]=K(o()))}}else P[a]=n=>{if(n.newValue===null){this.reset();return}try{b(le(n.newValue))}catch{console.warn("Invalid json value",n.newValue)}};return p}}let N;try{N=new BroadcastChannel("updateHere"),N.addEventListener("message",t=>{var e;(e=R[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const c=localStorage,P={},R={},De=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=P[t.key])==null||e.call(P,t)});const xe=c.setItem.bind(c),$e=c.removeItem.bind(c);c.setItem=(t,e)=>{R[t]===void 0&&xe.call(c,t,e)},c.removeItem=t=>{R[t]===void 0&&$e.call(c,t)};const Te=/"exp":\s*(\d+)/,C="atom\\",v="atom`s\\",F={},ce={},ie=/[a-z]/gi,U=5,W={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),r=String.fromCharCode(t+43).toLowerCase();W[e]=r,W[r]=e}const ae=t=>W[t]??t,I=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const r=t[1](e);return`${r.slice(0,U)}${r.slice(U).replace(ie,ae)}`},e=>btoa(t[2](e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})(),q=(()=>{const t=[e=>JSON.parse(e),e=>t[0](decodeURI(atob(e))),e=>t[1](`${e.slice(0,U)}${e.slice(U).replace(ie,ae)}`),e=>t[2](atob(e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(c).forEach(t=>{var s;if(typeof c[t]!="string"||!t.startsWith(C)&&!t.startsWith(v))return;const e=+((s=c[t].match(Te))==null?void 0:s[1]);if(!e||e*1e3-Date.now()>24*60*60*1e3)return;const r=q(c[t],0);!Array.isArray(r)||r[1]==null||!("exp"in r[1])||r[1].exp!==e||(ce[t]=setTimeout(()=>{F[t]?F[t].reset():delete c[t]},e*1e3-Date.now()))})},1e3),l.Atom=oe,l.AtomArrayDoActions=k,l.AtomBooleanDoActions=O,l.AtomMapDoActions=ee,l.AtomNumberDoActions=te,l.AtomObjectDoActions=se,l.AtomSetDoActions=re,l.atom=pe,l.configureAtomaric=me,l.configuredOptions=$,l.useAtom=ye,l.useAtomDo=ge,l.useAtomGet=Ae,l.useAtomSet=Y,l.useAtomSetDeferred=de,l.useAtomValue=X,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "atomaric",
3
3
  "description": "Manage your project state",
4
- "version": "0.0.69",
4
+ "version": "0.0.71",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
@@ -0,0 +1,77 @@
1
+ import { useSyncExternalStore } from 'react';
2
+ import { atom, configureAtomaric } from '../lib';
3
+ import { makeFullKey, wait } from '../utils';
4
+
5
+ configureAtomaric({
6
+ useSyncExternalStore,
7
+ keyPathSeparator: '.',
8
+ });
9
+
10
+ describe('Map', () => {
11
+ type Value = number | { asasa: '' };
12
+ const testAtom = atom(new Map<string, Value>(), {
13
+ storeKey: 'map:test',
14
+ do: (set, get) => ({
15
+ filterKeyValues: () => {
16
+ const newMap = new Map();
17
+ get().forEach((value, key) => {
18
+ if (value) newMap.set(key, value);
19
+ });
20
+ set(newMap);
21
+ },
22
+ }),
23
+ });
24
+
25
+ test('do.setValue()', async () => {
26
+ testAtom.do.setValue('!', 1);
27
+ testAtom.do.setValue('@', 0);
28
+ testAtom.do.setValue('#', { asasa: '' });
29
+ testAtom.do.setValue('', 123);
30
+ await wait();
31
+
32
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["@",0],["#",{"asasa":""}],["",123]]]');
33
+
34
+ expect(testAtom.get()).toEqual(
35
+ new Map<string, Value>([
36
+ ['!', 1],
37
+ ['@', 0],
38
+ ['#', { asasa: '' }],
39
+ ['', 123],
40
+ ]),
41
+ );
42
+ });
43
+
44
+ test('do.<filterKeyValues>()', async () => {
45
+ testAtom.do.filterKeyValues();
46
+ await wait();
47
+
48
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["#",{"asasa":""}],["",123]]]');
49
+ });
50
+
51
+ test('do.delete()', async () => {
52
+ testAtom.do.delete('#');
53
+ await wait();
54
+
55
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123]]]');
56
+ });
57
+
58
+ test('do.toggle()', async () => {
59
+ testAtom.do.toggle('#', 555);
60
+ await wait();
61
+
62
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123],["#",555]]]');
63
+
64
+ testAtom.do.toggle('', 999);
65
+ testAtom.do.toggle('!', 1234567890);
66
+ await wait();
67
+
68
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["#",555]]]');
69
+ });
70
+
71
+ test('do.clear()', async () => {
72
+ testAtom.do.clear();
73
+ await wait();
74
+
75
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[]]');
76
+ });
77
+ });
@@ -0,0 +1,42 @@
1
+ import { Atom } from '../../types';
2
+ import { AtomDoActionsBasic } from './_Basic';
3
+
4
+ export class AtomMapDoActions<
5
+ MapValue extends Map<any, any>,
6
+ Key extends MapValue extends Map<infer K, any> ? K : never,
7
+ Value extends MapValue extends Map<any, infer V> ? V : never,
8
+ > extends AtomDoActionsBasic {
9
+ constructor(private atom: Atom<MapValue>, actions: Record<string, Function> | nil) {
10
+ super(actions);
11
+ this.atom = atom;
12
+ }
13
+
14
+ /** like the Map.prototype.set() method */
15
+ setValue = (key: Key, value: Value) => {
16
+ const newMap = new Map(this.atom.get());
17
+ newMap.set(key, value);
18
+ this.atom.set(newMap as never);
19
+ };
20
+
21
+ /** like the Map.prototype.delete() method */
22
+ delete = (key: Key) => {
23
+ const newMap = new Map(this.atom.get());
24
+ newMap.delete(key);
25
+ this.atom.set(newMap as never);
26
+ };
27
+
28
+ /** will add value if it doesn't exist, otherwise delete */
29
+ toggle = (key: Key, value: Value) => {
30
+ const newMap = new Map(this.atom.get());
31
+
32
+ if (newMap.has(key)) newMap.delete(key);
33
+ else newMap.set(key, value);
34
+
35
+ this.atom.set(newMap as never);
36
+ };
37
+
38
+ /** like the Map.prototype.clear() method */
39
+ clear = () => {
40
+ this.atom.set(new Map() as never);
41
+ };
42
+ }
@@ -8,7 +8,7 @@ configureAtomaric({
8
8
  });
9
9
 
10
10
  describe('Object', () => {
11
- test('setDeepPartial()', async () => {
11
+ test('do.setDeepPartial()', async () => {
12
12
  const b = { c: [{ d: 8, e: 'e', f: 'F', g: { h: 'HHH' } }] };
13
13
  const a = { f: { g: '' }, b };
14
14
  const testAtom = atom({ a, b });
@@ -29,7 +29,7 @@ describe('Object', () => {
29
29
  expect(testAtom.get().b.c[8].e).toEqual('EE');
30
30
  });
31
31
 
32
- test('setDeepPartial() with first numeric prop', async () => {
32
+ test('do.setDeepPartial() with first numeric prop', async () => {
33
33
  enum Num {
34
34
  num = 123,
35
35
  }
@@ -42,7 +42,17 @@ describe('Object', () => {
42
42
  expect(testAtom.get()[Num.num].a).toEqual('AA');
43
43
  });
44
44
 
45
- test('update()', async () => {
45
+ test('do.setPartial()', async () => {
46
+ const testAtom = atom((): Record<number, unknown> => ({ 2: { a: 'A' } }));
47
+
48
+ testAtom.do.setPartial({ 3: { b: 'B' } });
49
+ testAtom.do.setPartial({ 4: [] });
50
+ await wait();
51
+
52
+ expect(testAtom.get()).toEqual({ 2: { a: 'A' }, 3: { b: 'B' }, 4: [] });
53
+ });
54
+
55
+ test('do.update()', async () => {
46
56
  const init = { a: { b: { c: { d: { e: 'E' } }, f: { g: {} } }, h: { i: { j: {} } } } };
47
57
  const testAtom = atom(init);
48
58
 
package/types/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { useSyncExternalStore } from 'react';
2
2
  import { AtomArrayDoActions } from '../src/do.classes/Array';
3
3
  import { AtomBooleanDoActions } from '../src/do.classes/Boolean';
4
+ import { AtomMapDoActions } from '../src/do.classes/Map';
4
5
  import { AtomNumberDoActions } from '../src/do.classes/Number';
5
6
  import { AtomObjectDoActions } from '../src/do.classes/Object';
6
7
  import { AtomSetDoActions } from '../src/do.classes/Set';
@@ -78,6 +79,8 @@ export type ObjectActionsSetDeepPartialDoAction<Value> = <
78
79
 
79
80
  export type DefaultActions<Value> = Value extends Set<infer Val>
80
81
  ? AtomSetDoActions<Val>
82
+ : Value extends Map<infer Key, infer Val>
83
+ ? AtomMapDoActions<Value, Key, Val>
81
84
  : Value extends boolean
82
85
  ? AtomBooleanDoActions
83
86
  : Value extends (infer Val)[]