atomaric 0.0.70 → 0.0.72

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