atomaric 0.0.36 → 0.0.37

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