atomaric 0.0.72 → 0.0.74

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,9 +1,9 @@
1
1
  let k = () => {
2
2
  throw "call configureAtomaric() before all!";
3
3
  };
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);
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), ie = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), ie(t)], Me = (t, e) => new pe(t, e);
7
7
  class _ {
8
8
  constructor(e) {
9
9
  if (e)
@@ -13,13 +13,13 @@ class _ {
13
13
  }
14
14
  }
15
15
  const ce = (t, e) => {
16
- const i = (s, n, o) => new Proxy(s, {
16
+ const i = (s, n, c) => new Proxy(s, {
17
17
  get: (l, h, p) => {
18
- const g = o === 0 && e.get != null ? e.get(l, h, p) : l[h];
18
+ const g = c === 0 && e.get != null ? e.get(l, h, p) : l[h];
19
19
  return typeof g == "object" && g !== null ? i(
20
20
  Array.isArray(g) ? g.slice(0) : { ...g },
21
21
  n.concat(Array.isArray(l) ? +h : h),
22
- o + 1
22
+ c + 1
23
23
  ) : g;
24
24
  },
25
25
  set: (l, h, p) => (e.onSet(l, n, h, p, l[h]) && (l[h] = p), !0)
@@ -31,7 +31,7 @@ class O extends _ {
31
31
  super(...arguments), this.updateValue = (e, i) => {
32
32
  const s = Array.isArray(e) ? e.slice(0) : { ...e };
33
33
  let n = !1;
34
- const o = ce(e, {
34
+ const c = ce(e, {
35
35
  onSet: (l, h, p, g, w) => {
36
36
  if (g === w) return !0;
37
37
  let m = s;
@@ -43,7 +43,7 @@ class O extends _ {
43
43
  return m[p] = g, !0;
44
44
  }
45
45
  });
46
- return i(o), n ? s : e;
46
+ return i(c), n ? s : e;
47
47
  };
48
48
  }
49
49
  }
@@ -54,8 +54,8 @@ class oe extends O {
54
54
  }, this.unshift = (...s) => {
55
55
  s.length !== 0 && this.a.set(s.concat(this.a.get()));
56
56
  }, this.update = (s) => {
57
- const n = this.a.get(), o = this.updateValue(n, s);
58
- o !== n && this.a.set(o);
57
+ const n = this.a.get(), c = this.updateValue(n, s);
58
+ c !== n && this.a.set(c);
59
59
  }, this.filter = (s) => {
60
60
  const n = this.a.get().filter(s ?? ae);
61
61
  n.length !== this.a.get().length && this.a.set(n);
@@ -64,11 +64,11 @@ class oe extends O {
64
64
  }, this.removeFirst = (s) => {
65
65
  const n = this.a.get().indexOf(s);
66
66
  if (n < 0) return;
67
- const o = this.a.get().slice(0);
68
- o.splice(n, 1), this.a.set(o);
67
+ const c = this.a.get().slice(0);
68
+ c.splice(n, 1), this.a.set(c);
69
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);
70
+ const c = this.a.get().slice(), l = c.indexOf(s);
71
+ l < 0 ? n ? c.unshift(s) : c.push(s) : c.splice(l, 1), this.a.set(c);
72
72
  };
73
73
  }
74
74
  }
@@ -84,15 +84,19 @@ class le extends _ {
84
84
  constructor(e, i) {
85
85
  super(i), this.a = e, this.setValue = (s, n) => {
86
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
+ const c = new Map(this.a.get());
88
+ c.set(s, n), this.a.set(c);
89
+ }, this.setIfNo = (s, n) => {
90
+ if (this.a.get().has(s)) return;
91
+ const c = new Map(this.a.get());
92
+ c.set(s, n), this.a.set(c);
89
93
  }, this.delete = (s) => {
90
94
  if (!this.a.get().has(s)) return;
91
95
  const n = new Map(this.a.get());
92
96
  n.delete(s), this.a.set(n);
93
97
  }, 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);
98
+ const c = new Map(this.a.get());
99
+ c.has(s) ? c.delete(s) : c.set(s, n), this.a.set(c);
96
100
  }, this.clear = () => {
97
101
  this.a.set(/* @__PURE__ */ new Map());
98
102
  }, this.a = e;
@@ -111,21 +115,21 @@ class he extends O {
111
115
  ...n,
112
116
  ...typeof s == "function" ? s(this.a.get()) : s
113
117
  })), this.update = (s) => {
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 || ".") => {
118
+ const n = this.a.get(), c = this.updateValue(n, s);
119
+ c !== n && this.a.set(c);
120
+ }, this.setDeepPartial = (s, n, c, l = M.keyPathSeparator || ".") => {
117
121
  if (!l) return;
118
122
  if (s.includes(l)) {
119
123
  let g = s.split(l);
120
124
  const w = g[g.length - 1];
121
125
  g = g.slice(0, -1);
122
126
  const m = { ...this.a.get() };
123
- let f = m, d = o;
127
+ let f = m, d = c;
124
128
  for (const y of g) {
125
129
  d = d == null ? void 0 : d[Array.isArray(d) ? "0" : y];
126
130
  const D = f[v(f, y)] ?? (Array.isArray(d) ? [] : {});
127
131
  if (D == null || typeof D != "object") {
128
- if (o == null) throw "Incorrect path for setDeepPartial";
132
+ if (c == null) throw "Incorrect path for setDeepPartial";
129
133
  const S = typeof n == "function" ? n(void 0) : n;
130
134
  this.a.get()[s] !== S && this.setPartial({ [s]: S });
131
135
  return;
@@ -160,17 +164,17 @@ class de extends _ {
160
164
  }
161
165
  const ge = (t, e, i) => {
162
166
  const s = typeof i == "object" && i != null && "do" in i ? i.do(
163
- (o, l) => e.set(o, l),
167
+ (c, l) => e.set(c, l),
164
168
  () => e.get(),
165
169
  e,
166
- (o, l, h) => e.setDeferred(o, l, h)
170
+ (c, l, h) => e.setDeferred(c, l, h)
167
171
  ) : 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);
172
+ return s && Object.keys(s).forEach((c) => Object.defineProperty(n, c, { get: () => s[c] })), 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);
169
173
  };
170
174
  class pe {
171
175
  constructor(e, i) {
172
176
  e = typeof e == "function" ? e() : e;
173
- const s = (r) => g = r, n = () => g, o = /* @__PURE__ */ new Set(), l = (r) => r(f());
177
+ const s = (r) => g = r, n = () => g, c = /* @__PURE__ */ new Set(), l = (r) => r(f());
174
178
  let h = !0, p = !1, g = e, w, m = () => {
175
179
  }, f = () => n(), d = null, E = () => {
176
180
  const r = ge(e, y, i);
@@ -187,10 +191,10 @@ class pe {
187
191
  }
188
192
  }, S = (r, u) => {
189
193
  const x = typeof r == "function" ? r(f()) : r;
190
- x !== f() && (s(x), p = u, h && (h = !1, o.forEach(l), queueMicrotask(D)));
194
+ x !== f() && (s(x), p = u, h && (h = !1, c.forEach(l), queueMicrotask(D)));
191
195
  };
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);
196
+ this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === n(), this.subscribe = (r) => (c.add(r), () => {
197
+ c.delete(r);
194
198
  }), this.reset = () => {
195
199
  S(e, !0);
196
200
  };
@@ -200,7 +204,7 @@ class pe {
200
204
  if (this.setDeferred = (r, u = 500, x, b = !0) => {
201
205
  b && w === void 0 && S(r, x), clearTimeout(w), w = setTimeout(re, u, r, x);
202
206
  }, 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) => {
207
+ let A = null, j = null, q = !0, F = !0, N = !1, $ = 0, T = 0, K = -1, I = e instanceof Set || e instanceof Map ? (r) => new Set(r) : (r) => r, L = e instanceof Set ? (r) => {
204
208
  if (r instanceof Set) return Array.from(r);
205
209
  throw console.error(r), "The value is not Set instance";
206
210
  } : e instanceof Map ? (r) => {
@@ -213,52 +217,52 @@ class pe {
213
217
  if (typeof i == "string")
214
218
  A = i;
215
219
  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;
220
+ q = i.warnOnDuplicateStoreKey ?? q, F = i.listenStorageChanges ?? F, A = i.storeKey, I = i.unzipValue ?? I, L = i.zipValue ?? L, N = i.unchangable ?? N, $ = i.securifyKeyLevel ?? M.securifyKeyLevel ?? $, T = i.securifyValueLevel ?? M.securifyValueLevel ?? T, j = i.exp ?? j;
217
221
  else return y;
218
- const N = $ ? I(A, $) : A, a = `${L ? J : P}${N}`;
222
+ const B = $ ? C(A, $) : A, a = `${T ? J : P}${B}`;
219
223
  if ($) {
220
224
  const r = `${P}${A}`;
221
- r in c && (c[a] = c[r], delete c[r]);
225
+ r in o && (o[a] = o[r], delete o[r]);
222
226
  } else {
223
- const r = `${P}${I(A, $)}`;
224
- r in c && (c[a] = c[r], delete c[r]);
227
+ const r = `${P}${C(A, $)}`;
228
+ r in o && (o[a] = o[r], delete o[r]);
225
229
  }
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) => {
230
+ const W = j === null || !(j(y, a in o) instanceof Date) ? (r) => C([L(r)], 0) : (r) => (d ?? (d = {}), d.exp = j(y, a in o).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), C([L(r), d], 0));
231
+ if (T) {
232
+ const r = `${P}${B}`, u = L, x = I;
233
+ if (L = (b) => {
230
234
  try {
231
- return I([u(b)], L);
235
+ return C([u(b)], T);
232
236
  } catch {
233
- return delete c[a], "";
237
+ return delete o[a], "";
234
238
  }
235
- }, r in c) {
236
- const b = `${P}${I(A, $)}`;
239
+ }, r in o) {
240
+ const b = `${P}${C(A, $)}`;
237
241
  try {
238
- c[b] = W(M(z(c[r], 0)[0])), delete c[r];
242
+ o[b] = W(I(z(o[r], 0)[0])), delete o[r];
239
243
  } catch {
240
244
  }
241
245
  }
242
- M = (b) => {
246
+ I = (b) => {
243
247
  try {
244
- return x(z(b, L)[0]);
248
+ return x(z(b, T)[0]);
245
249
  } catch {
246
- return delete c[a], "";
250
+ return delete o[a], "";
247
251
  }
248
252
  };
249
- } else delete c[`${J}${N}`];
253
+ } else delete o[`${J}${B}`];
250
254
  const X = (r) => {
251
255
  const u = z(r, 0);
252
- return d = u[1], M(u[0]);
256
+ return d = u[1], I(u[0]);
253
257
  };
254
258
  let Y = !0;
255
- if (G[a] = y, c[`atom/${A}`] && (c[a] || (c[a] = `[${c[`atom/${A}`]}]`), delete c[`atom/${A}`]), f = () => {
259
+ if (G[a] = y, o[`atom/${A}`] && (o[a] || (o[a] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => {
256
260
  if (f = n, Y) {
257
261
  Y = !1;
258
262
  try {
259
- s(a in c ? X(c[a]) : e);
263
+ s(a in o ? X(o[a]) : e);
260
264
  } catch {
261
- console.warn("Invalid json value", c[a]);
265
+ console.warn("Invalid json value", o[a]);
262
266
  }
263
267
  }
264
268
  return n();
@@ -267,14 +271,14 @@ class pe {
267
271
  this.reset();
268
272
  return;
269
273
  }
270
- c[a] = W(r);
274
+ o[a] = W(r);
271
275
  }, this.reset = () => {
272
- delete c[a], S(e, !0);
276
+ delete o[a], S(e, !0);
273
277
  }, q && V[a] !== void 0 && console.warn("Duplicate Atom key", A), F)
274
- if (B) {
278
+ if (N) {
275
279
  let r = !1, u;
276
280
  U[a] = this, V[a] = () => {
277
- clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = W(n()));
281
+ clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, o[a] = W(n()));
278
282
  };
279
283
  } else
280
284
  V[a] = (r) => {
@@ -299,19 +303,19 @@ try {
299
303
  });
300
304
  } catch {
301
305
  }
302
- const c = localStorage, V = {}, U = {}, we = (t, e) => {
306
+ const o = localStorage, V = {}, U = {}, we = (t, e) => {
303
307
  throw `${e} is readonly property`;
304
308
  };
305
309
  window.addEventListener("storage", (t) => {
306
310
  var e;
307
311
  t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
308
312
  });
309
- const ye = c.setItem.bind(c), Ae = c.removeItem.bind(c);
310
- c.setItem = (t, e) => {
311
- U[t] === void 0 && ye.call(c, t, e);
313
+ const ye = o.setItem.bind(o), Ae = o.removeItem.bind(o);
314
+ o.setItem = (t, e) => {
315
+ U[t] === void 0 && ye.call(o, t, e);
312
316
  };
313
- c.removeItem = (t) => {
314
- U[t] === void 0 && Ae.call(c, t);
317
+ o.removeItem = (t) => {
318
+ U[t] === void 0 && Ae.call(o, t);
315
319
  };
316
320
  const me = /"exp":\s*(\d+)/, P = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
317
321
  for (let t = 54; t < 80; t++) {
@@ -319,7 +323,7 @@ for (let t = 54; t < 80; t++) {
319
323
  const e = String.fromCharCode(t + 43).toUpperCase(), i = String.fromCharCode(t + 43).toLowerCase();
320
324
  Q[e] = i, Q[i] = e;
321
325
  }
322
- const se = (t) => Q[t] ?? t, I = /* @__PURE__ */ (() => {
326
+ const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
323
327
  const t = [
324
328
  (e) => JSON.stringify(e),
325
329
  (e) => btoa(encodeURI(t[0](e))),
@@ -356,14 +360,14 @@ const se = (t) => Q[t] ?? t, I = /* @__PURE__ */ (() => {
356
360
  };
357
361
  })();
358
362
  setTimeout(() => {
359
- Object.keys(c).forEach((t) => {
363
+ Object.keys(o).forEach((t) => {
360
364
  var s;
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]);
365
+ if (typeof o[t] != "string" || !t.startsWith(P) && !t.startsWith(J)) return;
366
+ const e = +((s = o[t].match(me)) == null ? void 0 : s[1]);
363
367
  if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
364
- const i = z(c[t], 0);
368
+ const i = z(o[t], 0);
365
369
  !Array.isArray(i) || i[1] == null || !("exp" in i[1]) || i[1].exp !== e || (ee[t] = setTimeout(() => {
366
- G[t] ? G[t].reset() : delete c[t];
370
+ G[t] ? G[t].reset() : delete o[t];
367
371
  }, e * 1e3 - Date.now()));
368
372
  });
369
373
  }, 1e3);
@@ -375,9 +379,9 @@ export {
375
379
  fe as AtomNumberDoActions,
376
380
  he as AtomObjectDoActions,
377
381
  de as AtomSetDoActions,
378
- Te as atom,
382
+ Me as atom,
379
383
  Se as configureAtomaric,
380
- T as configuredOptions,
384
+ M as configuredOptions,
381
385
  $e as useAtom,
382
386
  De as useAtomDo,
383
387
  be as useAtomGet,
@@ -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 $={},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"})});
1
+ (function(f,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(f=typeof globalThis<"u"?globalThis:f||self,j(f.atomaric={}))})(this,function(f){"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,pe=t=>[Y(t),Z(t)],we=(t,e)=>new ie(t,e);class I{constructor(e){if(e)return new Proxy(this,{get:(i,s)=>s in this?i[s]:e[s]})}}const ye=(t,e)=>{const i=(s,r,o)=>new Proxy(s,{get:(l,d,m)=>{const A=o===0&&e.get!=null?e.get(l,d,m):l[d];return typeof A=="object"&&A!==null?i(Array.isArray(A)?A.slice(0):{...A},r.concat(Array.isArray(l)?+d:d),o+1):A},set:(l,d,m)=>(e.onSet(l,r,d,m,l[d])&&(l[d]=m),!0)});return i(t,[],0)};class v extends I{constructor(){super(...arguments),this.updateValue=(e,i)=>{const s=Array.isArray(e)?e.slice(0):{...e};let r=!1;const o=ye(e,{onSet:(l,d,m,A,p)=>{if(A===p)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 i(o),r?s:e}}}class k extends v{constructor(e,i){super(i),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(),o=this.updateValue(r,s);o!==r&&this.a.set(o)},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 o=this.a.get().slice(0);o.splice(r,1),this.a.set(o)},this.toggle=(s,r)=>{const o=this.a.get().slice(),l=o.indexOf(s);l<0?r?o.unshift(s):o.push(s):o.splice(l,1),this.a.set(o)}}}const Se=t=>t;class O extends I{constructor(e,i){super(i),this.a=e,this.toggle=()=>{this.a.set(!this.a.get())}}}class ee extends I{constructor(e,i){super(i),this.a=e,this.setValue=(s,r)=>{if(this.a.get().get(s)===r)return;const o=new Map(this.a.get());o.set(s,r),this.a.set(o)},this.setIfNo=(s,r)=>{if(this.a.get().has(s))return;const o=new Map(this.a.get());o.set(s,r),this.a.set(o)},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 o=new Map(this.a.get());o.has(s)?o.delete(s):o.set(s,r),this.a.set(o)},this.clear=()=>{this.a.set(new Map)},this.a=e}}class te extends I{constructor(e,i){super(i),this.a=e,this.increment=s=>{this.a.set(+this.a.get()+(s??1))}}}class se extends v{constructor(e,i){super(i),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(),o=this.updateValue(r,s);o!==r&&this.a.set(o)},this.setDeepPartial=(s,r,o,l=$.keyPathSeparator||".")=>{if(!l)return;if(s.includes(l)){let A=s.split(l);const p=A[A.length-1];A=A.slice(0,-1);const S={...this.a.get()};let h=S,g=o;for(const w of A){g=g==null?void 0:g[Array.isArray(g)?"0":w];const M=h[ne(h,w)]??(Array.isArray(g)?[]:{});if(M==null||typeof M!="object"){if(o==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,w)]=Array.isArray(M)?[...M]:{...M}}const q=h[p];h[p]=typeof r=="function"?r(h[p]):r,q!==h[p]&&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 I{constructor(e,i){super(i),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,i)=>{const s=typeof i=="object"&&i!=null&&"do"in i?i.do((o,l)=>e.set(o,l),()=>e.get(),e,(o,l,d)=>e.setDeferred(o,l,d)):null,r={};return s&&Object.keys(s).forEach(o=>Object.defineProperty(r,o,{get:()=>s[o]})),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 I(s)};class ie{constructor(e,i){e=typeof e=="function"?e():e;const s=n=>A=n,r=()=>A,o=new Set,l=n=>n(h());let d=!0,m=!1,A=e,p,S=()=>{},h=()=>r(),g=null,q=()=>{const n=be(e,w,i);return q=()=>n,n};const w=new Proxy(this,{get:(n,u)=>u==="do"?q():n[u],set:De}),M=()=>{d=!0,m!==!0&&S(h()),m=!1;try{B.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,o.forEach(l),queueMicrotask(M)))};this.set=(n,u)=>b(n,u),this.get=()=>h(),this.initialValue=e,this.isInitialValue=()=>e===r(),this.subscribe=n=>(o.add(n),()=>{o.delete(n)}),this.reset=()=>{b(e,!0)};const Te=(n,u)=>{b(n,u),p=void 0};if(this.setDeferred=(n,u=500,D,x=!0)=>{x&&p===void 0&&b(n,D),clearTimeout(p),p=setTimeout(Te,u,n,D)},i==null)return w;let y=null,z=null,H=!0,J=!0,Q=!1,T=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 i=="string")y=i;else if("storeKey"in i)H=i.warnOnDuplicateStoreKey??H,J=i.listenStorageChanges??J,y=i.storeKey,_=i.unzipValue??_,E=i.zipValue??E,Q=i.unchangable??Q,T=i.securifyKeyLevel??$.securifyKeyLevel??T,V=i.securifyValueLevel??$.securifyValueLevel??V,z=i.exp??z;else return w;const K=T?C(y,T):y,a=`${V?F:P}${K}`;if(T){const n=`${P}${y}`;n in c&&(c[a]=c[n],delete c[n])}else{const n=`${P}${C(y,T)}`;n in c&&(c[a]=c[n],delete c[n])}const X=z===null||!(z(w,a in c)instanceof Date)?n=>C([E(n)],0):n=>(g??(g={}),g.exp=z(w,a in c).getTime(),g.exp-Date.now()<24*60*60*1e3&&(clearTimeout(ue),clearTimeout(oe[a]),ue=setTimeout(()=>this.reset(),g.exp-Date.now())),g.exp=Math.trunc(g.exp/1e3),C([E(n),g],0));if(V){const n=`${P}${K}`,u=E,D=_;if(E=x=>{try{return C([u(x)],V)}catch{return delete c[a],""}},n in c){const x=`${P}${C(y,T)}`;try{c[x]=X(_(U(c[n],0)[0])),delete c[n]}catch{}}_=x=>{try{return D(U(x,V)[0])}catch{return delete c[a],""}}}else delete c[`${F}${K}`];const fe=n=>{const u=U(n,0);return g=u[1],_(u[0])};let le=!0;if(W[a]=w,c[`atom/${y}`]&&(c[a]||(c[a]=`[${c[`atom/${y}`]}]`),delete c[`atom/${y}`]),h=()=>{if(h=r,le){le=!1;try{s(a in c?fe(c[a]):e)}catch{console.warn("Invalid json value",c[a])}}return r()},S=n=>{if(n===e){this.reset();return}c[a]=X(n)},this.reset=()=>{delete c[a],b(e,!0)},H&&L[a]!==void 0&&console.warn("Duplicate Atom key",y),J)if(Q){let n=!1,u;R[a]=this,L[a]=()=>{clearTimeout(u),u=setTimeout(()=>n=!1,10),!n&&(n=!0,c[a]=X(r()))}}else L[a]=n=>{if(n.newValue===null){this.reset();return}try{b(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=R[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const c=localStorage,L={},R={},De=(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 xe=c.setItem.bind(c),$e=c.removeItem.bind(c);c.setItem=(t,e)=>{R[t]===void 0&&xe.call(c,t,e)},c.removeItem=t=>{R[t]===void 0&&$e.call(c,t)};const Me=/"exp":\s*(\d+)/,P="atom\\",F="atom`s\\",W={},oe={},ce=/[a-z]/gi,N=5,G={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),i=String.fromCharCode(t+43).toLowerCase();G[e]=i,G[i]=e}const ae=t=>G[t]??t,C=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const i=t[1](e);return`${i.slice(0,N)}${i.slice(N).replace(ce,ae)}`},e=>btoa(t[2](e))];return(e,i)=>{try{return t[i](e)}catch(s){if(i===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,N)}${e.slice(N).replace(ce,ae)}`),e=>t[2](atob(e))];return(e,i)=>{try{return t[i](e)}catch(s){if(i===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(c).forEach(t=>{var s;if(typeof c[t]!="string"||!t.startsWith(P)&&!t.startsWith(F))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 i=U(c[t],0);!Array.isArray(i)||i[1]==null||!("exp"in i[1])||i[1].exp!==e||(oe[t]=setTimeout(()=>{W[t]?W[t].reset():delete c[t]},e*1e3-Date.now()))})},1e3),f.Atom=ie,f.AtomArrayDoActions=k,f.AtomBooleanDoActions=O,f.AtomMapDoActions=ee,f.AtomNumberDoActions=te,f.AtomObjectDoActions=se,f.AtomSetDoActions=re,f.atom=we,f.configureAtomaric=de,f.configuredOptions=$,f.useAtom=pe,f.useAtomDo=me,f.useAtomGet=Ae,f.useAtomSet=Z,f.useAtomSetDeferred=ge,f.useAtomValue=Y,Object.defineProperty(f,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.72",
4
+ "version": "0.0.74",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
@@ -11,7 +11,7 @@ export class AtomMapDoActions<
11
11
  this.a = a;
12
12
  }
13
13
 
14
- /** like the Map.prototype.set() method */
14
+ /** like the Map.prototype.set() method, when value is new for key in current atom value */
15
15
  setValue = (key: Key, value: Value) => {
16
16
  if (this.a.get().get(key) === value) return;
17
17
 
@@ -21,6 +21,16 @@ export class AtomMapDoActions<
21
21
  this.a.set(newMap as never);
22
22
  };
23
23
 
24
+ /** like the Map.prototype.set() method, when key is not exists */
25
+ setIfNo = (key: Key, value: Value) => {
26
+ if (this.a.get().has(key)) return;
27
+
28
+ const newMap = new Map(this.a.get());
29
+ newMap.set(key, value);
30
+
31
+ this.a.set(newMap as never);
32
+ };
33
+
24
34
  /** like the Map.prototype.delete() method */
25
35
  delete = (key: Key) => {
26
36
  if (!this.a.get().has(key)) return;