atomaric 0.0.70 → 0.0.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/atomaric.js CHANGED
@@ -1,9 +1,9 @@
1
1
  let k = () => {
2
2
  throw "call configureAtomaric() before all!";
3
3
  };
4
- const M = {}, Se = (t) => {
5
- M.useSyncExternalStore = k = t.useSyncExternalStore, M.keyPathSeparator = t.keyPathSeparator, M.securifyKeyLevel = t.securifyKeyLevel, M.securifyValueLevel = t.securifyValueLevel;
6
- }, ne = (t) => k(t.subscribe, t.get), oe = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), oe(t)], Me = (t, e) => new pe(t, e);
4
+ const T = {}, Se = (t) => {
5
+ T.useSyncExternalStore = k = t.useSyncExternalStore, T.keyPathSeparator = t.keyPathSeparator, T.securifyKeyLevel = t.securifyKeyLevel, T.securifyValueLevel = t.securifyValueLevel;
6
+ }, ne = (t) => k(t.subscribe, t.get), oe = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), oe(t)], Te = (t, e) => new pe(t, e);
7
7
  class _ {
8
8
  constructor(e) {
9
9
  if (e)
@@ -32,15 +32,15 @@ class O extends _ {
32
32
  const s = Array.isArray(e) ? e.slice(0) : { ...e };
33
33
  let o = !1;
34
34
  const i = ce(e, {
35
- onSet: (l, h, p, d, y) => {
36
- if (d === y) return !0;
37
- let S = s;
35
+ onSet: (l, h, p, d, g) => {
36
+ if (d === g) return !0;
37
+ let A = s;
38
38
  o = !0;
39
39
  for (const f of h) {
40
- const m = S[f];
41
- S = S[f] = Array.isArray(m) ? m.slice(0) : { ...m };
40
+ const m = A[f];
41
+ A = A[f] = Array.isArray(m) ? m.slice(0) : { ...m };
42
42
  }
43
- return S[p] = d, !0;
43
+ return A[p] = d, !0;
44
44
  }
45
45
  });
46
46
  return n(i), o ? s : e;
@@ -110,27 +110,27 @@ class he extends O {
110
110
  })), this.update = (s) => {
111
111
  const o = this.atom.get(), i = this.updateValue(o, s);
112
112
  i !== o && this.atom.set(i);
113
- }, this.setDeepPartial = (s, o, i, l = M.keyPathSeparator || ".") => {
113
+ }, this.setDeepPartial = (s, o, i, l = T.keyPathSeparator || ".") => {
114
114
  if (!l) return;
115
115
  if (s.includes(l)) {
116
116
  let d = s.split(l);
117
- const y = d[d.length - 1];
117
+ const g = d[d.length - 1];
118
118
  d = d.slice(0, -1);
119
- const S = { ...this.atom.get() };
120
- let f = S, m = i;
119
+ const A = { ...this.atom.get() };
120
+ let f = A, m = i;
121
121
  for (const w of d) {
122
122
  m = m == null ? void 0 : m[Array.isArray(m) ? "0" : w];
123
123
  const D = f[Z(f, w)] ?? (Array.isArray(m) ? [] : {});
124
124
  if (D == null || typeof D != "object") {
125
125
  if (i == null) throw "Incorrect path for setDeepPartial";
126
- const b = typeof o == "function" ? o(void 0) : o;
127
- this.atom.get()[s] !== b && this.setPartial({ [s]: b });
126
+ const S = typeof o == "function" ? o(void 0) : o;
127
+ this.atom.get()[s] !== S && this.setPartial({ [s]: S });
128
128
  return;
129
129
  }
130
130
  f = f[Z(f, w)] = Array.isArray(D) ? [...D] : { ...D };
131
131
  }
132
- const E = f[y];
133
- f[y] = typeof o == "function" ? o(f[y]) : o, E !== f[y] && this.atom.set(S);
132
+ const E = f[g];
133
+ f[g] = typeof o == "function" ? o(f[g]) : o, E !== f[g] && this.atom.set(A);
134
134
  return;
135
135
  }
136
136
  const h = this.atom.get()[s], p = typeof o == "function" ? o(h) : o;
@@ -167,133 +167,119 @@ class pe {
167
167
  constructor(e, n) {
168
168
  e = typeof e == "function" ? e() : e;
169
169
  const s = (r) => d = r, o = () => d, i = /* @__PURE__ */ new Set(), l = (r) => r(f());
170
- let h = !0, p = !1, d = e, y, S = () => {
170
+ let h = !0, p = !1, d = e, g, A = () => {
171
171
  }, f = () => o(), m = null, E = () => {
172
172
  const r = de(e, w, n);
173
173
  return E = () => r, r;
174
174
  };
175
175
  const w = new Proxy(this, {
176
- get: (r, a) => a === "do" ? E() : r[a],
176
+ get: (r, u) => u === "do" ? E() : r[u],
177
177
  set: ge
178
178
  }), D = () => {
179
- h = !0, p !== !0 && S(f()), p = !1;
179
+ h = !0, p !== !0 && A(f()), p = !1;
180
180
  try {
181
- v.postMessage({ key: u, value: o() });
181
+ v.postMessage({ key: a, value: o() });
182
182
  } catch {
183
183
  }
184
- }, b = (r, a) => {
185
- const g = typeof r == "function" ? r(f()) : r;
186
- g !== f() && (s(g), p = a, h && (h = !1, i.forEach(l), queueMicrotask(D)));
184
+ }, S = (r, u) => {
185
+ const x = typeof r == "function" ? r(f()) : r;
186
+ x !== f() && (s(x), p = u, h && (h = !1, i.forEach(l), queueMicrotask(D)));
187
187
  };
188
- this.set = (r, a) => b(r, a), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === o(), this.subscribe = (r) => (i.add(r), () => {
188
+ this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === o(), this.subscribe = (r) => (i.add(r), () => {
189
189
  i.delete(r);
190
190
  }), this.reset = () => {
191
- b(e, !0);
191
+ S(e, !0);
192
192
  };
193
- const re = (r, a) => {
194
- b(r, a), y = void 0;
193
+ const re = (r, u) => {
194
+ S(r, u), g = void 0;
195
195
  };
196
- if (this.setDeferred = (r, a = 500, g, x = !0) => {
197
- x && y === void 0 && b(r, g), clearTimeout(y), y = setTimeout(re, a, r, g);
196
+ if (this.setDeferred = (r, u = 500, x, b = !0) => {
197
+ b && g === void 0 && S(r, x), clearTimeout(g), g = setTimeout(re, u, r, x);
198
198
  }, n == null) return w;
199
- let A = null, V = null, U = !0, q = !0, J = !1, $ = 0, T = 0, K = -1, L = e instanceof Set ? (r) => new Set(r) : e instanceof Map ? (r) => {
200
- if (!r || typeof r != "object") return /* @__PURE__ */ new Map();
201
- const a = /* @__PURE__ */ new Map();
202
- for (const g in r)
203
- try {
204
- a.set(JSON.parse(g), r[g]);
205
- } catch {
206
- }
207
- return a;
208
- } : (r) => r, j = e instanceof Set ? (r) => {
199
+ let y = null, j = null, q = !0, B = !0, F = !1, $ = 0, L = 0, Q = -1, M = e instanceof Set || e instanceof Map ? (r) => new Set(r) : (r) => r, C = e instanceof Set ? (r) => {
209
200
  if (r instanceof Set) return Array.from(r);
210
201
  throw console.error(r), "The value is not Set instance";
211
202
  } : e instanceof Map ? (r) => {
212
203
  if (r instanceof Map) {
213
- const a = {};
214
- return r.forEach((g, x) => {
215
- try {
216
- a[JSON.stringify(x)] = g;
217
- } catch {
218
- }
219
- }), a;
204
+ const u = [];
205
+ return r.forEach((x, b) => u.push([b, x])), u;
220
206
  }
221
207
  throw console.error(r), "The value is not Set instance";
222
208
  } : (r) => r;
223
209
  if (typeof n == "string")
224
- A = n;
210
+ y = n;
225
211
  else if ("storeKey" in n)
226
- U = n.warnOnDuplicateStoreKey ?? U, q = n.listenStorageChanges ?? q, A = n.storeKey, L = n.unzipValue ?? L, j = n.zipValue ?? j, J = n.unchangable ?? J, $ = n.securifyKeyLevel ?? M.securifyKeyLevel ?? $, T = n.securifyValueLevel ?? M.securifyValueLevel ?? T, V = n.exp ?? V;
212
+ q = n.warnOnDuplicateStoreKey ?? q, B = n.listenStorageChanges ?? B, y = n.storeKey, M = n.unzipValue ?? M, C = n.zipValue ?? C, F = n.unchangable ?? F, $ = n.securifyKeyLevel ?? T.securifyKeyLevel ?? $, L = n.securifyValueLevel ?? T.securifyValueLevel ?? L, j = n.exp ?? j;
227
213
  else return w;
228
- const B = $ ? C(A, $) : A, u = `${T ? H : I}${B}`;
214
+ const N = $ ? I(y, $) : y, a = `${L ? H : P}${N}`;
229
215
  if ($) {
230
- const r = `${I}${A}`;
231
- r in c && (c[u] = c[r], delete c[r]);
216
+ const r = `${P}${y}`;
217
+ r in c && (c[a] = c[r], delete c[r]);
232
218
  } else {
233
- const r = `${I}${C(A, $)}`;
234
- r in c && (c[u] = c[r], delete c[r]);
219
+ const r = `${P}${I(y, $)}`;
220
+ r in c && (c[a] = c[r], delete c[r]);
235
221
  }
236
- const F = V === null || !(V(w, u in c) instanceof Date) ? (r) => C([j(r)], 0) : (r) => (m ?? (m = {}), m.exp = V(w, u in c).getTime(), m.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(K), clearTimeout(ee[u]), K = setTimeout(() => this.reset(), m.exp - Date.now())), m.exp = Math.trunc(m.exp / 1e3), C([j(r), m], 0));
237
- if (T) {
238
- const r = `${I}${B}`, a = j, g = L;
239
- if (j = (x) => {
222
+ const W = j === null || !(j(w, a in c) instanceof Date) ? (r) => I([C(r)], 0) : (r) => (m ?? (m = {}), m.exp = j(w, a in c).getTime(), m.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(Q), clearTimeout(ee[a]), Q = setTimeout(() => this.reset(), m.exp - Date.now())), m.exp = Math.trunc(m.exp / 1e3), I([C(r), m], 0));
223
+ if (L) {
224
+ const r = `${P}${N}`, u = C, x = M;
225
+ if (C = (b) => {
240
226
  try {
241
- return C([a(x)], T);
227
+ return I([u(b)], L);
242
228
  } catch {
243
- return delete c[u], "";
229
+ return delete c[a], "";
244
230
  }
245
231
  }, r in c) {
246
- const x = `${I}${C(A, $)}`;
232
+ const b = `${P}${I(y, $)}`;
247
233
  try {
248
- c[x] = F(L(z(c[r], 0)[0])), delete c[r];
234
+ c[b] = W(M(z(c[r], 0)[0])), delete c[r];
249
235
  } catch {
250
236
  }
251
237
  }
252
- L = (x) => {
238
+ M = (b) => {
253
239
  try {
254
- return g(z(x, T)[0]);
240
+ return x(z(b, L)[0]);
255
241
  } catch {
256
- return delete c[u], "";
242
+ return delete c[a], "";
257
243
  }
258
244
  };
259
- } else delete c[`${H}${B}`];
260
- const X = (r) => {
261
- const a = z(r, 0);
262
- return m = a[1], L(a[0]);
245
+ } else delete c[`${H}${N}`];
246
+ const K = (r) => {
247
+ const u = z(r, 0);
248
+ return m = u[1], M(u[0]);
263
249
  };
264
- let Y = !0;
265
- if (G[u] = w, c[`atom/${A}`] && (c[u] || (c[u] = `[${c[`atom/${A}`]}]`), delete c[`atom/${A}`]), f = () => {
266
- if (f = o, Y) {
267
- Y = !1;
250
+ let X = !0;
251
+ if (J[a] = w, c[`atom/${y}`] && (c[a] || (c[a] = `[${c[`atom/${y}`]}]`), delete c[`atom/${y}`]), f = () => {
252
+ if (f = o, X) {
253
+ X = !1;
268
254
  try {
269
- s(u in c ? X(c[u]) : e);
255
+ s(a in c ? K(c[a]) : e);
270
256
  } catch {
271
- console.warn("Invalid json value", c[u]);
257
+ console.warn("Invalid json value", c[a]);
272
258
  }
273
259
  }
274
260
  return o();
275
- }, S = (r) => {
261
+ }, A = (r) => {
276
262
  if (r === e) {
277
263
  this.reset();
278
264
  return;
279
265
  }
280
- c[u] = F(r);
266
+ c[a] = W(r);
281
267
  }, this.reset = () => {
282
- delete c[u], b(e, !0);
283
- }, U && P[u] !== void 0 && console.warn("Duplicate Atom key", A), q)
284
- if (J) {
285
- let r = !1, a;
286
- N[u] = this, P[u] = () => {
287
- clearTimeout(a), a = setTimeout(() => r = !1, 10), !r && (r = !0, c[u] = F(o()));
268
+ delete c[a], S(e, !0);
269
+ }, q && V[a] !== void 0 && console.warn("Duplicate Atom key", y), B)
270
+ if (F) {
271
+ let r = !1, u;
272
+ U[a] = this, V[a] = () => {
273
+ clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = W(o()));
288
274
  };
289
275
  } else
290
- P[u] = (r) => {
276
+ V[a] = (r) => {
291
277
  if (r.newValue === null) {
292
278
  this.reset();
293
279
  return;
294
280
  }
295
281
  try {
296
- b(X(r.newValue));
282
+ S(K(r.newValue));
297
283
  } catch {
298
284
  console.warn("Invalid json value", r.newValue);
299
285
  }
@@ -305,31 +291,31 @@ let v;
305
291
  try {
306
292
  v = new BroadcastChannel("updateHere"), v.addEventListener("message", (t) => {
307
293
  var e;
308
- (e = N[t.data.key]) == null || e.set(t.data.value, !0);
294
+ (e = U[t.data.key]) == null || e.set(t.data.value, !0);
309
295
  });
310
296
  } catch {
311
297
  }
312
- const c = localStorage, P = {}, N = {}, ge = (t, e) => {
298
+ const c = localStorage, V = {}, U = {}, ge = (t, e) => {
313
299
  throw `${e} is readonly property`;
314
300
  };
315
301
  window.addEventListener("storage", (t) => {
316
302
  var e;
317
- t.key === null || t.newValue === t.oldValue || (e = P[t.key]) == null || e.call(P, t);
303
+ t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
318
304
  });
319
- const ye = c.setItem.bind(c), we = c.removeItem.bind(c);
305
+ const we = c.setItem.bind(c), ye = c.removeItem.bind(c);
320
306
  c.setItem = (t, e) => {
321
- N[t] === void 0 && ye.call(c, t, e);
307
+ U[t] === void 0 && we.call(c, t, e);
322
308
  };
323
309
  c.removeItem = (t) => {
324
- N[t] === void 0 && we.call(c, t);
310
+ U[t] === void 0 && ye.call(c, t);
325
311
  };
326
- const Ae = /"exp":\s*(\d+)/, I = "atom\\", H = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
312
+ const Ae = /"exp":\s*(\d+)/, P = "atom\\", H = "atom`s\\", J = {}, ee = {}, te = /[a-z]/gi, R = 5, G = {};
327
313
  for (let t = 54; t < 80; t++) {
328
314
  if (t === 68 || t === 72 || t === 55) continue;
329
315
  const e = String.fromCharCode(t + 43).toUpperCase(), n = String.fromCharCode(t + 43).toLowerCase();
330
- Q[e] = n, Q[n] = e;
316
+ G[e] = n, G[n] = e;
331
317
  }
332
- const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
318
+ const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
333
319
  const t = [
334
320
  (e) => JSON.stringify(e),
335
321
  (e) => btoa(encodeURI(t[0](e))),
@@ -368,12 +354,12 @@ const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
368
354
  setTimeout(() => {
369
355
  Object.keys(c).forEach((t) => {
370
356
  var s;
371
- if (typeof c[t] != "string" || !t.startsWith(I) && !t.startsWith(H)) return;
357
+ if (typeof c[t] != "string" || !t.startsWith(P) && !t.startsWith(H)) return;
372
358
  const e = +((s = c[t].match(Ae)) == null ? void 0 : s[1]);
373
359
  if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
374
360
  const n = z(c[t], 0);
375
361
  !Array.isArray(n) || n[1] == null || !("exp" in n[1]) || n[1].exp !== e || (ee[t] = setTimeout(() => {
376
- G[t] ? G[t].reset() : delete c[t];
362
+ J[t] ? J[t].reset() : delete c[t];
377
363
  }, e * 1e3 - Date.now()));
378
364
  });
379
365
  }, 1e3);
@@ -385,9 +371,9 @@ export {
385
371
  fe as AtomNumberDoActions,
386
372
  he as AtomObjectDoActions,
387
373
  me as AtomSetDoActions,
388
- Me as atom,
374
+ Te as atom,
389
375
  Se as configureAtomaric,
390
- M as configuredOptions,
376
+ T as configuredOptions,
391
377
  $e as useAtom,
392
378
  De as useAtomDo,
393
379
  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 $={},me=t=>{$.useSyncExternalStore=j=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},Y=t=>j(t.subscribe,t.get),Z=t=>t.set,de=t=>t.setDeferred,Ae=t=>t.get,ge=t=>t.do,ye=t=>[Y(t),Z(t)],pe=(t,e)=>new ce(t,e);class L{constructor(e){if(e)return new Proxy(this,{get:(r,s)=>s in this?r[s]:e[s]})}}const we=(t,e)=>{const r=(s,o,i)=>new Proxy(s,{get:(f,m,g)=>{const A=i===0&&e.get!=null?e.get(f,m,g):f[m];return typeof A=="object"&&A!==null?r(Array.isArray(A)?A.slice(0):{...A},o.concat(Array.isArray(f)?+m:m),i+1):A},set:(f,m,g)=>(e.onSet(f,o,m,g,f[m])&&(f[m]=g),!0)});return r(t,[],0)};class k extends L{constructor(){super(...arguments),this.updateValue=(e,r)=>{const s=Array.isArray(e)?e.slice(0):{...e};let o=!1;const i=we(e,{onSet:(f,m,g,A,p)=>{if(A===p)return!0;let b=s;o=!0;for(const h of m){const d=b[h];b=b[h]=Array.isArray(d)?d.slice(0):{...d}}return b[g]=A,!0}});return r(i),o?s:e}}}class O extends k{constructor(e,r){super(r),this.atom=e,this.push=(...s)=>{this.atom.set(this.atom.get().concat(s))},this.unshift=(...s)=>{this.atom.set(s.concat(this.atom.get()))},this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.filter=s=>{this.atom.set(this.atom.get().filter(s??Se))},this.add=s=>{this.atom.get().includes(s)||this.atom.set(this.atom.get().concat([s]))},this.remove=s=>{const o=this.atom.get().indexOf(s);if(o<0)return;const i=this.atom.get().slice(0);i.splice(o,1),this.atom.set(i)},this.toggle=(s,o)=>{const i=this.atom.get().slice(),f=i.indexOf(s);f<0?o?i.unshift(s):i.push(s):i.splice(f,1),this.atom.set(i)}}}const Se=t=>t;class ee extends L{constructor(e,r){super(r),this.atom=e,this.toggle=()=>{this.atom.set(!this.atom.get())}}}class te extends L{constructor(e,r){super(r),this.atom=e,this.setValue=(s,o)=>{const i=new Map(this.atom.get());i.set(s,o),this.atom.set(i)},this.delete=s=>{const o=new Map(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=(s,o)=>{const i=new Map(this.atom.get());i.has(s)?i.delete(s):i.set(s,o),this.atom.set(i)},this.clear=()=>{this.atom.set(new Map)},this.atom=e}}class se extends L{constructor(e,r){super(r),this.atom=e,this.increment=s=>{this.atom.set(+this.atom.get()+(s??1))}}}class ne extends k{constructor(e,r){super(r),this.atom=e,this.setPartial=s=>this.atom.set(o=>({...o,...typeof s=="function"?s(this.atom.get()):s})),this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.setDeepPartial=(s,o,i,f=$.keyPathSeparator||".")=>{if(!f)return;if(s.includes(f)){let A=s.split(f);const p=A[A.length-1];A=A.slice(0,-1);const b={...this.atom.get()};let h=b,d=i;for(const w of A){d=d==null?void 0:d[Array.isArray(d)?"0":w];const M=h[re(h,w)]??(Array.isArray(d)?[]:{});if(M==null||typeof M!="object"){if(i==null)throw"Incorrect path for setDeepPartial";const x=typeof o=="function"?o(void 0):o;this.atom.get()[s]!==x&&this.setPartial({[s]:x});return}h=h[re(h,w)]=Array.isArray(M)?[...M]:{...M}}const q=h[p];h[p]=typeof o=="function"?o(h[p]):o,q!==h[p]&&this.atom.set(b);return}const m=this.atom.get()[s],g=typeof o=="function"?o(m):o;g!==m&&this.setPartial({[s]:g})}}}const re=(t,e)=>Array.isArray(t)?`${+e}`:e;class oe extends L{constructor(e,r){super(r),this.atom=e,this.add=s=>{this.atom.set(new Set(this.atom.get()).add(s))},this.delete=s=>{const o=new Set(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=s=>{const o=new Set(this.atom.get());o.has(s)?o.delete(s):o.add(s),this.atom.set(o)},this.clear=()=>{this.atom.set(new Set)},this.atom=e}}const be=(t,e,r)=>{const s=typeof r=="object"&&r!=null&&"do"in r?r.do((i,f)=>e.set(i,f),()=>e.get(),e,(i,f,m)=>e.setDeferred(i,f,m)):null,o={};return s&&Object.keys(s).forEach(i=>Object.defineProperty(o,i,{get:()=>s[i]})),typeof t=="number"?new se(e,s):typeof t=="boolean"?new ee(e,s):Array.isArray(t)?new O(e,s):t instanceof Set?new oe(e,s):t instanceof Map?new te(e,s):t instanceof Object?new ne(e,s):new L(s)};class ce{constructor(e,r){e=typeof e=="function"?e():e;const s=n=>A=n,o=()=>A,i=new Set,f=n=>n(h());let m=!0,g=!1,A=e,p,b=()=>{},h=()=>o(),d=null,q=()=>{const n=be(e,w,r);return q=()=>n,n};const w=new Proxy(this,{get:(n,a)=>a==="do"?q():n[a],set:De}),M=()=>{m=!0,g!==!0&&b(h()),g=!1;try{B.postMessage({key:u,value:o()})}catch{}},x=(n,a)=>{const y=typeof n=="function"?n(h()):n;y!==h()&&(s(y),g=a,m&&(m=!1,i.forEach(f),queueMicrotask(M)))};this.set=(n,a)=>x(n,a),this.get=()=>h(),this.initialValue=e,this.isInitialValue=()=>e===o(),this.subscribe=n=>(i.add(n),()=>{i.delete(n)}),this.reset=()=>{x(e,!0)};const Te=(n,a)=>{x(n,a),p=void 0};if(this.setDeferred=(n,a=500,y,D=!0)=>{D&&p===void 0&&x(n,y),clearTimeout(p),p=setTimeout(Te,a,n,y)},r==null)return w;let S=null,z=null,W=!0,G=!0,H=!1,T=0,_=0,le=-1,V=e instanceof Set?n=>new Set(n):e instanceof Map?n=>{if(!n||typeof n!="object")return new Map;const a=new Map;for(const y in n)try{a.set(JSON.parse(y),n[y])}catch{}return a}:n=>n,E=e instanceof Set?n=>{if(n instanceof Set)return Array.from(n);throw console.error(n),"The value is not Set instance"}:e instanceof Map?n=>{if(n instanceof Map){const a={};return n.forEach((y,D)=>{try{a[JSON.stringify(D)]=y}catch{}}),a}throw console.error(n),"The value is not Set instance"}:n=>n;if(typeof r=="string")S=r;else if("storeKey"in r)W=r.warnOnDuplicateStoreKey??W,G=r.listenStorageChanges??G,S=r.storeKey,V=r.unzipValue??V,E=r.zipValue??E,H=r.unchangable??H,T=r.securifyKeyLevel??$.securifyKeyLevel??T,_=r.securifyValueLevel??$.securifyValueLevel??_,z=r.exp??z;else return w;const Q=T?I(S,T):S,u=`${_?J:C}${Q}`;if(T){const n=`${C}${S}`;n in c&&(c[u]=c[n],delete c[n])}else{const n=`${C}${I(S,T)}`;n in c&&(c[u]=c[n],delete c[n])}const K=z===null||!(z(w,u in c)instanceof Date)?n=>I([E(n)],0):n=>(d??(d={}),d.exp=z(w,u in c).getTime(),d.exp-Date.now()<24*60*60*1e3&&(clearTimeout(le),clearTimeout(ie[u]),le=setTimeout(()=>this.reset(),d.exp-Date.now())),d.exp=Math.trunc(d.exp/1e3),I([E(n),d],0));if(_){const n=`${C}${Q}`,a=E,y=V;if(E=D=>{try{return I([a(D)],_)}catch{return delete c[u],""}},n in c){const D=`${C}${I(S,T)}`;try{c[D]=K(V(U(c[n],0)[0])),delete c[n]}catch{}}V=D=>{try{return y(U(D,_)[0])}catch{return delete c[u],""}}}else delete c[`${J}${Q}`];const fe=n=>{const a=U(n,0);return d=a[1],V(a[0])};let he=!0;if(v[u]=w,c[`atom/${S}`]&&(c[u]||(c[u]=`[${c[`atom/${S}`]}]`),delete c[`atom/${S}`]),h=()=>{if(h=o,he){he=!1;try{s(u in c?fe(c[u]):e)}catch{console.warn("Invalid json value",c[u])}}return o()},b=n=>{if(n===e){this.reset();return}c[u]=K(n)},this.reset=()=>{delete c[u],x(e,!0)},W&&P[u]!==void 0&&console.warn("Duplicate Atom key",S),G)if(H){let n=!1,a;N[u]=this,P[u]=()=>{clearTimeout(a),a=setTimeout(()=>n=!1,10),!n&&(n=!0,c[u]=K(o()))}}else P[u]=n=>{if(n.newValue===null){this.reset();return}try{x(fe(n.newValue))}catch{console.warn("Invalid json value",n.newValue)}};return w}}let B;try{B=new BroadcastChannel("updateHere"),B.addEventListener("message",t=>{var e;(e=N[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const c=localStorage,P={},N={},De=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=P[t.key])==null||e.call(P,t)});const xe=c.setItem.bind(c),$e=c.removeItem.bind(c);c.setItem=(t,e)=>{N[t]===void 0&&xe.call(c,t,e)},c.removeItem=t=>{N[t]===void 0&&$e.call(c,t)};const Me=/"exp":\s*(\d+)/,C="atom\\",J="atom`s\\",v={},ie={},ae=/[a-z]/gi,R=5,F={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),r=String.fromCharCode(t+43).toLowerCase();F[e]=r,F[r]=e}const ue=t=>F[t]??t,I=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const r=t[1](e);return`${r.slice(0,R)}${r.slice(R).replace(ae,ue)}`},e=>btoa(t[2](e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})(),U=(()=>{const t=[e=>JSON.parse(e),e=>t[0](decodeURI(atob(e))),e=>t[1](`${e.slice(0,R)}${e.slice(R).replace(ae,ue)}`),e=>t[2](atob(e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(c).forEach(t=>{var s;if(typeof c[t]!="string"||!t.startsWith(C)&&!t.startsWith(J))return;const e=+((s=c[t].match(Me))==null?void 0:s[1]);if(!e||e*1e3-Date.now()>24*60*60*1e3)return;const r=U(c[t],0);!Array.isArray(r)||r[1]==null||!("exp"in r[1])||r[1].exp!==e||(ie[t]=setTimeout(()=>{v[t]?v[t].reset():delete c[t]},e*1e3-Date.now()))})},1e3),l.Atom=ce,l.AtomArrayDoActions=O,l.AtomBooleanDoActions=ee,l.AtomMapDoActions=te,l.AtomNumberDoActions=se,l.AtomObjectDoActions=ne,l.AtomSetDoActions=oe,l.atom=pe,l.configureAtomaric=me,l.configuredOptions=$,l.useAtom=ye,l.useAtomDo=ge,l.useAtomGet=Ae,l.useAtomSet=Z,l.useAtomSetDeferred=de,l.useAtomValue=Y,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(l,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(l=typeof globalThis<"u"?globalThis:l||self,j(l.atomaric={}))})(this,function(l){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const $={},me=t=>{$.useSyncExternalStore=j=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},X=t=>j(t.subscribe,t.get),Y=t=>t.set,de=t=>t.setDeferred,Ae=t=>t.get,ge=t=>t.do,ye=t=>[X(t),Y(t)],pe=(t,e)=>new oe(t,e);class L{constructor(e){if(e)return new Proxy(this,{get:(r,s)=>s in this?r[s]:e[s]})}}const we=(t,e)=>{const r=(s,o,i)=>new Proxy(s,{get:(f,m,g)=>{const A=i===0&&e.get!=null?e.get(f,m,g):f[m];return typeof A=="object"&&A!==null?r(Array.isArray(A)?A.slice(0):{...A},o.concat(Array.isArray(f)?+m:m),i+1):A},set:(f,m,g)=>(e.onSet(f,o,m,g,f[m])&&(f[m]=g),!0)});return r(t,[],0)};class Z extends L{constructor(){super(...arguments),this.updateValue=(e,r)=>{const s=Array.isArray(e)?e.slice(0):{...e};let o=!1;const i=we(e,{onSet:(f,m,g,A,y)=>{if(A===y)return!0;let S=s;o=!0;for(const h of m){const d=S[h];S=S[h]=Array.isArray(d)?d.slice(0):{...d}}return S[g]=A,!0}});return r(i),o?s:e}}}class k extends Z{constructor(e,r){super(r),this.atom=e,this.push=(...s)=>{this.atom.set(this.atom.get().concat(s))},this.unshift=(...s)=>{this.atom.set(s.concat(this.atom.get()))},this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.filter=s=>{this.atom.set(this.atom.get().filter(s??Se))},this.add=s=>{this.atom.get().includes(s)||this.atom.set(this.atom.get().concat([s]))},this.remove=s=>{const o=this.atom.get().indexOf(s);if(o<0)return;const i=this.atom.get().slice(0);i.splice(o,1),this.atom.set(i)},this.toggle=(s,o)=>{const i=this.atom.get().slice(),f=i.indexOf(s);f<0?o?i.unshift(s):i.push(s):i.splice(f,1),this.atom.set(i)}}}const Se=t=>t;class O extends L{constructor(e,r){super(r),this.atom=e,this.toggle=()=>{this.atom.set(!this.atom.get())}}}class ee extends L{constructor(e,r){super(r),this.atom=e,this.setValue=(s,o)=>{const i=new Map(this.atom.get());i.set(s,o),this.atom.set(i)},this.delete=s=>{const o=new Map(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=(s,o)=>{const i=new Map(this.atom.get());i.has(s)?i.delete(s):i.set(s,o),this.atom.set(i)},this.clear=()=>{this.atom.set(new Map)},this.atom=e}}class te extends L{constructor(e,r){super(r),this.atom=e,this.increment=s=>{this.atom.set(+this.atom.get()+(s??1))}}}class se extends Z{constructor(e,r){super(r),this.atom=e,this.setPartial=s=>this.atom.set(o=>({...o,...typeof s=="function"?s(this.atom.get()):s})),this.update=s=>{const o=this.atom.get(),i=this.updateValue(o,s);i!==o&&this.atom.set(i)},this.setDeepPartial=(s,o,i,f=$.keyPathSeparator||".")=>{if(!f)return;if(s.includes(f)){let A=s.split(f);const y=A[A.length-1];A=A.slice(0,-1);const S={...this.atom.get()};let h=S,d=i;for(const p of A){d=d==null?void 0:d[Array.isArray(d)?"0":p];const T=h[ne(h,p)]??(Array.isArray(d)?[]:{});if(T==null||typeof T!="object"){if(i==null)throw"Incorrect path for setDeepPartial";const b=typeof o=="function"?o(void 0):o;this.atom.get()[s]!==b&&this.setPartial({[s]:b});return}h=h[ne(h,p)]=Array.isArray(T)?[...T]:{...T}}const B=h[y];h[y]=typeof o=="function"?o(h[y]):o,B!==h[y]&&this.atom.set(S);return}const m=this.atom.get()[s],g=typeof o=="function"?o(m):o;g!==m&&this.setPartial({[s]:g})}}}const ne=(t,e)=>Array.isArray(t)?`${+e}`:e;class re extends L{constructor(e,r){super(r),this.atom=e,this.add=s=>{this.atom.set(new Set(this.atom.get()).add(s))},this.delete=s=>{const o=new Set(this.atom.get());o.delete(s),this.atom.set(o)},this.toggle=s=>{const o=new Set(this.atom.get());o.has(s)?o.delete(s):o.add(s),this.atom.set(o)},this.clear=()=>{this.atom.set(new Set)},this.atom=e}}const be=(t,e,r)=>{const s=typeof r=="object"&&r!=null&&"do"in r?r.do((i,f)=>e.set(i,f),()=>e.get(),e,(i,f,m)=>e.setDeferred(i,f,m)):null,o={};return s&&Object.keys(s).forEach(i=>Object.defineProperty(o,i,{get:()=>s[i]})),typeof t=="number"?new te(e,s):typeof t=="boolean"?new O(e,s):Array.isArray(t)?new k(e,s):t instanceof Set?new re(e,s):t instanceof Map?new ee(e,s):t instanceof Object?new se(e,s):new L(s)};class oe{constructor(e,r){e=typeof e=="function"?e():e;const s=n=>A=n,o=()=>A,i=new Set,f=n=>n(h());let m=!0,g=!1,A=e,y,S=()=>{},h=()=>o(),d=null,B=()=>{const n=be(e,p,r);return B=()=>n,n};const p=new Proxy(this,{get:(n,u)=>u==="do"?B():n[u],set:De}),T=()=>{m=!0,g!==!0&&S(h()),g=!1;try{N.postMessage({key:a,value:o()})}catch{}},b=(n,u)=>{const D=typeof n=="function"?n(h()):n;D!==h()&&(s(D),g=u,m&&(m=!1,i.forEach(f),queueMicrotask(T)))};this.set=(n,u)=>b(n,u),this.get=()=>h(),this.initialValue=e,this.isInitialValue=()=>e===o(),this.subscribe=n=>(i.add(n),()=>{i.delete(n)}),this.reset=()=>{b(e,!0)};const Me=(n,u)=>{b(n,u),y=void 0};if(this.setDeferred=(n,u=500,D,x=!0)=>{x&&y===void 0&&b(n,D),clearTimeout(y),y=setTimeout(Me,u,n,D)},r==null)return p;let w=null,z=null,G=!0,H=!0,J=!1,M=0,V=0,ue=-1,_=e instanceof Set||e instanceof Map?n=>new Set(n):n=>n,E=e instanceof Set?n=>{if(n instanceof Set)return Array.from(n);throw console.error(n),"The value is not Set instance"}:e instanceof Map?n=>{if(n instanceof Map){const u=[];return n.forEach((D,x)=>u.push([x,D])),u}throw console.error(n),"The value is not Set instance"}:n=>n;if(typeof r=="string")w=r;else if("storeKey"in r)G=r.warnOnDuplicateStoreKey??G,H=r.listenStorageChanges??H,w=r.storeKey,_=r.unzipValue??_,E=r.zipValue??E,J=r.unchangable??J,M=r.securifyKeyLevel??$.securifyKeyLevel??M,V=r.securifyValueLevel??$.securifyValueLevel??V,z=r.exp??z;else return p;const Q=M?I(w,M):w,a=`${V?v:C}${Q}`;if(M){const n=`${C}${w}`;n in c&&(c[a]=c[n],delete c[n])}else{const n=`${C}${I(w,M)}`;n in c&&(c[a]=c[n],delete c[n])}const K=z===null||!(z(p,a in c)instanceof Date)?n=>I([E(n)],0):n=>(d??(d={}),d.exp=z(p,a in c).getTime(),d.exp-Date.now()<24*60*60*1e3&&(clearTimeout(ue),clearTimeout(ce[a]),ue=setTimeout(()=>this.reset(),d.exp-Date.now())),d.exp=Math.trunc(d.exp/1e3),I([E(n),d],0));if(V){const n=`${C}${Q}`,u=E,D=_;if(E=x=>{try{return I([u(x)],V)}catch{return delete c[a],""}},n in c){const x=`${C}${I(w,M)}`;try{c[x]=K(_(q(c[n],0)[0])),delete c[n]}catch{}}_=x=>{try{return D(q(x,V)[0])}catch{return delete c[a],""}}}else delete c[`${v}${Q}`];const le=n=>{const u=q(n,0);return d=u[1],_(u[0])};let fe=!0;if(F[a]=p,c[`atom/${w}`]&&(c[a]||(c[a]=`[${c[`atom/${w}`]}]`),delete c[`atom/${w}`]),h=()=>{if(h=o,fe){fe=!1;try{s(a in c?le(c[a]):e)}catch{console.warn("Invalid json value",c[a])}}return o()},S=n=>{if(n===e){this.reset();return}c[a]=K(n)},this.reset=()=>{delete c[a],b(e,!0)},G&&P[a]!==void 0&&console.warn("Duplicate Atom key",w),H)if(J){let n=!1,u;R[a]=this,P[a]=()=>{clearTimeout(u),u=setTimeout(()=>n=!1,10),!n&&(n=!0,c[a]=K(o()))}}else P[a]=n=>{if(n.newValue===null){this.reset();return}try{b(le(n.newValue))}catch{console.warn("Invalid json value",n.newValue)}};return p}}let N;try{N=new BroadcastChannel("updateHere"),N.addEventListener("message",t=>{var e;(e=R[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const c=localStorage,P={},R={},De=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=P[t.key])==null||e.call(P,t)});const xe=c.setItem.bind(c),$e=c.removeItem.bind(c);c.setItem=(t,e)=>{R[t]===void 0&&xe.call(c,t,e)},c.removeItem=t=>{R[t]===void 0&&$e.call(c,t)};const Te=/"exp":\s*(\d+)/,C="atom\\",v="atom`s\\",F={},ce={},ie=/[a-z]/gi,U=5,W={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),r=String.fromCharCode(t+43).toLowerCase();W[e]=r,W[r]=e}const ae=t=>W[t]??t,I=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const r=t[1](e);return`${r.slice(0,U)}${r.slice(U).replace(ie,ae)}`},e=>btoa(t[2](e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})(),q=(()=>{const t=[e=>JSON.parse(e),e=>t[0](decodeURI(atob(e))),e=>t[1](`${e.slice(0,U)}${e.slice(U).replace(ie,ae)}`),e=>t[2](atob(e))];return(e,r)=>{try{return t[r](e)}catch(s){if(r===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(c).forEach(t=>{var s;if(typeof c[t]!="string"||!t.startsWith(C)&&!t.startsWith(v))return;const e=+((s=c[t].match(Te))==null?void 0:s[1]);if(!e||e*1e3-Date.now()>24*60*60*1e3)return;const r=q(c[t],0);!Array.isArray(r)||r[1]==null||!("exp"in r[1])||r[1].exp!==e||(ce[t]=setTimeout(()=>{F[t]?F[t].reset():delete c[t]},e*1e3-Date.now()))})},1e3),l.Atom=oe,l.AtomArrayDoActions=k,l.AtomBooleanDoActions=O,l.AtomMapDoActions=ee,l.AtomNumberDoActions=te,l.AtomObjectDoActions=se,l.AtomSetDoActions=re,l.atom=pe,l.configureAtomaric=me,l.configuredOptions=$,l.useAtom=ye,l.useAtomDo=ge,l.useAtomGet=Ae,l.useAtomSet=Y,l.useAtomSetDeferred=de,l.useAtomValue=X,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "atomaric",
3
3
  "description": "Manage your project state",
4
- "version": "0.0.70",
4
+ "version": "0.0.71",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
@@ -8,30 +8,28 @@ configureAtomaric({
8
8
  });
9
9
 
10
10
  describe('Map', () => {
11
- test('do actions', async () => {
12
- type Value = number | { asasa: '' };
13
- const testAtom = atom(new Map<string, Value>(), {
14
- storeKey: 'map:test',
15
- do: (set, get) => ({
16
- filterKeyValues: () => {
17
- const newMap = new Map();
18
- get().forEach((value, key) => {
19
- if (value) newMap.set(key, value);
20
- });
21
- set(newMap);
22
- },
23
- }),
24
- });
11
+ type Value = number | { asasa: '' };
12
+ const testAtom = atom(new Map<string, Value>(), {
13
+ storeKey: 'map:test',
14
+ do: (set, get) => ({
15
+ filterKeyValues: () => {
16
+ const newMap = new Map();
17
+ get().forEach((value, key) => {
18
+ if (value) newMap.set(key, value);
19
+ });
20
+ set(newMap);
21
+ },
22
+ }),
23
+ });
25
24
 
25
+ test('do.setValue()', async () => {
26
26
  testAtom.do.setValue('!', 1);
27
27
  testAtom.do.setValue('@', 0);
28
28
  testAtom.do.setValue('#', { asasa: '' });
29
29
  testAtom.do.setValue('', 123);
30
30
  await wait();
31
31
 
32
- expect(localStorage[makeFullKey('map:test')]).toEqual(
33
- '[{"\\"!\\"":1,"\\"@\\"":0,"\\"#\\"":{"asasa":""},"\\"\\"":123}]',
34
- );
32
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["@",0],["#",{"asasa":""}],["",123]]]');
35
33
 
36
34
  expect(testAtom.get()).toEqual(
37
35
  new Map<string, Value>([
@@ -41,31 +39,39 @@ describe('Map', () => {
41
39
  ['', 123],
42
40
  ]),
43
41
  );
42
+ });
44
43
 
44
+ test('do.<filterKeyValues>()', async () => {
45
45
  testAtom.do.filterKeyValues();
46
46
  await wait();
47
47
 
48
- expect(localStorage[makeFullKey('map:test')]).toEqual('[{"\\"!\\"":1,"\\"#\\"":{"asasa":""},"\\"\\"":123}]');
48
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["#",{"asasa":""}],["",123]]]');
49
+ });
49
50
 
51
+ test('do.delete()', async () => {
50
52
  testAtom.do.delete('#');
51
53
  await wait();
52
54
 
53
- expect(localStorage[makeFullKey('map:test')]).toEqual('[{"\\"!\\"":1,"\\"\\"":123}]');
55
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123]]]');
56
+ });
54
57
 
58
+ test('do.toggle()', async () => {
55
59
  testAtom.do.toggle('#', 555);
56
60
  await wait();
57
61
 
58
- expect(localStorage[makeFullKey('map:test')]).toEqual('[{"\\"!\\"":1,"\\"\\"":123,"\\"#\\"":555}]');
62
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123],["#",555]]]');
59
63
 
60
64
  testAtom.do.toggle('', 999);
61
65
  testAtom.do.toggle('!', 1234567890);
62
66
  await wait();
63
67
 
64
- expect(localStorage[makeFullKey('map:test')]).toEqual('[{"\\"#\\"":555}]');
68
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[["#",555]]]');
69
+ });
65
70
 
71
+ test('do.clear()', async () => {
66
72
  testAtom.do.clear();
67
73
  await wait();
68
74
 
69
- expect(localStorage[makeFullKey('map:test')]).toEqual('[{}]');
75
+ expect(localStorage[makeFullKey('map:test')]).toEqual('[[]]');
70
76
  });
71
77
  });