atomaric 0.0.35 → 0.0.36

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,20 +1,25 @@
1
- const B = (e, r, i) => {
2
- let a = null;
3
- typeof e == "number" ? a = _(
1
+ let M = () => {
2
+ throw "call configureAtomaric() before all!";
3
+ };
4
+ const $ = {}, v = (e) => {
5
+ $.useSyncExternalStore = M = e.useSyncExternalStore, $.keyPathSeparator = e.keyPathSeparator;
6
+ }, B = (e) => M(e.subscribe, e.get), G = (e) => e.set, K = (e) => e.setDeferred, O = (e) => e.get, ee = (e) => e.do, te = (e) => [B(e), G(e)], se = (e, r) => new q(e, r), U = (e, r, i) => {
7
+ let d = null;
8
+ typeof e == "number" ? d = _(
4
9
  r,
5
10
  (s) => ({
6
11
  increment: (n) => {
7
12
  s.set(+s.get() + (n ?? 0));
8
13
  }
9
14
  })
10
- ) : typeof e == "boolean" ? a = _(
15
+ ) : typeof e == "boolean" ? d = _(
11
16
  r,
12
17
  (s) => ({
13
18
  toggle: () => {
14
19
  s.set(!s.get());
15
20
  }
16
21
  })
17
- ) : Array.isArray(e) ? a = _(
22
+ ) : Array.isArray(e) ? d = _(
18
23
  r,
19
24
  (s) => ({
20
25
  push: (...n) => {
@@ -28,14 +33,14 @@ const B = (e, r, i) => {
28
33
  n(c), s.set(c);
29
34
  },
30
35
  filter: (n) => {
31
- s.set(s.get().filter(n ?? G));
36
+ s.set(s.get().filter(n ?? W));
32
37
  },
33
38
  toggle: (n, c) => {
34
39
  const g = s.get().slice(), f = g.indexOf(n);
35
40
  f < 0 ? c ? g.unshift(n) : g.push(n) : g.splice(f, 1), s.set(g);
36
41
  }
37
42
  })
38
- ) : e instanceof Set ? a = _(
43
+ ) : e instanceof Set ? d = _(
39
44
  r,
40
45
  (s) => ({
41
46
  add: (n) => {
@@ -57,7 +62,7 @@ const B = (e, r, i) => {
57
62
  n(c), s.set(c);
58
63
  }
59
64
  })
60
- ) : e instanceof Object && (a = _(
65
+ ) : e instanceof Object && (d = _(
61
66
  r,
62
67
  (s) => ({
63
68
  setPartial: (n) => s.set((c) => ({
@@ -68,22 +73,22 @@ const B = (e, r, i) => {
68
73
  const c = { ...s.get() };
69
74
  n(c), s.set(c);
70
75
  },
71
- setDeepPartial: (n, c, g = ".") => {
76
+ setDeepPartial: (n, c, g = $.keyPathSeparator ?? ".") => {
72
77
  if (n.includes(g)) {
73
78
  let f = n.split(g);
74
79
  const w = f[f.length - 1];
75
80
  f = f.slice(0, -1);
76
81
  const D = { ...s.get() };
77
- let d = D;
82
+ let a = D;
78
83
  for (const S of f) {
79
- const A = d[S];
80
- if (A == null || typeof A != "object") {
84
+ const y = a[S];
85
+ if (y == null || typeof y != "object") {
81
86
  s.do.setPartial({ [n]: typeof c == "function" ? c(void 0) : c });
82
87
  return;
83
88
  }
84
- d = d[S] = Array.isArray(A) ? [...A] : { ...A };
89
+ a = a[S] = Array.isArray(y) ? [...y] : { ...y };
85
90
  }
86
- d[w] = typeof c == "function" ? c(d[w]) : c, s.set(D);
91
+ a[w] = typeof c == "function" ? c(a[w]) : c, s.set(D);
87
92
  } else s.do.setPartial({ [n]: c });
88
93
  }
89
94
  })
@@ -93,63 +98,63 @@ const B = (e, r, i) => {
93
98
  () => r.get(),
94
99
  r,
95
100
  (s, n, c) => r.setDeferred(s, n, c)
96
- ) : null, p = {};
97
- return h && Object.keys(h).forEach((s) => Object.defineProperty(p, s, { get: () => h[s] })), a && Object.keys(a).forEach(
98
- (s) => Object.defineProperty(p, s, { get: () => a[s] })
99
- ), p;
100
- }, G = (e) => e, _ = (e, r, i) => r(e);
101
- class U {
101
+ ) : null, b = {};
102
+ return h && Object.keys(h).forEach((s) => Object.defineProperty(b, s, { get: () => h[s] })), d && Object.keys(d).forEach(
103
+ (s) => Object.defineProperty(b, s, { get: () => d[s] })
104
+ ), b;
105
+ }, W = (e) => e, _ = (e, r, i) => r(e);
106
+ class q {
102
107
  constructor(r, i) {
103
- const a = (t) => n = t, h = () => n, p = /* @__PURE__ */ new Set(), s = (t) => t(f());
108
+ const d = (t) => n = t, h = () => n, b = /* @__PURE__ */ new Set(), s = (t) => t(f());
104
109
  let n = r, c, g = () => {
105
110
  }, f = () => h(), w = null, D = () => {
106
- const t = B(r, d, i);
111
+ const t = U(r, a, i);
107
112
  return D = () => t, t;
108
113
  };
109
- const d = new Proxy(this, {
114
+ const a = new Proxy(this, {
110
115
  get: (t, u) => u === "do" ? D() : t[u],
111
- set: W
116
+ set: Q
112
117
  }), S = (t, u) => {
113
- const b = typeof t == "function" ? t(f()) : t;
114
- if (!(b === f() || b === void 0 || typeof b == "number" && isNaN(b))) {
115
- a(b), p.forEach(s, this);
118
+ const p = typeof t == "function" ? t(f()) : t;
119
+ if (!(p === f() || p === void 0 || typeof p == "number" && isNaN(p))) {
120
+ d(p), b.forEach(s, this);
116
121
  try {
117
- P.postMessage({ key: l, value: h() });
122
+ N.postMessage({ key: l, value: h() });
118
123
  } catch {
119
124
  }
120
- u !== !0 && g(b);
125
+ u !== !0 && g(p);
121
126
  }
122
127
  };
123
- this.set = (t, u) => S(t, u), this.get = () => f(), this.initialValue = r, this.isInitialValue = () => r === h(), this.subscribe = (t) => (p.add(t), () => {
124
- p.delete(t);
128
+ this.set = (t, u) => S(t, u), this.get = () => f(), this.initialValue = r, this.isInitialValue = () => r === h(), this.subscribe = (t) => (b.add(t), () => {
129
+ b.delete(t);
125
130
  }), this.reset = () => {
126
- S(r, !0), p.forEach(s, this);
131
+ S(r, !0), b.forEach(s, this);
127
132
  };
128
- const A = (t, u) => {
133
+ const y = (t, u) => {
129
134
  S(t, u), c = void 0;
130
135
  };
131
- if (this.setDeferred = (t, u = 500, b, H = !0) => {
132
- H && c === void 0 && S(t, b), clearTimeout(c), c = setTimeout(A, u, t, b);
133
- }, i == null) return d;
134
- let y = null, T = null, I = !0, E = !0, C = !1, z = -1, $ = r instanceof Set ? (t) => new Set(t) : (t) => t, j = r instanceof Set ? (t) => {
136
+ if (this.setDeferred = (t, u = 500, p, L = !0) => {
137
+ L && c === void 0 && S(t, p), clearTimeout(c), c = setTimeout(y, u, t, p);
138
+ }, i == null) return a;
139
+ let A = null, T = null, I = !0, E = !0, P = !1, V = -1, C = r instanceof Set ? (t) => new Set(t) : (t) => t, j = r instanceof Set ? (t) => {
135
140
  if (t instanceof Set) return Array.from(t);
136
141
  throw console.error(t), "The value is not Set instance";
137
142
  } : (t) => t;
138
143
  if (typeof i == "string")
139
- y = i;
144
+ A = i;
140
145
  else if ("storeKey" in i)
141
- I = i.warnOnDuplicateStoreKey ?? I, E = i.listenStorageChanges ?? E, y = i.storeKey, $ = i.unzipValue ?? $, j = i.zipValue ?? j, C = i.unchangable ?? C, T = i.exp ?? T;
142
- else return d;
143
- const l = `${L}${y}`, V = T === null || !(T(d, l in o) instanceof Date) ? (t) => JSON.stringify([j(t)]) : (t) => (w ?? (w = {}), w.exp = T(d, l in o).getTime() + 0.2866, w.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(z), clearTimeout(M[l]), z = setTimeout(() => this.reset(), w.exp - Date.now())), JSON.stringify([j(t), w])), F = (t) => {
146
+ I = i.warnOnDuplicateStoreKey ?? I, E = i.listenStorageChanges ?? E, A = i.storeKey, C = i.unzipValue ?? C, j = i.zipValue ?? j, P = i.unchangable ?? P, T = i.exp ?? T;
147
+ else return a;
148
+ const l = `${R}${A}`, F = T === null || !(T(a, l in o) instanceof Date) ? (t) => JSON.stringify([j(t)]) : (t) => (w ?? (w = {}), w.exp = T(a, l in o).getTime() + 0.2866, w.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(V), clearTimeout(k[l]), V = setTimeout(() => this.reset(), w.exp - Date.now())), JSON.stringify([j(t), w])), J = (t) => {
144
149
  const u = JSON.parse(t);
145
- return w = u[1], $(u[0]);
150
+ return w = u[1], C(u[0]);
146
151
  };
147
- let J = !0;
148
- if (N[l] = d, o[`atom/${y}`] && (o[l] || (o[l] = `[${o[`atom/${y}`]}]`), delete o[`atom/${y}`]), f = () => {
149
- if (f = h, J) {
150
- J = !1;
152
+ let H = !0;
153
+ if (z[l] = a, o[`atom/${A}`] && (o[l] || (o[l] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => {
154
+ if (f = h, H) {
155
+ H = !1;
151
156
  try {
152
- a(l in o ? F(o[l]) : r);
157
+ d(l in o ? J(o[l]) : r);
153
158
  } catch {
154
159
  console.warn("Invalid json value", o[l]);
155
160
  }
@@ -160,14 +165,14 @@ class U {
160
165
  this.reset();
161
166
  return;
162
167
  }
163
- o[l] = V(t);
168
+ o[l] = F(t);
164
169
  }, this.reset = () => {
165
170
  delete o[l], S(r, !0);
166
- }, I && x[l] !== void 0 && console.warn("Duplicate Atom key", y), E)
167
- if (C) {
171
+ }, I && x[l] !== void 0 && console.warn("Duplicate Atom key", A), E)
172
+ if (P) {
168
173
  let t = !1, u;
169
174
  m[l] = this, x[l] = () => {
170
- clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = V(h()));
175
+ clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = F(h()));
171
176
  };
172
177
  } else
173
178
  x[l] = (t) => {
@@ -176,59 +181,56 @@ class U {
176
181
  return;
177
182
  }
178
183
  try {
179
- S(F(t.newValue));
184
+ S(J(t.newValue));
180
185
  } catch {
181
186
  console.warn("Invalid json value", t.newValue);
182
187
  }
183
188
  };
184
- return d;
189
+ return a;
185
190
  }
186
191
  }
187
- let P;
192
+ let N;
188
193
  try {
189
- P = new BroadcastChannel("updateHere"), P.addEventListener("message", (e) => {
194
+ N = new BroadcastChannel("updateHere"), N.addEventListener("message", (e) => {
190
195
  var r;
191
196
  (r = m[e.data.key]) == null || r.set(e.data.value, !0);
192
197
  });
193
198
  } catch {
194
199
  }
195
- const o = window.localStorage, x = {}, m = {}, W = (e, r) => {
200
+ const o = window.localStorage, x = {}, m = {}, Q = (e, r) => {
196
201
  throw `${r} is readonly property`;
197
202
  };
198
203
  window.addEventListener("storage", (e) => {
199
204
  var r;
200
205
  e.key === null || e.newValue === e.oldValue || (r = x[e.key]) == null || r.call(x, e);
201
206
  });
202
- const q = o.setItem.bind(o), Q = o.removeItem.bind(o);
207
+ const X = o.setItem.bind(o), Y = o.removeItem.bind(o);
203
208
  o.setItem = (e, r) => {
204
- m[e] === void 0 && q.call(o, e, r);
209
+ m[e] === void 0 && X.call(o, e, r);
205
210
  };
206
211
  o.removeItem = (e) => {
207
- m[e] === void 0 && Q.call(o, e);
212
+ m[e] === void 0 && Y.call(o, e);
208
213
  };
209
- const X = /"exp":(\d+)\.2866/, L = "atom\\", N = {}, M = {};
214
+ const Z = /"exp":(\d+)\.2866/, R = "atom\\", z = {}, k = {};
210
215
  setTimeout(() => {
211
216
  Object.keys(o).forEach((e) => {
212
217
  var i;
213
- if (!e.startsWith(L) || typeof o[e] != "string") return;
214
- const r = +((i = o[e].match(X)) == null ? void 0 : i[1]);
215
- r && r - Date.now() < 24 * 60 * 60 * 1e3 && (M[e] = setTimeout(() => {
216
- N[e] ? N[e].reset() : delete o[e];
218
+ if (!e.startsWith(R) || typeof o[e] != "string") return;
219
+ const r = +((i = o[e].match(Z)) == null ? void 0 : i[1]);
220
+ r && r - Date.now() < 24 * 60 * 60 * 1e3 && (k[e] = setTimeout(() => {
221
+ z[e] ? z[e].reset() : delete o[e];
217
222
  }, r - Date.now()));
218
223
  });
219
224
  }, 1e3);
220
- let R = () => {
221
- throw "call configureAtomaric() before all!";
222
- };
223
- const k = (e) => R = e.useSyncExternalStore, Y = (e) => R(e.subscribe, e.get), Z = (e) => e.set, v = (e) => e.setDeferred, K = (e) => e.get, O = (e) => e.do, ee = (e) => [Y(e), Z(e)], te = (e, r) => new U(e, r);
224
225
  export {
225
- U as Atom,
226
- te as atom,
227
- k as configureAtomaric,
228
- ee as useAtom,
229
- O as useAtomDo,
230
- K as useAtomGet,
231
- Z as useAtomSet,
232
- v as useAtomSetDeferred,
233
- Y as useAtomValue
226
+ q as Atom,
227
+ se as atom,
228
+ v as configureAtomaric,
229
+ $ as configuredOptions,
230
+ te as useAtom,
231
+ ee as useAtomDo,
232
+ O as useAtomGet,
233
+ G as useAtomSet,
234
+ K as useAtomSetDeferred,
235
+ B as useAtomValue
234
236
  };
@@ -1 +1 @@
1
- (function(d,D){typeof exports=="object"&&typeof module<"u"?D(exports):typeof define=="function"&&define.amd?define(["exports"],D):(d=typeof globalThis<"u"?globalThis:d||self,D(d.atomaric={}))})(this,function(d){"use strict";const D=(e,r,i)=>{let g=null;typeof e=="number"?g=j(r,n=>({increment:s=>{n.set(+n.get()+(s??0))}})):typeof e=="boolean"?g=j(r,n=>({toggle:()=>{n.set(!n.get())}})):Array.isArray(e)?g=j(r,n=>({push:(...s)=>{n.set(n.get().concat(s))},unshift:(...s)=>{n.set(s.concat(n.get()))},update:s=>{const c=n.get().slice();s(c),n.set(c)},filter:s=>{n.set(n.get().filter(s??Q))},toggle:(s,c)=>{const h=n.get().slice(),f=h.indexOf(s);f<0?c?h.unshift(s):h.push(s):h.splice(f,1),n.set(h)}})):e instanceof Set?g=j(r,n=>({add:s=>{n.set(new Set(n.get()).add(s))},delete:s=>{const c=new Set(n.get());c.delete(s),n.set(c)},toggle:s=>{const c=new Set(n.get());c.has(s)?c.delete(s):c.add(s),n.set(c)},clear:()=>{n.set(new Set)},update:s=>{const c=new Set(n.get());s(c),n.set(c)}})):e instanceof Object&&(g=j(r,n=>({setPartial:s=>n.set(c=>({...c,...typeof s=="function"?s(n.get()):s})),update:s=>{const c={...n.get()};s(c),n.set(c)},setDeepPartial:(s,c,h=".")=>{if(s.includes(h)){let f=s.split(h);const S=f[f.length-1];f=f.slice(0,-1);const _={...n.get()};let a=_;for(const A of f){const m=a[A];if(m==null||typeof m!="object"){n.do.setPartial({[s]:typeof c=="function"?c(void 0):c});return}a=a[A]=Array.isArray(m)?[...m]:{...m}}a[S]=typeof c=="function"?c(a[S]):c,n.set(_)}else n.do.setPartial({[s]:c})}})));const w=typeof i=="object"&&i!=null&&"do"in i?i.do((n,s)=>r.set(n,s),()=>r.get(),r,(n,s,c)=>r.setDeferred(n,s,c)):null,p={};return w&&Object.keys(w).forEach(n=>Object.defineProperty(p,n,{get:()=>w[n]})),g&&Object.keys(g).forEach(n=>Object.defineProperty(p,n,{get:()=>g[n]})),p},Q=e=>e,j=(e,r,i)=>r(e);class F{constructor(r,i){const g=t=>s=t,w=()=>s,p=new Set,n=t=>t(f());let s=r,c,h=()=>{},f=()=>w(),S=null,_=()=>{const t=D(r,a,i);return _=()=>t,t};const a=new Proxy(this,{get:(t,u)=>u==="do"?_():t[u],set:X}),A=(t,u)=>{const b=typeof t=="function"?t(f()):t;if(!(b===f()||b===void 0||typeof b=="number"&&isNaN(b))){g(b),p.forEach(n,this);try{C.postMessage({key:l,value:w()})}catch{}u!==!0&&h(b)}};this.set=(t,u)=>A(t,u),this.get=()=>f(),this.initialValue=r,this.isInitialValue=()=>r===w(),this.subscribe=t=>(p.add(t),()=>{p.delete(t)}),this.reset=()=>{A(r,!0),p.forEach(n,this)};const m=(t,u)=>{A(t,u),c=void 0};if(this.setDeferred=(t,u=500,b,q=!0)=>{q&&c===void 0&&A(t,b),clearTimeout(c),c=setTimeout(m,u,t,b)},i==null)return a;let y=null,I=null,$=!0,N=!0,V=!1,R=-1,z=r instanceof Set?t=>new Set(t):t=>t,E=r instanceof Set?t=>{if(t instanceof Set)return Array.from(t);throw console.error(t),"The value is not Set instance"}:t=>t;if(typeof i=="string")y=i;else if("storeKey"in i)$=i.warnOnDuplicateStoreKey??$,N=i.listenStorageChanges??N,y=i.storeKey,z=i.unzipValue??z,E=i.zipValue??E,V=i.unchangable??V,I=i.exp??I;else return a;const l=`${J}${y}`,B=I===null||!(I(a,l in o)instanceof Date)?t=>JSON.stringify([E(t)]):t=>(S??(S={}),S.exp=I(a,l in o).getTime()+.2866,S.exp-Date.now()<24*60*60*1e3&&(clearTimeout(R),clearTimeout(M[l]),R=setTimeout(()=>this.reset(),S.exp-Date.now())),JSON.stringify([E(t),S])),U=t=>{const u=JSON.parse(t);return S=u[1],z(u[0])};let W=!0;if(P[l]=a,o[`atom/${y}`]&&(o[l]||(o[l]=`[${o[`atom/${y}`]}]`),delete o[`atom/${y}`]),f=()=>{if(f=w,W){W=!1;try{g(l in o?U(o[l]):r)}catch{console.warn("Invalid json value",o[l])}}return w()},h=t=>{if(t===r){this.reset();return}o[l]=B(t)},this.reset=()=>{delete o[l],A(r,!0)},$&&T[l]!==void 0&&console.warn("Duplicate Atom key",y),N)if(V){let t=!1,u;x[l]=this,T[l]=()=>{clearTimeout(u),u=setTimeout(()=>t=!1,10),!t&&(t=!0,o[l]=B(w()))}}else T[l]=t=>{if(t.newValue===null){this.reset();return}try{A(U(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}};return a}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=x[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const o=window.localStorage,T={},x={},X=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=T[e.key])==null||r.call(T,e)});const Y=o.setItem.bind(o),Z=o.removeItem.bind(o);o.setItem=(e,r)=>{x[e]===void 0&&Y.call(o,e,r)},o.removeItem=e=>{x[e]===void 0&&Z.call(o,e)};const v=/"exp":(\d+)\.2866/,J="atom\\",P={},M={};setTimeout(()=>{Object.keys(o).forEach(e=>{var i;if(!e.startsWith(J)||typeof o[e]!="string")return;const r=+((i=o[e].match(v))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(M[e]=setTimeout(()=>{P[e]?P[e].reset():delete o[e]},r-Date.now()))})},1e3);let G=()=>{throw"call configureAtomaric() before all!"};const k=e=>G=e.useSyncExternalStore,H=e=>G(e.subscribe,e.get),L=e=>e.set,K=e=>e.setDeferred,O=e=>e.get,ee=e=>e.do,te=e=>[H(e),L(e)],ne=(e,r)=>new F(e,r);d.Atom=F,d.atom=ne,d.configureAtomaric=k,d.useAtom=te,d.useAtomDo=ee,d.useAtomGet=O,d.useAtomSet=L,d.useAtomSetDeferred=K,d.useAtomValue=H,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
1
+ (function(f,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(f=typeof globalThis<"u"?globalThis:f||self,T(f.atomaric={}))})(this,function(f){"use strict";let T=()=>{throw"call configureAtomaric() before all!"};const E={},q=e=>{E.useSyncExternalStore=T=e.useSyncExternalStore,E.keyPathSeparator=e.keyPathSeparator},J=e=>T(e.subscribe,e.get),M=e=>e.set,Q=e=>e.setDeferred,X=e=>e.get,Y=e=>e.do,Z=e=>[J(e),M(e)],v=(e,r)=>new G(e,r),K=(e,r,i)=>{let g=null;typeof e=="number"?g=j(r,n=>({increment:s=>{n.set(+n.get()+(s??0))}})):typeof e=="boolean"?g=j(r,n=>({toggle:()=>{n.set(!n.get())}})):Array.isArray(e)?g=j(r,n=>({push:(...s)=>{n.set(n.get().concat(s))},unshift:(...s)=>{n.set(s.concat(n.get()))},update:s=>{const o=n.get().slice();s(o),n.set(o)},filter:s=>{n.set(n.get().filter(s??O))},toggle:(s,o)=>{const h=n.get().slice(),a=h.indexOf(s);a<0?o?h.unshift(s):h.push(s):h.splice(a,1),n.set(h)}})):e instanceof Set?g=j(r,n=>({add:s=>{n.set(new Set(n.get()).add(s))},delete:s=>{const o=new Set(n.get());o.delete(s),n.set(o)},toggle:s=>{const o=new Set(n.get());o.has(s)?o.delete(s):o.add(s),n.set(o)},clear:()=>{n.set(new Set)},update:s=>{const o=new Set(n.get());s(o),n.set(o)}})):e instanceof Object&&(g=j(r,n=>({setPartial:s=>n.set(o=>({...o,...typeof s=="function"?s(n.get()):s})),update:s=>{const o={...n.get()};s(o),n.set(o)},setDeepPartial:(s,o,h=E.keyPathSeparator??".")=>{if(s.includes(h)){let a=s.split(h);const w=a[a.length-1];a=a.slice(0,-1);const _={...n.get()};let d=_;for(const A of a){const y=d[A];if(y==null||typeof y!="object"){n.do.setPartial({[s]:typeof o=="function"?o(void 0):o});return}d=d[A]=Array.isArray(y)?[...y]:{...y}}d[w]=typeof o=="function"?o(d[w]):o,n.set(_)}else n.do.setPartial({[s]:o})}})));const S=typeof i=="object"&&i!=null&&"do"in i?i.do((n,s)=>r.set(n,s),()=>r.get(),r,(n,s,o)=>r.setDeferred(n,s,o)):null,p={};return S&&Object.keys(S).forEach(n=>Object.defineProperty(p,n,{get:()=>S[n]})),g&&Object.keys(g).forEach(n=>Object.defineProperty(p,n,{get:()=>g[n]})),p},O=e=>e,j=(e,r,i)=>r(e);class G{constructor(r,i){const g=t=>s=t,S=()=>s,p=new Set,n=t=>t(a());let s=r,o,h=()=>{},a=()=>S(),w=null,_=()=>{const t=K(r,d,i);return _=()=>t,t};const d=new Proxy(this,{get:(t,u)=>u==="do"?_():t[u],set:ee}),A=(t,u)=>{const b=typeof t=="function"?t(a()):t;if(!(b===a()||b===void 0||typeof b=="number"&&isNaN(b))){g(b),p.forEach(n,this);try{C.postMessage({key:l,value:S()})}catch{}u!==!0&&h(b)}};this.set=(t,u)=>A(t,u),this.get=()=>a(),this.initialValue=r,this.isInitialValue=()=>r===S(),this.subscribe=t=>(p.add(t),()=>{p.delete(t)}),this.reset=()=>{A(r,!0),p.forEach(n,this)};const y=(t,u)=>{A(t,u),o=void 0};if(this.setDeferred=(t,u=500,b,W=!0)=>{W&&o===void 0&&A(t,b),clearTimeout(o),o=setTimeout(y,u,t,b)},i==null)return d;let m=null,I=null,N=!0,V=!0,z=!1,R=-1,F=r instanceof Set?t=>new Set(t):t=>t,x=r instanceof Set?t=>{if(t instanceof Set)return Array.from(t);throw console.error(t),"The value is not Set instance"}:t=>t;if(typeof i=="string")m=i;else if("storeKey"in i)N=i.warnOnDuplicateStoreKey??N,V=i.listenStorageChanges??V,m=i.storeKey,F=i.unzipValue??F,x=i.zipValue??x,z=i.unchangable??z,I=i.exp??I;else return d;const l=`${H}${m}`,k=I===null||!(I(d,l in c)instanceof Date)?t=>JSON.stringify([x(t)]):t=>(w??(w={}),w.exp=I(d,l in c).getTime()+.2866,w.exp-Date.now()<24*60*60*1e3&&(clearTimeout(R),clearTimeout(L[l]),R=setTimeout(()=>this.reset(),w.exp-Date.now())),JSON.stringify([x(t),w])),B=t=>{const u=JSON.parse(t);return w=u[1],F(u[0])};let U=!0;if($[l]=d,c[`atom/${m}`]&&(c[l]||(c[l]=`[${c[`atom/${m}`]}]`),delete c[`atom/${m}`]),a=()=>{if(a=S,U){U=!1;try{g(l in c?B(c[l]):r)}catch{console.warn("Invalid json value",c[l])}}return S()},h=t=>{if(t===r){this.reset();return}c[l]=k(t)},this.reset=()=>{delete c[l],A(r,!0)},N&&D[l]!==void 0&&console.warn("Duplicate Atom key",m),V)if(z){let t=!1,u;P[l]=this,D[l]=()=>{clearTimeout(u),u=setTimeout(()=>t=!1,10),!t&&(t=!0,c[l]=k(S()))}}else D[l]=t=>{if(t.newValue===null){this.reset();return}try{A(B(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}};return d}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=P[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const c=window.localStorage,D={},P={},ee=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=D[e.key])==null||r.call(D,e)});const te=c.setItem.bind(c),ne=c.removeItem.bind(c);c.setItem=(e,r)=>{P[e]===void 0&&te.call(c,e,r)},c.removeItem=e=>{P[e]===void 0&&ne.call(c,e)};const se=/"exp":(\d+)\.2866/,H="atom\\",$={},L={};setTimeout(()=>{Object.keys(c).forEach(e=>{var i;if(!e.startsWith(H)||typeof c[e]!="string")return;const r=+((i=c[e].match(se))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(L[e]=setTimeout(()=>{$[e]?$[e].reset():delete c[e]},r-Date.now()))})},1e3),f.Atom=G,f.atom=v,f.configureAtomaric=q,f.configuredOptions=E,f.useAtom=Z,f.useAtomDo=Y,f.useAtomGet=X,f.useAtomSet=M,f.useAtomSetDeferred=Q,f.useAtomValue=J,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.35",
4
+ "version": "0.0.36",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",