atomaric 0.0.36 → 0.0.38

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