atomaric 0.0.35 → 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
@@ -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 h = null;
8
+ typeof e == "number" ? h = j(
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" ? h = j(
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) ? h = j(
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
- const g = s.get().slice(), f = g.indexOf(n);
35
- 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);
36
41
  }
37
42
  })
38
- ) : e instanceof Set ? a = _(
43
+ ) : e instanceof Set ? h = j(
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 && (h = j(
61
66
  r,
62
67
  (s) => ({
63
68
  setPartial: (n) => s.set((c) => ({
@@ -68,167 +73,165 @@ 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 = ".") => {
72
- if (n.includes(g)) {
73
- let f = n.split(g);
74
- const w = f[f.length - 1];
75
- f = f.slice(0, -1);
76
- const D = { ...s.get() };
77
- let d = D;
78
- for (const S of f) {
79
- const A = d[S];
80
- if (A == null || typeof A != "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") {
81
87
  s.do.setPartial({ [n]: typeof c == "function" ? c(void 0) : c });
82
88
  return;
83
89
  }
84
- d = d[S] = Array.isArray(A) ? [...A] : { ...A };
90
+ g = g[w] = Array.isArray(f) ? [...f] : { ...f };
85
91
  }
86
- d[w] = typeof c == "function" ? c(d[w]) : c, s.set(D);
92
+ g[T] = typeof c == "function" ? c(g[T]) : c, s.set(b);
87
93
  } else s.do.setPartial({ [n]: c });
88
94
  }
89
95
  })
90
96
  ));
91
- 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(
92
98
  (s, n) => r.set(s, n),
93
99
  () => r.get(),
94
100
  r,
95
101
  (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 {
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);
107
+ class q {
102
108
  constructor(r, i) {
103
- const a = (t) => n = t, h = () => n, p = /* @__PURE__ */ new Set(), s = (t) => t(f());
104
- let n = r, c, g = () => {
105
- }, f = () => h(), w = null, D = () => {
106
- const t = B(r, d, i);
107
- 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;
108
114
  };
109
- const d = new Proxy(this, {
110
- get: (t, u) => u === "do" ? D() : t[u],
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);
115
+ const b = new Proxy(this, {
116
+ get: (t, u) => u === "do" ? T() : t[u],
117
+ set: Q
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);
116
122
  try {
117
- P.postMessage({ key: l, value: h() });
123
+ N.postMessage({ key: l, value: S() });
118
124
  } catch {
119
125
  }
120
- u !== !0 && g(b);
126
+ u !== !0 && p(y);
121
127
  }
122
128
  };
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);
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);
125
131
  }), this.reset = () => {
126
- S(r, !0), p.forEach(s, this);
132
+ g(r, !0), A.forEach(s, this);
127
133
  };
128
- const A = (t, u) => {
129
- S(t, u), c = void 0;
134
+ const x = (t, u) => {
135
+ g(t, u), c = void 0;
130
136
  };
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) => {
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) => {
135
141
  if (t instanceof Set) return Array.from(t);
136
142
  throw console.error(t), "The value is not Set instance";
137
143
  } : (t) => t;
138
144
  if (typeof i == "string")
139
- y = i;
145
+ w = i;
140
146
  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) => {
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) => {
144
150
  const u = JSON.parse(t);
145
- return w = u[1], $(u[0]);
151
+ return d = u[1], C(u[0]);
146
152
  };
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;
153
+ let H = !0;
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) {
156
+ H = !1;
151
157
  try {
152
- a(l in o ? F(o[l]) : r);
158
+ h(l in o ? J(o[l]) : r);
153
159
  } catch {
154
160
  console.warn("Invalid json value", o[l]);
155
161
  }
156
162
  }
157
- return h();
158
- }, g = (t) => {
163
+ return S();
164
+ }, p = (t) => {
159
165
  if (t === r) {
160
166
  this.reset();
161
167
  return;
162
168
  }
163
- o[l] = V(t);
169
+ o[l] = F(t);
164
170
  }, this.reset = () => {
165
- delete o[l], S(r, !0);
166
- }, I && x[l] !== void 0 && console.warn("Duplicate Atom key", y), E)
167
- if (C) {
171
+ delete o[l], g(r, !0);
172
+ }, _ && D[l] !== void 0 && console.warn("Duplicate Atom key", w), E)
173
+ if (P) {
168
174
  let t = !1, u;
169
- m[l] = this, x[l] = () => {
170
- clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = V(h()));
175
+ I[l] = this, D[l] = () => {
176
+ clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = F(S()));
171
177
  };
172
178
  } else
173
- x[l] = (t) => {
179
+ D[l] = (t) => {
174
180
  if (t.newValue === null) {
175
181
  this.reset();
176
182
  return;
177
183
  }
178
184
  try {
179
- S(F(t.newValue));
185
+ g(J(t.newValue));
180
186
  } catch {
181
187
  console.warn("Invalid json value", t.newValue);
182
188
  }
183
189
  };
184
- return d;
190
+ return b;
185
191
  }
186
192
  }
187
- let P;
193
+ let N;
188
194
  try {
189
- P = new BroadcastChannel("updateHere"), P.addEventListener("message", (e) => {
195
+ N = new BroadcastChannel("updateHere"), N.addEventListener("message", (e) => {
190
196
  var r;
191
- (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);
192
198
  });
193
199
  } catch {
194
200
  }
195
- const o = window.localStorage, x = {}, m = {}, W = (e, r) => {
201
+ const o = window.localStorage, D = {}, I = {}, Q = (e, r) => {
196
202
  throw `${r} is readonly property`;
197
203
  };
198
204
  window.addEventListener("storage", (e) => {
199
205
  var r;
200
- 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);
201
207
  });
202
- const q = o.setItem.bind(o), Q = o.removeItem.bind(o);
208
+ const X = o.setItem.bind(o), Y = o.removeItem.bind(o);
203
209
  o.setItem = (e, r) => {
204
- m[e] === void 0 && q.call(o, e, r);
210
+ I[e] === void 0 && X.call(o, e, r);
205
211
  };
206
212
  o.removeItem = (e) => {
207
- m[e] === void 0 && Q.call(o, e);
213
+ I[e] === void 0 && Y.call(o, e);
208
214
  };
209
- const X = /"exp":(\d+)\.2866/, L = "atom\\", N = {}, M = {};
215
+ const Z = /"exp":(\d+)\.2866/, R = "atom\\", z = {}, k = {};
210
216
  setTimeout(() => {
211
217
  Object.keys(o).forEach((e) => {
212
218
  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];
219
+ if (!e.startsWith(R) || typeof o[e] != "string") return;
220
+ const r = +((i = o[e].match(Z)) == null ? void 0 : i[1]);
221
+ r && r - Date.now() < 24 * 60 * 60 * 1e3 && (k[e] = setTimeout(() => {
222
+ z[e] ? z[e].reset() : delete o[e];
217
223
  }, r - Date.now()));
218
224
  });
219
225
  }, 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
226
  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
227
+ q as Atom,
228
+ se as atom,
229
+ v as configureAtomaric,
230
+ $ as configuredOptions,
231
+ te as useAtom,
232
+ ee as useAtomDo,
233
+ O as useAtomGet,
234
+ G as useAtomSet,
235
+ K as useAtomSetDeferred,
236
+ B as useAtomValue
234
237
  };
@@ -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,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.35",
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>