atomaric 0.0.40 → 0.0.44

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