atomaric 0.0.64 → 0.0.66

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