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