atomaric 0.0.75 → 0.0.77

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,10 +1,10 @@
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), 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
- class _ {
4
+ const _ = {}, Se = (t) => {
5
+ _.useSyncExternalStore = k = t.useSyncExternalStore, _.keyPathSeparator = t.keyPathSeparator, _.securifyKeyLevel = t.securifyKeyLevel, _.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)], _e = (t, e) => new pe(t, e);
7
+ class V {
8
8
  constructor(e) {
9
9
  if (e)
10
10
  return new Proxy(this, {
@@ -26,7 +26,7 @@ const ce = (t, e) => {
26
26
  });
27
27
  return i(t, [], 0);
28
28
  };
29
- class O extends _ {
29
+ class O extends V {
30
30
  constructor() {
31
31
  super(...arguments), this.updateValue = (e, i) => {
32
32
  const s = Array.isArray(e) ? e.slice(0) : { ...e };
@@ -73,14 +73,14 @@ class oe extends O {
73
73
  }
74
74
  }
75
75
  const ae = (t) => t;
76
- class ue extends _ {
76
+ class ue extends V {
77
77
  constructor(e, i) {
78
78
  super(i), this.a = e, this.toggle = () => {
79
79
  this.a.set(!this.a.get());
80
80
  };
81
81
  }
82
82
  }
83
- class le extends _ {
83
+ class le extends V {
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;
@@ -102,7 +102,7 @@ class le extends _ {
102
102
  }, this.a = e;
103
103
  }
104
104
  }
105
- class fe extends _ {
105
+ class fe extends V {
106
106
  constructor(e, i) {
107
107
  super(i), this.a = e, this.increment = (s) => {
108
108
  this.a.set(+this.a.get() + (s ?? 1));
@@ -117,7 +117,7 @@ class he extends O {
117
117
  })), this.update = (s) => {
118
118
  const n = this.a.get(), c = this.updateValue(n, s);
119
119
  c !== n && this.a.set(c);
120
- }, this.setDeepPartial = (s, n, c, l = M.keyPathSeparator || ".") => {
120
+ }, this.setDeepPartial = (s, n, c, l = _.keyPathSeparator || ".") => {
121
121
  if (!l) return;
122
122
  if (s.includes(l)) {
123
123
  let g = s.split(l);
@@ -146,7 +146,7 @@ class he extends O {
146
146
  }
147
147
  }
148
148
  const v = (t, e) => Array.isArray(t) ? `${+e}` : e;
149
- class de extends _ {
149
+ class de extends V {
150
150
  constructor(e, i) {
151
151
  super(i), this.a = e, this.add = (s) => {
152
152
  this.a.get().has(s) || this.a.set(new Set(this.a.get()).add(s));
@@ -169,7 +169,7 @@ const ge = (t, e, i) => {
169
169
  e,
170
170
  (c, l, h) => e.setDeferred(c, l, h)
171
171
  ) : null, n = {};
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);
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 V(s);
173
173
  };
174
174
  class pe {
175
175
  constructor(e, i) {
@@ -204,7 +204,7 @@ class pe {
204
204
  if (this.setDeferred = (r, u = 500, x, b = !0) => {
205
205
  b && w === void 0 && S(r, x), clearTimeout(w), w = setTimeout(re, u, r, x);
206
206
  }, i == null) return y;
207
- let A = null, j = null, q = !0, F = !0, N = !1, $ = 0, T = 0, K = -1, I = e instanceof Set ? (r) => new Set(r) : e instanceof Map ? (r) => new Map(r) : (r) => r, L = e instanceof Set ? (r) => {
207
+ let A = null, j = null, q = !0, F = !0, N = !1, $ = 0, M = 0, K = -1, T = e instanceof Set ? (r) => new Set(r) : e instanceof Map ? (r) => new Map(r) : (r) => r, I = e instanceof Set ? (r) => {
208
208
  if (r instanceof Set) return Array.from(r);
209
209
  throw console.error(r), "The value is not Set instance";
210
210
  } : e instanceof Map ? (r) => {
@@ -217,35 +217,35 @@ class pe {
217
217
  if (typeof i == "string")
218
218
  A = i;
219
219
  else if ("storeKey" in i)
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;
220
+ q = i.warnOnDuplicateStoreKey ?? q, F = i.listenStorageChanges ?? F, A = i.storeKey, T = i.unzipValue ?? T, I = i.zipValue ?? I, N = i.unchangable ?? N, $ = i.securifyKeyLevel ?? _.securifyKeyLevel ?? $, M = i.securifyValueLevel ?? _.securifyValueLevel ?? M, j = i.exp ?? j;
221
221
  else return y;
222
- const B = $ ? C(A, $) : A, a = `${T ? J : P}${B}`;
222
+ const B = $ ? L(A, $) : A, a = `${M ? J : C}${B}`;
223
223
  if ($) {
224
- const r = `${P}${A}`;
224
+ const r = `${C}${A}`;
225
225
  r in o && (o[a] = o[r], delete o[r]);
226
226
  } else {
227
- const r = `${P}${C(A, $)}`;
227
+ const r = `${C}${L(A, $)}`;
228
228
  r in o && (o[a] = o[r], delete o[r]);
229
229
  }
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
+ const W = j === null || !(j(y, a in o) instanceof Date) ? (r) => L([I(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), L([I(r), d], 0));
231
+ if (M) {
232
+ const r = `${C}${B}`, u = I, x = T;
233
+ if (I = (b) => {
234
234
  try {
235
- return C([u(b)], T);
235
+ return L([u(b)], M);
236
236
  } catch {
237
237
  return delete o[a], "";
238
238
  }
239
239
  }, r in o) {
240
- const b = `${P}${C(A, $)}`;
240
+ const b = `${C}${L(A, $)}`;
241
241
  try {
242
- o[b] = W(I(z(o[r], 0)[0])), delete o[r];
242
+ o[b] = W(T(z(o[r], 0)[0])), delete o[r];
243
243
  } catch {
244
244
  }
245
245
  }
246
- I = (b) => {
246
+ T = (b) => {
247
247
  try {
248
- return x(z(b, T)[0]);
248
+ return x(z(b, M)[0]);
249
249
  } catch {
250
250
  return delete o[a], "";
251
251
  }
@@ -253,7 +253,7 @@ class pe {
253
253
  } else delete o[`${J}${B}`];
254
254
  const X = (r) => {
255
255
  const u = z(r, 0);
256
- return d = u[1], I(u[0]);
256
+ return d = u[1], T(u[0]);
257
257
  };
258
258
  let Y = !0;
259
259
  if (G[a] = y, o[`atom/${A}`] && (o[a] || (o[a] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => {
@@ -274,14 +274,14 @@ class pe {
274
274
  o[a] = W(r);
275
275
  }, this.reset = () => {
276
276
  delete o[a], S(e, !0);
277
- }, q && V[a] !== void 0 && console.warn("Duplicate Atom key", A), F)
277
+ }, q && P[a] !== void 0 && console.warn("Duplicate Atom key", A), F)
278
278
  if (N) {
279
279
  let r = !1, u;
280
- U[a] = this, V[a] = () => {
280
+ U[a] = this, P[a] = () => {
281
281
  clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, o[a] = W(n()));
282
282
  };
283
283
  } else
284
- V[a] = (r) => {
284
+ P[a] = (r) => {
285
285
  if (r.newValue === null) {
286
286
  this.reset();
287
287
  return;
@@ -303,12 +303,12 @@ try {
303
303
  });
304
304
  } catch {
305
305
  }
306
- const o = localStorage, V = {}, U = {}, we = (t, e) => {
306
+ const o = localStorage, P = {}, U = {}, we = (t, e) => {
307
307
  throw `${e} is readonly property`;
308
308
  };
309
309
  window.addEventListener("storage", (t) => {
310
310
  var e;
311
- t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
311
+ t.key === null || t.newValue === t.oldValue || (e = P[t.key]) == null || e.call(P, t);
312
312
  });
313
313
  const ye = o.setItem.bind(o), Ae = o.removeItem.bind(o);
314
314
  o.setItem = (t, e) => {
@@ -317,13 +317,13 @@ o.setItem = (t, e) => {
317
317
  o.removeItem = (t) => {
318
318
  U[t] === void 0 && Ae.call(o, t);
319
319
  };
320
- const me = /"exp":\s*(\d+)/, P = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
320
+ const me = /"exp":\s*(\d+)/, C = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
321
321
  for (let t = 54; t < 80; t++) {
322
322
  if (t === 68 || t === 72 || t === 55) continue;
323
323
  const e = String.fromCharCode(t + 43).toUpperCase(), i = String.fromCharCode(t + 43).toLowerCase();
324
324
  Q[e] = i, Q[i] = e;
325
325
  }
326
- const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
326
+ const se = (t) => Q[t] ?? t, L = /* @__PURE__ */ (() => {
327
327
  const t = [
328
328
  (e) => JSON.stringify(e),
329
329
  (e) => btoa(encodeURI(t[0](e))),
@@ -362,13 +362,13 @@ const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
362
362
  setTimeout(() => {
363
363
  Object.keys(o).forEach((t) => {
364
364
  var s;
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]);
367
- if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
365
+ if (typeof o[t] != "string" || !t.startsWith(C) && !t.startsWith(J)) return;
366
+ const e = (s = o[t].match(me)) == null ? void 0 : s[1];
367
+ if (!e || +e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
368
368
  const i = z(o[t], 0);
369
369
  !Array.isArray(i) || i[1] == null || !("exp" in i[1]) || i[1].exp !== e || (ee[t] = setTimeout(() => {
370
370
  G[t] ? G[t].reset() : delete o[t];
371
- }, e * 1e3 - Date.now()));
371
+ }, +e * 1e3 - Date.now()));
372
372
  });
373
373
  }, 1e3);
374
374
  export {
@@ -379,9 +379,9 @@ export {
379
379
  fe as AtomNumberDoActions,
380
380
  he as AtomObjectDoActions,
381
381
  de as AtomSetDoActions,
382
- Me as atom,
382
+ _e as atom,
383
383
  Se as configureAtomaric,
384
- M as configuredOptions,
384
+ _ as configuredOptions,
385
385
  $e as useAtom,
386
386
  De as useAtomDo,
387
387
  be as useAtomGet,
@@ -1 +1 @@
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?n=>new Set(n):e instanceof Map?n=>new Map(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"})});
1
+ (function(f,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(f=typeof globalThis<"u"?globalThis:f||self,_(f.atomaric={}))})(this,function(f){"use strict";let _=()=>{throw"call configureAtomaric() before all!"};const $={},de=t=>{$.useSyncExternalStore=_=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},Y=t=>_(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 j{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 j{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 j{constructor(e,i){super(i),this.a=e,this.toggle=()=>{this.a.set(!this.a.get())}}}class ee extends j{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 j{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 j{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 j(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,C=0,ue=-1,V=e instanceof Set?n=>new Set(n):e instanceof Map?n=>new Map(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,V=i.unzipValue??V,E=i.zipValue??E,Q=i.unchangable??Q,T=i.securifyKeyLevel??$.securifyKeyLevel??T,C=i.securifyValueLevel??$.securifyValueLevel??C,z=i.exp??z;else return w;const K=T?P(y,T):y,a=`${C?F:L}${K}`;if(T){const n=`${L}${y}`;n in c&&(c[a]=c[n],delete c[n])}else{const n=`${L}${P(y,T)}`;n in c&&(c[a]=c[n],delete c[n])}const X=z===null||!(z(w,a in c)instanceof Date)?n=>P([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),P([E(n),g],0));if(C){const n=`${L}${K}`,u=E,D=V;if(E=x=>{try{return P([u(x)],C)}catch{return delete c[a],""}},n in c){const x=`${L}${P(y,T)}`;try{c[x]=X(V(U(c[n],0)[0])),delete c[n]}catch{}}V=x=>{try{return D(U(x,C)[0])}catch{return delete c[a],""}}}else delete c[`${F}${K}`];const fe=n=>{const u=U(n,0);return g=u[1],V(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&&I[a]!==void 0&&console.warn("Duplicate Atom key",y),J)if(Q){let n=!1,u;R[a]=this,I[a]=()=>{clearTimeout(u),u=setTimeout(()=>n=!1,10),!n&&(n=!0,c[a]=X(r()))}}else I[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,I={},R={},De=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=I[t.key])==null||e.call(I,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+)/,L="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,P=(()=>{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(L)&&!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.75",
4
+ "version": "0.0.77",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
@@ -15,8 +15,7 @@
15
15
  },
16
16
  "files": [
17
17
  "build",
18
- "types",
19
- "src/do.classes"
18
+ "types"
20
19
  ],
21
20
  "keywords": [
22
21
  "react",
@@ -27,7 +26,8 @@
27
26
  "start": "vite --port 8378 --host",
28
27
  "build": "tsc && vite build",
29
28
  "preview": "vite preview",
30
- "test": "jest"
29
+ "test": "jest",
30
+ "lint": "tsc --project ./tsconfig.json --noEmit && eslint **/*.{ts,tsx,json} *.*"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@testing-library/jest-dom": "^6.9.1",
@@ -44,6 +44,7 @@
44
44
  "react": "^19.1.0",
45
45
  "react-dom": "^19.1.1",
46
46
  "ts-jest": "^29.4.6",
47
+ "typescript-eslint": "^8.52.0",
47
48
  "vite": "^6.3.1",
48
49
  "vite-plugin-eslint": "^1.8.1"
49
50
  },
@@ -0,0 +1,24 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ export interface IAtomArrayDoActions<Value> {
4
+ /** like the Array.prototype.push() method */
5
+ push: (...values: Value[]) => void;
6
+
7
+ /** like the Array.prototype.unshift() method */
8
+ unshift: (...values: Value[]) => void;
9
+
10
+ /** transform current taken value */
11
+ update: (updater: (value: Value[]) => void) => void;
12
+
13
+ /** like the Array.prototype.filter() method, but callback is optional - (it) => !!it */
14
+ filter: (filter?: (value: Value, index: number, Array: Value[]) => any) => void;
15
+
16
+ /** will add value if not exists */
17
+ add: (value: Value) => void;
18
+
19
+ /** will delete value from array */
20
+ removeFirst: (value: Value) => void;
21
+
22
+ /** will add value if it doesn't exist, otherwise delete */
23
+ toggle: (value: Value, isAddToStart?: boolean) => void;
24
+ }
@@ -0,0 +1,4 @@
1
+ export interface IAtomBooleanDoActions {
2
+ /** toggle current value between true/false */
3
+ toggle: () => void;
4
+ }
@@ -0,0 +1,22 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ export interface IAtomMapDoActions<
4
+ MapValue extends Map<any, any>,
5
+ Key extends MapValue extends Map<infer K, any> ? K : never,
6
+ Value extends MapValue extends Map<any, infer V> ? V : never,
7
+ > {
8
+ /** like the Map.prototype.set() method, when value is new for key in current atom value */
9
+ setValue: (key: Key, value: Value) => void;
10
+
11
+ /** like the Map.prototype.set() method, when key is not exists */
12
+ setIfNo: (key: Key, value: Value) => void;
13
+
14
+ /** like the Map.prototype.delete() method */
15
+ delete: (key: Key) => void;
16
+
17
+ /** will add value if it doesn't exist, otherwise delete */
18
+ toggle: (key: Key, value: Value) => void;
19
+
20
+ /** like the Map.prototype.clear() method */
21
+ clear: () => void;
22
+ }
@@ -0,0 +1,6 @@
1
+ export interface IAtomNumberDoActions {
2
+ /** pass the 2 to increment on 2, pass the -2 to decrement on 2
3
+ * **default: 1**
4
+ */
5
+ increment: (delta?: number) => void;
6
+ }
@@ -0,0 +1,12 @@
1
+ import { ObjectActionsSetDeepPartialDoAction } from '..';
2
+
3
+ export interface IAtomObjectDoActions<Value extends object> {
4
+ /** pass partial object to update some field values */
5
+ setPartial: (value: Partial<Value> | ((value: Value) => Partial<Value>)) => void;
6
+
7
+ /** transform current taken value */
8
+ update: (updater: (value: Value) => void) => void;
9
+
10
+ /** pass partial value to update some deep values by flat path */
11
+ setDeepPartial: ObjectActionsSetDeepPartialDoAction<Value>;
12
+ }
@@ -0,0 +1,13 @@
1
+ export interface IAtomSetDoActions<Value> {
2
+ /** like the Set.prototype.add() method */
3
+ add: (value: Value) => void;
4
+
5
+ /** like the Set.prototype.delete() method */
6
+ delete: (value: Value) => void;
7
+
8
+ /** will add value if it doesn't exist, otherwise delete */
9
+ toggle: (value: Value) => void;
10
+
11
+ /** like the Set.prototype.clear() method */
12
+ clear: () => void;
13
+ }
package/types/index.d.ts CHANGED
@@ -1,14 +1,16 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import { useSyncExternalStore } from 'react';
2
- import { AtomArrayDoActions } from '../src/do.classes/Array';
3
- import { AtomBooleanDoActions } from '../src/do.classes/Boolean';
4
- import { AtomMapDoActions } from '../src/do.classes/Map';
5
- import { AtomNumberDoActions } from '../src/do.classes/Number';
6
- import { AtomObjectDoActions } from '../src/do.classes/Object';
7
- import { AtomSetDoActions } from '../src/do.classes/Set';
3
+ import { IAtomArrayDoActions } from './do.classes.model/Array';
4
+ import { IAtomBooleanDoActions } from './do.classes.model/Boolean';
5
+ import { IAtomMapDoActions } from './do.classes.model/Map';
6
+ import { IAtomNumberDoActions } from './do.classes.model/Number';
7
+ import { IAtomObjectDoActions } from './do.classes.model/Object';
8
+ import { IAtomSetDoActions } from './do.classes.model/Set';
8
9
  import { Path, PathValue, PathValueDonor } from './paths';
9
10
 
10
11
  export type AtomSecureLevel = 0 | 1 | 2 | 3;
11
12
 
13
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
12
14
  export interface Register {}
13
15
 
14
16
  export type ObjectActionsSetDeepPartialSeparator = Register extends {
@@ -21,7 +23,7 @@ type Sunscriber<Value> = (value: Value) => void;
21
23
 
22
24
  export type AtomStoreKey = `${string}${string}:${string}${string}`;
23
25
 
24
- export type AtomOptions<Value, Actions extends Record<string, Function> = {}> = {
26
+ export type AtomOptions<Value, Actions extends Record<string, AnyFunc> = Record<string, AnyFunc>> = {
25
27
  /** **default: true** */
26
28
  warnOnDuplicateStoreKey?: boolean;
27
29
  /** will update value if localStorage value is changed
@@ -78,20 +80,20 @@ export type ObjectActionsSetDeepPartialDoAction<Value> = <
78
80
  ) => void;
79
81
 
80
82
  export type DefaultActions<Value> = Value extends Set<infer Val>
81
- ? AtomSetDoActions<Val>
83
+ ? IAtomSetDoActions<Val>
82
84
  : Value extends Map<infer Key, infer Val>
83
- ? AtomMapDoActions<Value, Key, Val>
85
+ ? IAtomMapDoActions<Value, Key, Val>
84
86
  : Value extends boolean
85
- ? AtomBooleanDoActions
87
+ ? IAtomBooleanDoActions
86
88
  : Value extends (infer Val)[]
87
- ? AtomArrayDoActions<Val>
89
+ ? IAtomArrayDoActions<Val>
88
90
  : Value extends number
89
- ? AtomNumberDoActions
91
+ ? IAtomNumberDoActions
90
92
  : Value extends object
91
- ? AtomObjectDoActions<Value>
92
- : {};
93
+ ? IAtomObjectDoActions<Value>
94
+ : object;
93
95
 
94
- declare class Atom<Value, Actions extends Record<string, Function> = {}> {
96
+ declare class Atom<Value, Actions extends Record<string, AnyFunc> = Record<string, AnyFunc>> {
95
97
  constructor(initialValue: Value | (() => Value), storeKeyOrOptions: StoreKeyOrOptions<Value, Actions> | undefined);
96
98
 
97
99
  /** initial value */
@@ -122,15 +124,15 @@ export function useAtomSetDeferred<Value>(atom: Atom<Value>): AtomSetDeferredMet
122
124
  export function useAtomGet<Value>(atom: Atom<Value>): () => Value;
123
125
 
124
126
  /** get your custom actions */
125
- export function useAtomDo<Value, Actions extends Record<string, Function> = {}>(
127
+ export function useAtomDo<Value, Actions extends Record<string, AnyFunc> = Record<string, AnyFunc>>(
126
128
  atom: Atom<Value, Actions>,
127
129
  ): Actions & DefaultActions<Value>;
128
130
 
129
- export type StoreKeyOrOptions<Value, Actions extends Record<string, Function> = {}> =
131
+ export type StoreKeyOrOptions<Value, Actions extends Record<string, AnyFunc> = Record<string, AnyFunc>> =
130
132
  | AtomStoreKey
131
133
  | AtomOptions<Value, Actions>;
132
134
 
133
- export function atom<Value, Actions extends Record<string, Function> = {}>(
135
+ export function atom<Value, Actions extends Record<string, AnyFunc> = Record<string, AnyFunc>>(
134
136
  value: Value | (() => Value),
135
137
  storeKeyOrOptions?: StoreKeyOrOptions<Value, Actions>,
136
138
  ): Atom<Value, Actions>;
package/types/paths.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  export type Path<T, Sep extends string> = T extends any ? PathInternal<T, Sep> : never;
2
3
  export type PathValue<T, Sep extends string, ValuePath extends Path<T, Sep>> = TPathValue<T, Sep, ValuePath>;
3
4
 
@@ -1,103 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Array', () => {
11
- test('simple init', async () => {
12
- const initArray: number[] = [];
13
- const testAtom = atom(initArray);
14
- testAtom.set([]);
15
-
16
- expect(testAtom.get() !== initArray).toBeTruthy();
17
- });
18
-
19
- test('do actions', async () => {
20
- const testAtom = atom((): (number | nil | string | { nums: number[] })[] => [], {
21
- do: (_set, _get, self) => ({
22
- switch30(toggleValue: string) {
23
- self.do.toggle(30);
24
- self.do.toggle(toggleValue);
25
- },
26
- }),
27
- });
28
-
29
- testAtom.do.push(1, 2, 5, 2, 5, 5, 5, 5, 1, null, '', '#');
30
- await wait();
31
-
32
- expect(testAtom.get()).toEqual([1, 2, 5, 2, 5, 5, 5, 5, 1, null, '', '#']);
33
-
34
- testAtom.do.add(0);
35
- testAtom.do.add(5);
36
- testAtom.do.add(8);
37
- await wait();
38
-
39
- expect(testAtom.get()).toEqual([1, 2, 5, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8]);
40
-
41
- testAtom.do.removeFirst(5);
42
- await wait();
43
-
44
- expect(testAtom.get()).toEqual([1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8]);
45
-
46
- testAtom.do.toggle('+');
47
- await wait();
48
-
49
- expect(testAtom.get()).toEqual([1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8, '+']);
50
-
51
- testAtom.do.toggle('+');
52
- await wait();
53
-
54
- expect(testAtom.get()).toEqual([1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8]);
55
-
56
- testAtom.do.switch30('@');
57
- await wait();
58
-
59
- expect(testAtom.get()).toEqual([1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8, 30, '@']);
60
-
61
- testAtom.do.unshift('**');
62
- await wait();
63
-
64
- expect(testAtom.get()).toEqual(['**', 1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8, 30, '@']);
65
-
66
- testAtom.do.filter();
67
- await wait();
68
-
69
- expect(testAtom.get()).toEqual(['**', 1, 2, 2, 5, 5, 5, 5, 1, '#', 8, 30, '@']);
70
-
71
- testAtom.do.filter(val => typeof val === 'string');
72
- await wait();
73
-
74
- expect(testAtom.get()).toEqual(['**', '#', '@']);
75
-
76
- testAtom.do.update(val => val.reverse());
77
- await wait();
78
-
79
- expect(testAtom.get()).toEqual(['@', '#', '**']);
80
-
81
- const nums555 = { nums: [5, 5, 5] };
82
- const nums123 = { nums: [1, 2, 3] };
83
-
84
- testAtom.do.add(nums123);
85
- testAtom.do.add(nums555);
86
- await wait();
87
-
88
- expect(testAtom.get()).toEqual(['@', '#', '**', nums123, nums555]);
89
-
90
- testAtom.do.update(val => {
91
- if (val[3] && typeof val[3] === 'object' && 'nums' in val[3]) val[3].nums.reverse();
92
- });
93
- await wait();
94
-
95
- expect(testAtom.get()).toEqual(['@', '#', '**', { nums: [3, 2, 1] }, nums555]);
96
-
97
- expect(testAtom.get()[4] === nums555).toBeTruthy();
98
- expect(testAtom.get()[3] !== nums123).toBeTruthy();
99
-
100
- const value = testAtom.get();
101
- expect(value[3] && typeof value[3] === 'object' && value[3].nums !== nums123.nums).toBeTruthy();
102
- });
103
- });
@@ -1,65 +0,0 @@
1
- import { Atom } from '../../types';
2
- import { AtomUpdateDoAction } from './_Update';
3
-
4
- export class AtomArrayDoActions<Value> extends AtomUpdateDoAction {
5
- constructor(private a: Atom<Value[]>, actions: Record<string, Function> | nil) {
6
- super(actions);
7
- }
8
-
9
- /** like the Array.prototype.push() method */
10
- push = (...values: Value[]) => {
11
- if (values.length === 0) return;
12
- this.a.set(this.a.get().concat(values));
13
- };
14
-
15
- /** like the Array.prototype.unshift() method */
16
- unshift = (...values: Value[]) => {
17
- if (values.length === 0) return;
18
- this.a.set(values.concat(this.a.get()));
19
- };
20
-
21
- /** transform current taken value */
22
- update = (updater: (value: Value[]) => void) => {
23
- const prev = this.a.get();
24
- const newValue = this.updateValue(prev, updater);
25
- if (newValue === prev) return;
26
- this.a.set(newValue);
27
- };
28
-
29
- /** like the Array.prototype.filter() method, but callback is optional - (it) => !!it */
30
- filter = (filter?: (value: Value, index: number, Array: Value[]) => any) => {
31
- const filtered = this.a.get().filter(filter ?? itIt);
32
- if (filtered.length === this.a.get().length) return;
33
- this.a.set(filtered);
34
- };
35
-
36
- /** will add value if not exists */
37
- add = (value: Value) => {
38
- if (this.a.get().includes(value)) return;
39
- this.a.set(this.a.get().concat([value]));
40
- };
41
-
42
- /** will delete value from array */
43
- removeFirst = (value: Value) => {
44
- const index = this.a.get().indexOf(value);
45
- if (index < 0) return;
46
- const newArray = this.a.get().slice(0);
47
- newArray.splice(index, 1);
48
- this.a.set(newArray);
49
- };
50
-
51
- /** will add value if it doesn't exist, otherwise delete */
52
- toggle = (value: Value, isAddToStart?: boolean) => {
53
- const newArray = this.a.get().slice();
54
- const index = newArray.indexOf(value);
55
-
56
- if (index < 0) {
57
- if (isAddToStart) newArray.unshift(value);
58
- else newArray.push(value);
59
- } else newArray.splice(index, 1);
60
-
61
- this.a.set(newArray);
62
- };
63
- }
64
-
65
- const itIt = <It>(it: It) => it;
@@ -1,27 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Boolean', () => {
11
- test('do actions', async () => {
12
- const testAtom = atom(false, {
13
- do: (set, get) => ({
14
- switch(news?: boolean) {
15
- set(news ?? !get());
16
- },
17
- }),
18
- });
19
-
20
- testAtom.do.switch(true);
21
- testAtom.do.switch();
22
- testAtom.do.toggle();
23
- await wait();
24
-
25
- expect(testAtom.get() === true).toBeTruthy();
26
- });
27
- });
@@ -1,13 +0,0 @@
1
- import { Atom } from '../../types';
2
- import { AtomDoActionsBasic } from './_Basic';
3
-
4
- export class AtomBooleanDoActions extends AtomDoActionsBasic {
5
- constructor(private a: Atom<boolean>, actions: Record<string, Function> | nil) {
6
- super(actions);
7
- }
8
-
9
- /** toggle current value between true/false */
10
- toggle = () => {
11
- this.a.set(!this.a.get());
12
- };
13
- }
@@ -1,77 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { makeFullKey, wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Map', () => {
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
- });
24
-
25
- test('do.setValue()', async () => {
26
- testAtom.do.setValue('!', 1);
27
- testAtom.do.setValue('@', 0);
28
- testAtom.do.setValue('#', { asasa: '' });
29
- testAtom.do.setValue('', 123);
30
- await wait();
31
-
32
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["@",0],["#",{"asasa":""}],["",123]]]');
33
-
34
- expect(testAtom.get()).toEqual(
35
- new Map<string, Value>([
36
- ['!', 1],
37
- ['@', 0],
38
- ['#', { asasa: '' }],
39
- ['', 123],
40
- ]),
41
- );
42
- });
43
-
44
- test('do.<filterKeyValues>()', async () => {
45
- testAtom.do.filterKeyValues();
46
- await wait();
47
-
48
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["#",{"asasa":""}],["",123]]]');
49
- });
50
-
51
- test('do.delete()', async () => {
52
- testAtom.do.delete('#');
53
- await wait();
54
-
55
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123]]]');
56
- });
57
-
58
- test('do.toggle()', async () => {
59
- testAtom.do.toggle('#', 555);
60
- await wait();
61
-
62
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123],["#",555]]]');
63
-
64
- testAtom.do.toggle('', 999);
65
- testAtom.do.toggle('!', 1234567890);
66
- await wait();
67
-
68
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[["#",555]]]');
69
- });
70
-
71
- test('do.clear()', async () => {
72
- testAtom.do.clear();
73
- await wait();
74
-
75
- expect(localStorage[makeFullKey('map:test')]).toEqual('[[]]');
76
- });
77
- });
@@ -1,58 +0,0 @@
1
- import { Atom } from '../../types';
2
- import { AtomDoActionsBasic } from './_Basic';
3
-
4
- export class AtomMapDoActions<
5
- MapValue extends Map<any, any>,
6
- Key extends MapValue extends Map<infer K, any> ? K : never,
7
- Value extends MapValue extends Map<any, infer V> ? V : never,
8
- > extends AtomDoActionsBasic {
9
- constructor(private a: Atom<MapValue>, actions: Record<string, Function> | nil) {
10
- super(actions);
11
- this.a = a;
12
- }
13
-
14
- /** like the Map.prototype.set() method, when value is new for key in current atom value */
15
- setValue = (key: Key, value: Value) => {
16
- if (this.a.get().get(key) === value) return;
17
-
18
- const newMap = new Map(this.a.get());
19
- newMap.set(key, value);
20
-
21
- this.a.set(newMap as never);
22
- };
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
-
34
- /** like the Map.prototype.delete() method */
35
- delete = (key: Key) => {
36
- if (!this.a.get().has(key)) return;
37
-
38
- const newMap = new Map(this.a.get());
39
- newMap.delete(key);
40
-
41
- this.a.set(newMap as never);
42
- };
43
-
44
- /** will add value if it doesn't exist, otherwise delete */
45
- toggle = (key: Key, value: Value) => {
46
- const newMap = new Map(this.a.get());
47
-
48
- if (newMap.has(key)) newMap.delete(key);
49
- else newMap.set(key, value);
50
-
51
- this.a.set(newMap as never);
52
- };
53
-
54
- /** like the Map.prototype.clear() method */
55
- clear = () => {
56
- this.a.set(new Map() as never);
57
- };
58
- }
@@ -1,25 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Number', () => {
11
- test('do actions', async () => {
12
- const testAtom = atom(100, { do: (set, get) => ({ switchSign: () => set(-get()) }) });
13
- const testFuncAtom = atom(() => 200, { do: (set, get) => ({ switchSign: () => set(-get()) }) });
14
-
15
- testAtom.do.increment();
16
- testAtom.do.switchSign();
17
-
18
- testFuncAtom.do.increment(-3);
19
- testFuncAtom.do.switchSign();
20
- await wait();
21
-
22
- expect(testAtom.get()).toEqual(-101);
23
- expect(testFuncAtom.get()).toEqual(-197);
24
- });
25
- });
@@ -1,15 +0,0 @@
1
- import { Atom } from '../../types';
2
- import { AtomDoActionsBasic } from './_Basic';
3
-
4
- export class AtomNumberDoActions extends AtomDoActionsBasic {
5
- constructor(private a: Atom<number>, actions: Record<string, Function> | nil) {
6
- super(actions);
7
- }
8
-
9
- /** pass the 2 to increment on 2, pass the -2 to decrement on 2
10
- * **default: 1**
11
- */
12
- increment = (delta?: number) => {
13
- this.a.set(+this.a.get() + (delta ?? 1));
14
- };
15
- }
@@ -1,67 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Object', () => {
11
- test('do.setDeepPartial()', async () => {
12
- const b = { c: [{ d: 8, e: 'e', f: 'F', g: { h: 'HHH' } }] };
13
- const a = { f: { g: '' }, b };
14
- const testAtom = atom({ a, b });
15
-
16
- testAtom.do.setDeepPartial('b.c.0.d', 123, { b: { c: [{}] } });
17
-
18
- await wait();
19
-
20
- expect(testAtom.get().b).not.toEqual(b);
21
- expect(testAtom.get().b.c[0].d).toEqual(123);
22
- expect(testAtom.get().b.c).not.toEqual(b.c);
23
- expect(testAtom.get().a).toEqual(a);
24
-
25
- testAtom.do.setDeepPartial('b+c+8+e', 'EE', null, '+');
26
-
27
- await wait();
28
-
29
- expect(testAtom.get().b.c[8].e).toEqual('EE');
30
- });
31
-
32
- test('do.setDeepPartial() with first numeric prop', async () => {
33
- enum Num {
34
- num = 123,
35
- }
36
- const testAtom = atom({ [Num.num]: { a: 'A' } });
37
-
38
- testAtom.do.setDeepPartial(`${Num.num}.a`, 'AA', { [Num.num]: {} });
39
-
40
- await wait();
41
-
42
- expect(testAtom.get()[Num.num].a).toEqual('AA');
43
- });
44
-
45
- test('do.setPartial()', async () => {
46
- const testAtom = atom((): Record<number, unknown> => ({ 2: { a: 'A' } }));
47
-
48
- testAtom.do.setPartial({ 3: { b: 'B' } });
49
- testAtom.do.setPartial({ 4: [] });
50
- await wait();
51
-
52
- expect(testAtom.get()).toEqual({ 2: { a: 'A' }, 3: { b: 'B' }, 4: [] });
53
- });
54
-
55
- test('do.update()', async () => {
56
- const init = { a: { b: { c: { d: { e: 'E' } }, f: { g: {} } }, h: { i: { j: {} } } } };
57
- const testAtom = atom(init);
58
-
59
- testAtom.do.update(obj => (obj.a.b.c.d.e = 'eE'));
60
-
61
- await wait();
62
-
63
- expect(testAtom.get().a.b.c.d.e).toEqual('eE');
64
- expect(testAtom.get().a.b.c).not.toEqual(init.a.b.c);
65
- expect(testAtom.get().a.h.i).toEqual(init.a.h.i);
66
- });
67
- });
@@ -1,75 +0,0 @@
1
- import { Atom, ObjectActionsSetDeepPartialDoAction } from '../../types';
2
- import { configuredOptions } from '../lib';
3
- import { AtomUpdateDoAction } from './_Update';
4
-
5
- export class AtomObjectDoActions<Value extends object> extends AtomUpdateDoAction {
6
- constructor(private a: Atom<Value>, actions: Record<string, Function> | nil) {
7
- super(actions);
8
- }
9
-
10
- /** pass partial object to update some field values */
11
- setPartial = (value: Partial<Value> | ((value: Value) => Partial<Value>)) =>
12
- this.a.set(prev => ({
13
- ...prev,
14
- ...(typeof value === 'function' ? value(this.a.get()) : value),
15
- }));
16
-
17
- /** transform current taken value */
18
- update = (updater: (value: Value) => void) => {
19
- const prev = this.a.get();
20
- const newValue = this.updateValue(prev, updater);
21
- if (newValue === prev) return;
22
- this.a.set(newValue);
23
- };
24
-
25
- /** pass partial value to update some deep values by flat path */
26
- setDeepPartial: ObjectActionsSetDeepPartialDoAction<Value> = (
27
- path,
28
- value,
29
- donor,
30
- separator = (configuredOptions.keyPathSeparator || '.') as never,
31
- ) => {
32
- if (!separator) return;
33
-
34
- if (path.includes(separator)) {
35
- let keys = path.split(separator);
36
- const lastKey = keys[keys.length - 1];
37
- keys = keys.slice(0, -1);
38
- const newObject = { ...this.a.get() };
39
- let lastObject = newObject as Record<string, unknown>;
40
- let lastDonorObject = donor as Record<string, unknown> | nil;
41
-
42
- for (const key of keys) {
43
- lastDonorObject = lastDonorObject?.[Array.isArray(lastDonorObject) ? '0' : key] as never;
44
- const currentObject = lastObject[makeKey(lastObject, key)] ?? (Array.isArray(lastDonorObject) ? [] : {});
45
-
46
- if (currentObject == null || typeof currentObject !== 'object') {
47
- if (donor == null) throw 'Incorrect path for setDeepPartial';
48
-
49
- const newValue = typeof value === 'function' ? (value as (val: undefined) => Value)(undefined) : value;
50
-
51
- if (this.a.get()[path as never] !== newValue) this.setPartial({ [path]: newValue } as never);
52
- return;
53
- }
54
-
55
- lastObject = lastObject[makeKey(lastObject, key)] = (
56
- Array.isArray(currentObject) ? [...currentObject] : { ...currentObject }
57
- ) as never;
58
- }
59
-
60
- const prev = lastObject[lastKey];
61
- lastObject[lastKey] =
62
- typeof value === 'function' ? (value as (val: unknown) => Value)(lastObject[lastKey]) : value;
63
-
64
- if (prev !== lastObject[lastKey]) this.a.set(newObject);
65
-
66
- return;
67
- }
68
-
69
- const prevValue = this.a.get()[path as never];
70
- const newValue = typeof value === 'function' ? (value as (val: Value) => Value)(prevValue) : value;
71
- if (newValue !== prevValue) this.setPartial({ [path]: newValue } as never);
72
- };
73
- }
74
-
75
- const makeKey = (obj: object, key: string) => (Array.isArray(obj) ? `${+key}` : key);
@@ -1,46 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- import { atom, configureAtomaric } from '../lib';
3
- import { makeFullKey, wait } from '../utils';
4
-
5
- configureAtomaric({
6
- useSyncExternalStore,
7
- keyPathSeparator: '.',
8
- });
9
-
10
- describe('Set', () => {
11
- test('do actions', async () => {
12
- const testAtom = atom(new Set<string>(), {
13
- storeKey: 'set:test',
14
- do: (set, get) => ({
15
- filterValues: () => {
16
- const array = Array.from(get());
17
- set(new Set(array.filter(it => it)));
18
- },
19
- }),
20
- });
21
-
22
- testAtom.do.add('!');
23
- testAtom.do.add('@');
24
- testAtom.do.add('#');
25
- testAtom.do.add('');
26
- await wait();
27
-
28
- expect(localStorage[makeFullKey('set:test')]).toEqual('[["!","@","#",""]]');
29
-
30
- testAtom.do.filterValues();
31
- await wait();
32
-
33
- expect(localStorage[makeFullKey('set:test')]).toEqual('[["!","@","#"]]');
34
-
35
- testAtom.do.toggle('@');
36
- await wait();
37
-
38
- expect(localStorage[makeFullKey('set:test')]).toEqual('[["!","#"]]');
39
-
40
- testAtom.do.toggle('@');
41
- testAtom.do.delete('!');
42
- await wait();
43
-
44
- expect(localStorage[makeFullKey('set:test')]).toEqual('[["#","@"]]');
45
- });
46
- });
@@ -1,40 +0,0 @@
1
- import { Atom } from '../../types';
2
- import { AtomDoActionsBasic } from './_Basic';
3
-
4
- export class AtomSetDoActions<Value> extends AtomDoActionsBasic {
5
- constructor(private a: Atom<Set<Value>>, actions: Record<string, Function> | nil) {
6
- super(actions);
7
- this.a = a;
8
- }
9
-
10
- /** like the Set.prototype.add() method */
11
- add = (value: Value) => {
12
- if (this.a.get().has(value)) return;
13
-
14
- this.a.set(new Set(this.a.get()).add(value));
15
- };
16
-
17
- /** like the Set.prototype.delete() method */
18
- delete = (value: Value) => {
19
- if (!this.a.get().has(value)) return;
20
-
21
- const newSet = new Set(this.a.get());
22
- newSet.delete(value);
23
- this.a.set(newSet);
24
- };
25
-
26
- /** will add value if it doesn't exist, otherwise delete */
27
- toggle = (value: Value) => {
28
- const newSet = new Set(this.a.get());
29
-
30
- if (newSet.has(value)) newSet.delete(value);
31
- else newSet.add(value);
32
-
33
- this.a.set(newSet);
34
- };
35
-
36
- /** like the Set.prototype.clear() method */
37
- clear = () => {
38
- this.a.set(new Set());
39
- };
40
- }
@@ -1,11 +0,0 @@
1
- export class AtomDoActionsBasic {
2
- constructor(actions: Record<string, Function> | nil) {
3
- if (actions)
4
- return new Proxy(this, {
5
- get: (self, p) => {
6
- if (p in this) return self[p as never];
7
- return actions[p as never];
8
- },
9
- });
10
- }
11
- }
@@ -1,37 +0,0 @@
1
- import { makeDeepProxyObject } from '../makeDeepProxyObject';
2
- import { AtomDoActionsBasic } from './_Basic';
3
-
4
- export class AtomUpdateDoAction extends AtomDoActionsBasic {
5
- protected updateValue = <Object extends object | unknown[]>(
6
- object: Object,
7
- updater: (object: Object) => void,
8
- ): Object => {
9
- const newObject = Array.isArray(object) ? object.slice(0) : { ...object };
10
- let isSomeSetted = false;
11
-
12
- const pro = makeDeepProxyObject(object, {
13
- onSet: (_, keys, setKey, value, prevValue) => {
14
- if (value === prevValue) return true;
15
- let currentObject = newObject as Record<string, unknown> | unknown[];
16
-
17
- isSomeSetted = true;
18
-
19
- for (const key of keys) {
20
- const nextObject = currentObject[key as never] as object;
21
-
22
- currentObject = currentObject[key as never] = (
23
- Array.isArray(nextObject) ? nextObject.slice() : { ...nextObject }
24
- ) as never;
25
- }
26
-
27
- currentObject[setKey as never] = value as never;
28
-
29
- return true;
30
- },
31
- });
32
-
33
- updater(pro);
34
-
35
- return isSomeSetted ? (newObject as never) : object;
36
- };
37
- }