atomaric 0.0.45 → 0.0.61

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