atomaric 0.0.32 → 0.0.34

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