atomaric 0.0.65 → 0.0.67

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