atomaric 0.0.71 → 0.0.72
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 +166 -162
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/src/do.classes/Array.test.ts +1 -1
- package/src/do.classes/Array.ts +20 -14
- package/src/do.classes/Boolean.ts +2 -2
- package/src/do.classes/Map.ts +15 -9
- package/src/do.classes/Number.ts +2 -2
- package/src/do.classes/Object.ts +9 -9
- package/src/do.classes/Set.ts +12 -8
- package/src/do.classes/_Update.ts +1 -1
package/build/atomaric.js
CHANGED
|
@@ -3,200 +3,204 @@ let k = () => {
|
|
|
3
3
|
};
|
|
4
4
|
const T = {}, Se = (t) => {
|
|
5
5
|
T.useSyncExternalStore = k = t.useSyncExternalStore, T.keyPathSeparator = t.keyPathSeparator, T.securifyKeyLevel = t.securifyKeyLevel, T.securifyValueLevel = t.securifyValueLevel;
|
|
6
|
-
}, ne = (t) => k(t.subscribe, t.get),
|
|
6
|
+
}, ne = (t) => k(t.subscribe, t.get), ie = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), ie(t)], Te = (t, e) => new pe(t, e);
|
|
7
7
|
class _ {
|
|
8
8
|
constructor(e) {
|
|
9
9
|
if (e)
|
|
10
10
|
return new Proxy(this, {
|
|
11
|
-
get: (
|
|
11
|
+
get: (i, s) => s in this ? i[s] : e[s]
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
const ce = (t, e) => {
|
|
16
|
-
const
|
|
16
|
+
const i = (s, n, o) => new Proxy(s, {
|
|
17
17
|
get: (l, h, p) => {
|
|
18
|
-
const
|
|
19
|
-
return typeof
|
|
20
|
-
Array.isArray(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
) :
|
|
18
|
+
const g = o === 0 && e.get != null ? e.get(l, h, p) : l[h];
|
|
19
|
+
return typeof g == "object" && g !== null ? i(
|
|
20
|
+
Array.isArray(g) ? g.slice(0) : { ...g },
|
|
21
|
+
n.concat(Array.isArray(l) ? +h : h),
|
|
22
|
+
o + 1
|
|
23
|
+
) : g;
|
|
24
24
|
},
|
|
25
|
-
set: (l, h, p) => (e.onSet(l,
|
|
25
|
+
set: (l, h, p) => (e.onSet(l, n, h, p, l[h]) && (l[h] = p), !0)
|
|
26
26
|
});
|
|
27
|
-
return
|
|
27
|
+
return i(t, [], 0);
|
|
28
28
|
};
|
|
29
29
|
class O extends _ {
|
|
30
30
|
constructor() {
|
|
31
|
-
super(...arguments), this.updateValue = (e,
|
|
31
|
+
super(...arguments), this.updateValue = (e, i) => {
|
|
32
32
|
const s = Array.isArray(e) ? e.slice(0) : { ...e };
|
|
33
|
-
let
|
|
34
|
-
const
|
|
35
|
-
onSet: (l, h, p,
|
|
36
|
-
if (
|
|
37
|
-
let
|
|
38
|
-
|
|
33
|
+
let n = !1;
|
|
34
|
+
const o = ce(e, {
|
|
35
|
+
onSet: (l, h, p, g, w) => {
|
|
36
|
+
if (g === w) return !0;
|
|
37
|
+
let m = s;
|
|
38
|
+
n = !0;
|
|
39
39
|
for (const f of h) {
|
|
40
|
-
const
|
|
41
|
-
|
|
40
|
+
const d = m[f];
|
|
41
|
+
m = m[f] = Array.isArray(d) ? d.slice() : { ...d };
|
|
42
42
|
}
|
|
43
|
-
return
|
|
43
|
+
return m[p] = g, !0;
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
|
-
return
|
|
46
|
+
return i(o), n ? s : e;
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
class
|
|
51
|
-
constructor(e,
|
|
52
|
-
super(
|
|
53
|
-
this.
|
|
50
|
+
class oe extends O {
|
|
51
|
+
constructor(e, i) {
|
|
52
|
+
super(i), this.a = e, this.push = (...s) => {
|
|
53
|
+
s.length !== 0 && this.a.set(this.a.get().concat(s));
|
|
54
54
|
}, this.unshift = (...s) => {
|
|
55
|
-
this.
|
|
55
|
+
s.length !== 0 && this.a.set(s.concat(this.a.get()));
|
|
56
56
|
}, this.update = (s) => {
|
|
57
|
-
const
|
|
58
|
-
|
|
57
|
+
const n = this.a.get(), o = this.updateValue(n, s);
|
|
58
|
+
o !== n && this.a.set(o);
|
|
59
59
|
}, this.filter = (s) => {
|
|
60
|
-
this.
|
|
60
|
+
const n = this.a.get().filter(s ?? ae);
|
|
61
|
+
n.length !== this.a.get().length && this.a.set(n);
|
|
61
62
|
}, this.add = (s) => {
|
|
62
|
-
this.
|
|
63
|
-
}, this.
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
}, this.toggle = (s,
|
|
69
|
-
const
|
|
70
|
-
l < 0 ?
|
|
63
|
+
this.a.get().includes(s) || this.a.set(this.a.get().concat([s]));
|
|
64
|
+
}, this.removeFirst = (s) => {
|
|
65
|
+
const n = this.a.get().indexOf(s);
|
|
66
|
+
if (n < 0) return;
|
|
67
|
+
const o = this.a.get().slice(0);
|
|
68
|
+
o.splice(n, 1), this.a.set(o);
|
|
69
|
+
}, this.toggle = (s, n) => {
|
|
70
|
+
const o = this.a.get().slice(), l = o.indexOf(s);
|
|
71
|
+
l < 0 ? n ? o.unshift(s) : o.push(s) : o.splice(l, 1), this.a.set(o);
|
|
71
72
|
};
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
const ae = (t) => t;
|
|
75
76
|
class ue extends _ {
|
|
76
|
-
constructor(e,
|
|
77
|
-
super(
|
|
78
|
-
this.
|
|
77
|
+
constructor(e, i) {
|
|
78
|
+
super(i), this.a = e, this.toggle = () => {
|
|
79
|
+
this.a.set(!this.a.get());
|
|
79
80
|
};
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
83
|
class le extends _ {
|
|
83
|
-
constructor(e,
|
|
84
|
-
super(
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
constructor(e, i) {
|
|
85
|
+
super(i), this.a = e, this.setValue = (s, n) => {
|
|
86
|
+
if (this.a.get().get(s) === n) return;
|
|
87
|
+
const o = new Map(this.a.get());
|
|
88
|
+
o.set(s, n), this.a.set(o);
|
|
87
89
|
}, this.delete = (s) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
if (!this.a.get().has(s)) return;
|
|
91
|
+
const n = new Map(this.a.get());
|
|
92
|
+
n.delete(s), this.a.set(n);
|
|
93
|
+
}, this.toggle = (s, n) => {
|
|
94
|
+
const o = new Map(this.a.get());
|
|
95
|
+
o.has(s) ? o.delete(s) : o.set(s, n), this.a.set(o);
|
|
93
96
|
}, this.clear = () => {
|
|
94
|
-
this.
|
|
95
|
-
}, this.
|
|
97
|
+
this.a.set(/* @__PURE__ */ new Map());
|
|
98
|
+
}, this.a = e;
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
class fe extends _ {
|
|
99
|
-
constructor(e,
|
|
100
|
-
super(
|
|
101
|
-
this.
|
|
102
|
+
constructor(e, i) {
|
|
103
|
+
super(i), this.a = e, this.increment = (s) => {
|
|
104
|
+
this.a.set(+this.a.get() + (s ?? 1));
|
|
102
105
|
};
|
|
103
106
|
}
|
|
104
107
|
}
|
|
105
108
|
class he extends O {
|
|
106
|
-
constructor(e,
|
|
107
|
-
super(
|
|
108
|
-
...
|
|
109
|
-
...typeof s == "function" ? s(this.
|
|
109
|
+
constructor(e, i) {
|
|
110
|
+
super(i), this.a = e, this.setPartial = (s) => this.a.set((n) => ({
|
|
111
|
+
...n,
|
|
112
|
+
...typeof s == "function" ? s(this.a.get()) : s
|
|
110
113
|
})), this.update = (s) => {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
}, this.setDeepPartial = (s,
|
|
114
|
+
const n = this.a.get(), o = this.updateValue(n, s);
|
|
115
|
+
o !== n && this.a.set(o);
|
|
116
|
+
}, this.setDeepPartial = (s, n, o, l = T.keyPathSeparator || ".") => {
|
|
114
117
|
if (!l) return;
|
|
115
118
|
if (s.includes(l)) {
|
|
116
|
-
let
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
let f =
|
|
121
|
-
for (const
|
|
122
|
-
|
|
123
|
-
const D = f[
|
|
119
|
+
let g = s.split(l);
|
|
120
|
+
const w = g[g.length - 1];
|
|
121
|
+
g = g.slice(0, -1);
|
|
122
|
+
const m = { ...this.a.get() };
|
|
123
|
+
let f = m, d = o;
|
|
124
|
+
for (const y of g) {
|
|
125
|
+
d = d == null ? void 0 : d[Array.isArray(d) ? "0" : y];
|
|
126
|
+
const D = f[v(f, y)] ?? (Array.isArray(d) ? [] : {});
|
|
124
127
|
if (D == null || typeof D != "object") {
|
|
125
|
-
if (
|
|
126
|
-
const S = typeof
|
|
127
|
-
this.
|
|
128
|
+
if (o == null) throw "Incorrect path for setDeepPartial";
|
|
129
|
+
const S = typeof n == "function" ? n(void 0) : n;
|
|
130
|
+
this.a.get()[s] !== S && this.setPartial({ [s]: S });
|
|
128
131
|
return;
|
|
129
132
|
}
|
|
130
|
-
f = f[
|
|
133
|
+
f = f[v(f, y)] = Array.isArray(D) ? [...D] : { ...D };
|
|
131
134
|
}
|
|
132
|
-
const E = f[
|
|
133
|
-
f[
|
|
135
|
+
const E = f[w];
|
|
136
|
+
f[w] = typeof n == "function" ? n(f[w]) : n, E !== f[w] && this.a.set(m);
|
|
134
137
|
return;
|
|
135
138
|
}
|
|
136
|
-
const h = this.
|
|
139
|
+
const h = this.a.get()[s], p = typeof n == "function" ? n(h) : n;
|
|
137
140
|
p !== h && this.setPartial({ [s]: p });
|
|
138
141
|
};
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
|
-
const
|
|
142
|
-
class
|
|
143
|
-
constructor(e,
|
|
144
|
-
super(
|
|
145
|
-
this.
|
|
144
|
+
const v = (t, e) => Array.isArray(t) ? `${+e}` : e;
|
|
145
|
+
class de extends _ {
|
|
146
|
+
constructor(e, i) {
|
|
147
|
+
super(i), this.a = e, this.add = (s) => {
|
|
148
|
+
this.a.get().has(s) || this.a.set(new Set(this.a.get()).add(s));
|
|
146
149
|
}, this.delete = (s) => {
|
|
147
|
-
|
|
148
|
-
|
|
150
|
+
if (!this.a.get().has(s)) return;
|
|
151
|
+
const n = new Set(this.a.get());
|
|
152
|
+
n.delete(s), this.a.set(n);
|
|
149
153
|
}, this.toggle = (s) => {
|
|
150
|
-
const
|
|
151
|
-
|
|
154
|
+
const n = new Set(this.a.get());
|
|
155
|
+
n.has(s) ? n.delete(s) : n.add(s), this.a.set(n);
|
|
152
156
|
}, this.clear = () => {
|
|
153
|
-
this.
|
|
154
|
-
}, this.
|
|
157
|
+
this.a.set(/* @__PURE__ */ new Set());
|
|
158
|
+
}, this.a = e;
|
|
155
159
|
}
|
|
156
160
|
}
|
|
157
|
-
const
|
|
158
|
-
const s = typeof
|
|
159
|
-
(
|
|
161
|
+
const ge = (t, e, i) => {
|
|
162
|
+
const s = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
163
|
+
(o, l) => e.set(o, l),
|
|
160
164
|
() => e.get(),
|
|
161
165
|
e,
|
|
162
|
-
(
|
|
163
|
-
) : null,
|
|
164
|
-
return s && Object.keys(s).forEach((
|
|
166
|
+
(o, l, h) => e.setDeferred(o, l, h)
|
|
167
|
+
) : null, n = {};
|
|
168
|
+
return s && Object.keys(s).forEach((o) => Object.defineProperty(n, o, { get: () => s[o] })), typeof t == "number" ? new fe(e, s) : typeof t == "boolean" ? new ue(e, s) : Array.isArray(t) ? new oe(e, s) : t instanceof Set ? new de(e, s) : t instanceof Map ? new le(e, s) : t instanceof Object ? new he(e, s) : new _(s);
|
|
165
169
|
};
|
|
166
170
|
class pe {
|
|
167
|
-
constructor(e,
|
|
171
|
+
constructor(e, i) {
|
|
168
172
|
e = typeof e == "function" ? e() : e;
|
|
169
|
-
const s = (r) =>
|
|
170
|
-
let h = !0, p = !1,
|
|
171
|
-
}, f = () =>
|
|
172
|
-
const r =
|
|
173
|
+
const s = (r) => g = r, n = () => g, o = /* @__PURE__ */ new Set(), l = (r) => r(f());
|
|
174
|
+
let h = !0, p = !1, g = e, w, m = () => {
|
|
175
|
+
}, f = () => n(), d = null, E = () => {
|
|
176
|
+
const r = ge(e, y, i);
|
|
173
177
|
return E = () => r, r;
|
|
174
178
|
};
|
|
175
|
-
const
|
|
179
|
+
const y = new Proxy(this, {
|
|
176
180
|
get: (r, u) => u === "do" ? E() : r[u],
|
|
177
|
-
set:
|
|
181
|
+
set: we
|
|
178
182
|
}), D = () => {
|
|
179
|
-
h = !0, p !== !0 &&
|
|
183
|
+
h = !0, p !== !0 && m(f()), p = !1;
|
|
180
184
|
try {
|
|
181
|
-
|
|
185
|
+
H.postMessage({ key: a, value: n() });
|
|
182
186
|
} catch {
|
|
183
187
|
}
|
|
184
188
|
}, S = (r, u) => {
|
|
185
189
|
const x = typeof r == "function" ? r(f()) : r;
|
|
186
|
-
x !== f() && (s(x), p = u, h && (h = !1,
|
|
190
|
+
x !== f() && (s(x), p = u, h && (h = !1, o.forEach(l), queueMicrotask(D)));
|
|
187
191
|
};
|
|
188
|
-
this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e ===
|
|
189
|
-
|
|
192
|
+
this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === n(), this.subscribe = (r) => (o.add(r), () => {
|
|
193
|
+
o.delete(r);
|
|
190
194
|
}), this.reset = () => {
|
|
191
195
|
S(e, !0);
|
|
192
196
|
};
|
|
193
197
|
const re = (r, u) => {
|
|
194
|
-
S(r, u),
|
|
198
|
+
S(r, u), w = void 0;
|
|
195
199
|
};
|
|
196
200
|
if (this.setDeferred = (r, u = 500, x, b = !0) => {
|
|
197
|
-
b &&
|
|
198
|
-
},
|
|
199
|
-
let
|
|
201
|
+
b && w === void 0 && S(r, x), clearTimeout(w), w = setTimeout(re, u, r, x);
|
|
202
|
+
}, i == null) return y;
|
|
203
|
+
let A = null, j = null, q = !0, F = !0, B = !1, $ = 0, L = 0, K = -1, M = e instanceof Set || e instanceof Map ? (r) => new Set(r) : (r) => r, C = e instanceof Set ? (r) => {
|
|
200
204
|
if (r instanceof Set) return Array.from(r);
|
|
201
205
|
throw console.error(r), "The value is not Set instance";
|
|
202
206
|
} : e instanceof Map ? (r) => {
|
|
@@ -206,20 +210,20 @@ class pe {
|
|
|
206
210
|
}
|
|
207
211
|
throw console.error(r), "The value is not Set instance";
|
|
208
212
|
} : (r) => r;
|
|
209
|
-
if (typeof
|
|
210
|
-
|
|
211
|
-
else if ("storeKey" in
|
|
212
|
-
q =
|
|
213
|
-
else return
|
|
214
|
-
const N = $ ? I(
|
|
213
|
+
if (typeof i == "string")
|
|
214
|
+
A = i;
|
|
215
|
+
else if ("storeKey" in i)
|
|
216
|
+
q = i.warnOnDuplicateStoreKey ?? q, F = i.listenStorageChanges ?? F, A = i.storeKey, M = i.unzipValue ?? M, C = i.zipValue ?? C, B = i.unchangable ?? B, $ = i.securifyKeyLevel ?? T.securifyKeyLevel ?? $, L = i.securifyValueLevel ?? T.securifyValueLevel ?? L, j = i.exp ?? j;
|
|
217
|
+
else return y;
|
|
218
|
+
const N = $ ? I(A, $) : A, a = `${L ? J : P}${N}`;
|
|
215
219
|
if ($) {
|
|
216
|
-
const r = `${P}${
|
|
220
|
+
const r = `${P}${A}`;
|
|
217
221
|
r in c && (c[a] = c[r], delete c[r]);
|
|
218
222
|
} else {
|
|
219
|
-
const r = `${P}${I(
|
|
223
|
+
const r = `${P}${I(A, $)}`;
|
|
220
224
|
r in c && (c[a] = c[r], delete c[r]);
|
|
221
225
|
}
|
|
222
|
-
const W = j === null || !(j(
|
|
226
|
+
const W = j === null || !(j(y, a in c) instanceof Date) ? (r) => I([C(r)], 0) : (r) => (d ?? (d = {}), d.exp = j(y, a in c).getTime(), d.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(K), clearTimeout(ee[a]), K = setTimeout(() => this.reset(), d.exp - Date.now())), d.exp = Math.trunc(d.exp / 1e3), I([C(r), d], 0));
|
|
223
227
|
if (L) {
|
|
224
228
|
const r = `${P}${N}`, u = C, x = M;
|
|
225
229
|
if (C = (b) => {
|
|
@@ -229,7 +233,7 @@ class pe {
|
|
|
229
233
|
return delete c[a], "";
|
|
230
234
|
}
|
|
231
235
|
}, r in c) {
|
|
232
|
-
const b = `${P}${I(
|
|
236
|
+
const b = `${P}${I(A, $)}`;
|
|
233
237
|
try {
|
|
234
238
|
c[b] = W(M(z(c[r], 0)[0])), delete c[r];
|
|
235
239
|
} catch {
|
|
@@ -242,23 +246,23 @@ class pe {
|
|
|
242
246
|
return delete c[a], "";
|
|
243
247
|
}
|
|
244
248
|
};
|
|
245
|
-
} else delete c[`${
|
|
246
|
-
const
|
|
249
|
+
} else delete c[`${J}${N}`];
|
|
250
|
+
const X = (r) => {
|
|
247
251
|
const u = z(r, 0);
|
|
248
|
-
return
|
|
252
|
+
return d = u[1], M(u[0]);
|
|
249
253
|
};
|
|
250
|
-
let
|
|
251
|
-
if (
|
|
252
|
-
if (f =
|
|
253
|
-
|
|
254
|
+
let Y = !0;
|
|
255
|
+
if (G[a] = y, c[`atom/${A}`] && (c[a] || (c[a] = `[${c[`atom/${A}`]}]`), delete c[`atom/${A}`]), f = () => {
|
|
256
|
+
if (f = n, Y) {
|
|
257
|
+
Y = !1;
|
|
254
258
|
try {
|
|
255
|
-
s(a in c ?
|
|
259
|
+
s(a in c ? X(c[a]) : e);
|
|
256
260
|
} catch {
|
|
257
261
|
console.warn("Invalid json value", c[a]);
|
|
258
262
|
}
|
|
259
263
|
}
|
|
260
|
-
return
|
|
261
|
-
},
|
|
264
|
+
return n();
|
|
265
|
+
}, m = (r) => {
|
|
262
266
|
if (r === e) {
|
|
263
267
|
this.reset();
|
|
264
268
|
return;
|
|
@@ -266,11 +270,11 @@ class pe {
|
|
|
266
270
|
c[a] = W(r);
|
|
267
271
|
}, this.reset = () => {
|
|
268
272
|
delete c[a], S(e, !0);
|
|
269
|
-
}, q && V[a] !== void 0 && console.warn("Duplicate Atom key",
|
|
270
|
-
if (
|
|
273
|
+
}, q && V[a] !== void 0 && console.warn("Duplicate Atom key", A), F)
|
|
274
|
+
if (B) {
|
|
271
275
|
let r = !1, u;
|
|
272
276
|
U[a] = this, V[a] = () => {
|
|
273
|
-
clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = W(
|
|
277
|
+
clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, c[a] = W(n()));
|
|
274
278
|
};
|
|
275
279
|
} else
|
|
276
280
|
V[a] = (r) => {
|
|
@@ -279,57 +283,57 @@ class pe {
|
|
|
279
283
|
return;
|
|
280
284
|
}
|
|
281
285
|
try {
|
|
282
|
-
S(
|
|
286
|
+
S(X(r.newValue));
|
|
283
287
|
} catch {
|
|
284
288
|
console.warn("Invalid json value", r.newValue);
|
|
285
289
|
}
|
|
286
290
|
};
|
|
287
|
-
return
|
|
291
|
+
return y;
|
|
288
292
|
}
|
|
289
293
|
}
|
|
290
|
-
let
|
|
294
|
+
let H;
|
|
291
295
|
try {
|
|
292
|
-
|
|
296
|
+
H = new BroadcastChannel("updateHere"), H.addEventListener("message", (t) => {
|
|
293
297
|
var e;
|
|
294
298
|
(e = U[t.data.key]) == null || e.set(t.data.value, !0);
|
|
295
299
|
});
|
|
296
300
|
} catch {
|
|
297
301
|
}
|
|
298
|
-
const c = localStorage, V = {}, U = {},
|
|
302
|
+
const c = localStorage, V = {}, U = {}, we = (t, e) => {
|
|
299
303
|
throw `${e} is readonly property`;
|
|
300
304
|
};
|
|
301
305
|
window.addEventListener("storage", (t) => {
|
|
302
306
|
var e;
|
|
303
307
|
t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
|
|
304
308
|
});
|
|
305
|
-
const
|
|
309
|
+
const ye = c.setItem.bind(c), Ae = c.removeItem.bind(c);
|
|
306
310
|
c.setItem = (t, e) => {
|
|
307
|
-
U[t] === void 0 &&
|
|
311
|
+
U[t] === void 0 && ye.call(c, t, e);
|
|
308
312
|
};
|
|
309
313
|
c.removeItem = (t) => {
|
|
310
|
-
U[t] === void 0 &&
|
|
314
|
+
U[t] === void 0 && Ae.call(c, t);
|
|
311
315
|
};
|
|
312
|
-
const
|
|
316
|
+
const me = /"exp":\s*(\d+)/, P = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
|
|
313
317
|
for (let t = 54; t < 80; t++) {
|
|
314
318
|
if (t === 68 || t === 72 || t === 55) continue;
|
|
315
|
-
const e = String.fromCharCode(t + 43).toUpperCase(),
|
|
316
|
-
|
|
319
|
+
const e = String.fromCharCode(t + 43).toUpperCase(), i = String.fromCharCode(t + 43).toLowerCase();
|
|
320
|
+
Q[e] = i, Q[i] = e;
|
|
317
321
|
}
|
|
318
|
-
const se = (t) =>
|
|
322
|
+
const se = (t) => Q[t] ?? t, I = /* @__PURE__ */ (() => {
|
|
319
323
|
const t = [
|
|
320
324
|
(e) => JSON.stringify(e),
|
|
321
325
|
(e) => btoa(encodeURI(t[0](e))),
|
|
322
326
|
(e) => {
|
|
323
|
-
const
|
|
324
|
-
return `${
|
|
327
|
+
const i = t[1](e);
|
|
328
|
+
return `${i.slice(0, R)}${i.slice(R).replace(te, se)}`;
|
|
325
329
|
},
|
|
326
330
|
(e) => btoa(t[2](e))
|
|
327
331
|
];
|
|
328
|
-
return (e,
|
|
332
|
+
return (e, i) => {
|
|
329
333
|
try {
|
|
330
|
-
return t[
|
|
334
|
+
return t[i](e);
|
|
331
335
|
} catch (s) {
|
|
332
|
-
if (
|
|
336
|
+
if (i === 0) throw s;
|
|
333
337
|
return t[0](e);
|
|
334
338
|
}
|
|
335
339
|
};
|
|
@@ -342,11 +346,11 @@ const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
|
|
|
342
346
|
),
|
|
343
347
|
(e) => t[2](atob(e))
|
|
344
348
|
];
|
|
345
|
-
return (e,
|
|
349
|
+
return (e, i) => {
|
|
346
350
|
try {
|
|
347
|
-
return t[
|
|
351
|
+
return t[i](e);
|
|
348
352
|
} catch (s) {
|
|
349
|
-
if (
|
|
353
|
+
if (i === 0) throw s;
|
|
350
354
|
return t[0](e);
|
|
351
355
|
}
|
|
352
356
|
};
|
|
@@ -354,30 +358,30 @@ const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
|
|
|
354
358
|
setTimeout(() => {
|
|
355
359
|
Object.keys(c).forEach((t) => {
|
|
356
360
|
var s;
|
|
357
|
-
if (typeof c[t] != "string" || !t.startsWith(P) && !t.startsWith(
|
|
358
|
-
const e = +((s = c[t].match(
|
|
361
|
+
if (typeof c[t] != "string" || !t.startsWith(P) && !t.startsWith(J)) return;
|
|
362
|
+
const e = +((s = c[t].match(me)) == null ? void 0 : s[1]);
|
|
359
363
|
if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
|
|
360
|
-
const
|
|
361
|
-
!Array.isArray(
|
|
362
|
-
|
|
364
|
+
const i = z(c[t], 0);
|
|
365
|
+
!Array.isArray(i) || i[1] == null || !("exp" in i[1]) || i[1].exp !== e || (ee[t] = setTimeout(() => {
|
|
366
|
+
G[t] ? G[t].reset() : delete c[t];
|
|
363
367
|
}, e * 1e3 - Date.now()));
|
|
364
368
|
});
|
|
365
369
|
}, 1e3);
|
|
366
370
|
export {
|
|
367
371
|
pe as Atom,
|
|
368
|
-
|
|
372
|
+
oe as AtomArrayDoActions,
|
|
369
373
|
ue as AtomBooleanDoActions,
|
|
370
374
|
le as AtomMapDoActions,
|
|
371
375
|
fe as AtomNumberDoActions,
|
|
372
376
|
he as AtomObjectDoActions,
|
|
373
|
-
|
|
377
|
+
de as AtomSetDoActions,
|
|
374
378
|
Te as atom,
|
|
375
379
|
Se as configureAtomaric,
|
|
376
380
|
T as configuredOptions,
|
|
377
381
|
$e as useAtom,
|
|
378
382
|
De as useAtomDo,
|
|
379
383
|
be as useAtomGet,
|
|
380
|
-
|
|
384
|
+
ie as useAtomSet,
|
|
381
385
|
xe as useAtomSetDeferred,
|
|
382
386
|
ne as useAtomValue
|
|
383
387
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(l,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(l=typeof globalThis<"u"?globalThis:l||self,j(l.atomaric={}))})(this,function(l){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const $={},
|
|
1
|
+
(function(l,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(l=typeof globalThis<"u"?globalThis:l||self,j(l.atomaric={}))})(this,function(l){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const $={},de=t=>{$.useSyncExternalStore=j=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},Y=t=>j(t.subscribe,t.get),Z=t=>t.set,ge=t=>t.setDeferred,Ae=t=>t.get,me=t=>t.do,ye=t=>[Y(t),Z(t)],pe=(t,e)=>new oe(t,e);class L{constructor(e){if(e)return new Proxy(this,{get:(o,s)=>s in this?o[s]:e[s]})}}const we=(t,e)=>{const o=(s,r,c)=>new Proxy(s,{get:(f,d,m)=>{const A=c===0&&e.get!=null?e.get(f,d,m):f[d];return typeof A=="object"&&A!==null?o(Array.isArray(A)?A.slice(0):{...A},r.concat(Array.isArray(f)?+d:d),c+1):A},set:(f,d,m)=>(e.onSet(f,r,d,m,f[d])&&(f[d]=m),!0)});return o(t,[],0)};class v extends L{constructor(){super(...arguments),this.updateValue=(e,o)=>{const s=Array.isArray(e)?e.slice(0):{...e};let r=!1;const c=we(e,{onSet:(f,d,m,A,y)=>{if(A===y)return!0;let S=s;r=!0;for(const h of d){const g=S[h];S=S[h]=Array.isArray(g)?g.slice():{...g}}return S[m]=A,!0}});return o(c),r?s:e}}}class k extends v{constructor(e,o){super(o),this.a=e,this.push=(...s)=>{s.length!==0&&this.a.set(this.a.get().concat(s))},this.unshift=(...s)=>{s.length!==0&&this.a.set(s.concat(this.a.get()))},this.update=s=>{const r=this.a.get(),c=this.updateValue(r,s);c!==r&&this.a.set(c)},this.filter=s=>{const r=this.a.get().filter(s??Se);r.length!==this.a.get().length&&this.a.set(r)},this.add=s=>{this.a.get().includes(s)||this.a.set(this.a.get().concat([s]))},this.removeFirst=s=>{const r=this.a.get().indexOf(s);if(r<0)return;const c=this.a.get().slice(0);c.splice(r,1),this.a.set(c)},this.toggle=(s,r)=>{const c=this.a.get().slice(),f=c.indexOf(s);f<0?r?c.unshift(s):c.push(s):c.splice(f,1),this.a.set(c)}}}const Se=t=>t;class O extends L{constructor(e,o){super(o),this.a=e,this.toggle=()=>{this.a.set(!this.a.get())}}}class ee extends L{constructor(e,o){super(o),this.a=e,this.setValue=(s,r)=>{if(this.a.get().get(s)===r)return;const c=new Map(this.a.get());c.set(s,r),this.a.set(c)},this.delete=s=>{if(!this.a.get().has(s))return;const r=new Map(this.a.get());r.delete(s),this.a.set(r)},this.toggle=(s,r)=>{const c=new Map(this.a.get());c.has(s)?c.delete(s):c.set(s,r),this.a.set(c)},this.clear=()=>{this.a.set(new Map)},this.a=e}}class te extends L{constructor(e,o){super(o),this.a=e,this.increment=s=>{this.a.set(+this.a.get()+(s??1))}}}class se extends v{constructor(e,o){super(o),this.a=e,this.setPartial=s=>this.a.set(r=>({...r,...typeof s=="function"?s(this.a.get()):s})),this.update=s=>{const r=this.a.get(),c=this.updateValue(r,s);c!==r&&this.a.set(c)},this.setDeepPartial=(s,r,c,f=$.keyPathSeparator||".")=>{if(!f)return;if(s.includes(f)){let A=s.split(f);const y=A[A.length-1];A=A.slice(0,-1);const S={...this.a.get()};let h=S,g=c;for(const p of A){g=g==null?void 0:g[Array.isArray(g)?"0":p];const T=h[ne(h,p)]??(Array.isArray(g)?[]:{});if(T==null||typeof T!="object"){if(c==null)throw"Incorrect path for setDeepPartial";const b=typeof r=="function"?r(void 0):r;this.a.get()[s]!==b&&this.setPartial({[s]:b});return}h=h[ne(h,p)]=Array.isArray(T)?[...T]:{...T}}const B=h[y];h[y]=typeof r=="function"?r(h[y]):r,B!==h[y]&&this.a.set(S);return}const d=this.a.get()[s],m=typeof r=="function"?r(d):r;m!==d&&this.setPartial({[s]:m})}}}const ne=(t,e)=>Array.isArray(t)?`${+e}`:e;class re extends L{constructor(e,o){super(o),this.a=e,this.add=s=>{this.a.get().has(s)||this.a.set(new Set(this.a.get()).add(s))},this.delete=s=>{if(!this.a.get().has(s))return;const r=new Set(this.a.get());r.delete(s),this.a.set(r)},this.toggle=s=>{const r=new Set(this.a.get());r.has(s)?r.delete(s):r.add(s),this.a.set(r)},this.clear=()=>{this.a.set(new Set)},this.a=e}}const be=(t,e,o)=>{const s=typeof o=="object"&&o!=null&&"do"in o?o.do((c,f)=>e.set(c,f),()=>e.get(),e,(c,f,d)=>e.setDeferred(c,f,d)):null,r={};return s&&Object.keys(s).forEach(c=>Object.defineProperty(r,c,{get:()=>s[c]})),typeof t=="number"?new te(e,s):typeof t=="boolean"?new O(e,s):Array.isArray(t)?new k(e,s):t instanceof Set?new re(e,s):t instanceof Map?new ee(e,s):t instanceof Object?new se(e,s):new L(s)};class oe{constructor(e,o){e=typeof e=="function"?e():e;const s=n=>A=n,r=()=>A,c=new Set,f=n=>n(h());let d=!0,m=!1,A=e,y,S=()=>{},h=()=>r(),g=null,B=()=>{const n=be(e,p,o);return B=()=>n,n};const p=new Proxy(this,{get:(n,u)=>u==="do"?B():n[u],set:De}),T=()=>{d=!0,m!==!0&&S(h()),m=!1;try{F.postMessage({key:a,value:r()})}catch{}},b=(n,u)=>{const D=typeof n=="function"?n(h()):n;D!==h()&&(s(D),m=u,d&&(d=!1,c.forEach(f),queueMicrotask(T)))};this.set=(n,u)=>b(n,u),this.get=()=>h(),this.initialValue=e,this.isInitialValue=()=>e===r(),this.subscribe=n=>(c.add(n),()=>{c.delete(n)}),this.reset=()=>{b(e,!0)};const Me=(n,u)=>{b(n,u),y=void 0};if(this.setDeferred=(n,u=500,D,x=!0)=>{x&&y===void 0&&b(n,D),clearTimeout(y),y=setTimeout(Me,u,n,D)},o==null)return p;let w=null,z=null,H=!0,J=!0,Q=!1,M=0,V=0,ue=-1,_=e instanceof Set||e instanceof Map?n=>new Set(n):n=>n,E=e instanceof Set?n=>{if(n instanceof Set)return Array.from(n);throw console.error(n),"The value is not Set instance"}:e instanceof Map?n=>{if(n instanceof Map){const u=[];return n.forEach((D,x)=>u.push([x,D])),u}throw console.error(n),"The value is not Set instance"}:n=>n;if(typeof o=="string")w=o;else if("storeKey"in o)H=o.warnOnDuplicateStoreKey??H,J=o.listenStorageChanges??J,w=o.storeKey,_=o.unzipValue??_,E=o.zipValue??E,Q=o.unchangable??Q,M=o.securifyKeyLevel??$.securifyKeyLevel??M,V=o.securifyValueLevel??$.securifyValueLevel??V,z=o.exp??z;else return p;const K=M?I(w,M):w,a=`${V?N:C}${K}`;if(M){const n=`${C}${w}`;n in i&&(i[a]=i[n],delete i[n])}else{const n=`${C}${I(w,M)}`;n in i&&(i[a]=i[n],delete i[n])}const X=z===null||!(z(p,a in i)instanceof Date)?n=>I([E(n)],0):n=>(g??(g={}),g.exp=z(p,a in i).getTime(),g.exp-Date.now()<24*60*60*1e3&&(clearTimeout(ue),clearTimeout(ie[a]),ue=setTimeout(()=>this.reset(),g.exp-Date.now())),g.exp=Math.trunc(g.exp/1e3),I([E(n),g],0));if(V){const n=`${C}${K}`,u=E,D=_;if(E=x=>{try{return I([u(x)],V)}catch{return delete i[a],""}},n in i){const x=`${C}${I(w,M)}`;try{i[x]=X(_(q(i[n],0)[0])),delete i[n]}catch{}}_=x=>{try{return D(q(x,V)[0])}catch{return delete i[a],""}}}else delete i[`${N}${K}`];const le=n=>{const u=q(n,0);return g=u[1],_(u[0])};let fe=!0;if(W[a]=p,i[`atom/${w}`]&&(i[a]||(i[a]=`[${i[`atom/${w}`]}]`),delete i[`atom/${w}`]),h=()=>{if(h=r,fe){fe=!1;try{s(a in i?le(i[a]):e)}catch{console.warn("Invalid json value",i[a])}}return r()},S=n=>{if(n===e){this.reset();return}i[a]=X(n)},this.reset=()=>{delete i[a],b(e,!0)},H&&P[a]!==void 0&&console.warn("Duplicate Atom key",w),J)if(Q){let n=!1,u;R[a]=this,P[a]=()=>{clearTimeout(u),u=setTimeout(()=>n=!1,10),!n&&(n=!0,i[a]=X(r()))}}else P[a]=n=>{if(n.newValue===null){this.reset();return}try{b(le(n.newValue))}catch{console.warn("Invalid json value",n.newValue)}};return p}}let F;try{F=new BroadcastChannel("updateHere"),F.addEventListener("message",t=>{var e;(e=R[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const i=localStorage,P={},R={},De=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=P[t.key])==null||e.call(P,t)});const xe=i.setItem.bind(i),$e=i.removeItem.bind(i);i.setItem=(t,e)=>{R[t]===void 0&&xe.call(i,t,e)},i.removeItem=t=>{R[t]===void 0&&$e.call(i,t)};const Te=/"exp":\s*(\d+)/,C="atom\\",N="atom`s\\",W={},ie={},ce=/[a-z]/gi,U=5,G={};for(let t=54;t<80;t++){if(t===68||t===72||t===55)continue;const e=String.fromCharCode(t+43).toUpperCase(),o=String.fromCharCode(t+43).toLowerCase();G[e]=o,G[o]=e}const ae=t=>G[t]??t,I=(()=>{const t=[e=>JSON.stringify(e),e=>btoa(encodeURI(t[0](e))),e=>{const o=t[1](e);return`${o.slice(0,U)}${o.slice(U).replace(ce,ae)}`},e=>btoa(t[2](e))];return(e,o)=>{try{return t[o](e)}catch(s){if(o===0)throw s;return t[0](e)}}})(),q=(()=>{const t=[e=>JSON.parse(e),e=>t[0](decodeURI(atob(e))),e=>t[1](`${e.slice(0,U)}${e.slice(U).replace(ce,ae)}`),e=>t[2](atob(e))];return(e,o)=>{try{return t[o](e)}catch(s){if(o===0)throw s;return t[0](e)}}})();setTimeout(()=>{Object.keys(i).forEach(t=>{var s;if(typeof i[t]!="string"||!t.startsWith(C)&&!t.startsWith(N))return;const e=+((s=i[t].match(Te))==null?void 0:s[1]);if(!e||e*1e3-Date.now()>24*60*60*1e3)return;const o=q(i[t],0);!Array.isArray(o)||o[1]==null||!("exp"in o[1])||o[1].exp!==e||(ie[t]=setTimeout(()=>{W[t]?W[t].reset():delete i[t]},e*1e3-Date.now()))})},1e3),l.Atom=oe,l.AtomArrayDoActions=k,l.AtomBooleanDoActions=O,l.AtomMapDoActions=ee,l.AtomNumberDoActions=te,l.AtomObjectDoActions=se,l.AtomSetDoActions=re,l.atom=pe,l.configureAtomaric=de,l.configuredOptions=$,l.useAtom=ye,l.useAtomDo=me,l.useAtomGet=Ae,l.useAtomSet=Z,l.useAtomSetDeferred=ge,l.useAtomValue=Y,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -38,7 +38,7 @@ describe('Array', () => {
|
|
|
38
38
|
|
|
39
39
|
expect(testAtom.get()).toEqual([1, 2, 5, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8]);
|
|
40
40
|
|
|
41
|
-
testAtom.do.
|
|
41
|
+
testAtom.do.removeFirst(5);
|
|
42
42
|
await wait();
|
|
43
43
|
|
|
44
44
|
expect(testAtom.get()).toEqual([1, 2, 2, 5, 5, 5, 5, 1, null, '', '#', 0, 8]);
|
package/src/do.classes/Array.ts
CHANGED
|
@@ -2,57 +2,63 @@ import { Atom } from '../../types';
|
|
|
2
2
|
import { AtomUpdateDoAction } from './_Update';
|
|
3
3
|
|
|
4
4
|
export class AtomArrayDoActions<Value> extends AtomUpdateDoAction {
|
|
5
|
-
constructor(private
|
|
5
|
+
constructor(private a: Atom<Value[]>, actions: Record<string, Function> | nil) {
|
|
6
6
|
super(actions);
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
/** like the Array.prototype.push() method */
|
|
10
10
|
push = (...values: Value[]) => {
|
|
11
|
-
|
|
11
|
+
if (values.length === 0) return;
|
|
12
|
+
this.a.set(this.a.get().concat(values));
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
/** like the Array.prototype.unshift() method */
|
|
15
16
|
unshift = (...values: Value[]) => {
|
|
16
|
-
|
|
17
|
+
if (values.length === 0) return;
|
|
18
|
+
this.a.set(values.concat(this.a.get()));
|
|
17
19
|
};
|
|
18
20
|
|
|
19
21
|
/** transform current taken value */
|
|
20
22
|
update = (updater: (value: Value[]) => void) => {
|
|
21
|
-
const prev = this.
|
|
23
|
+
const prev = this.a.get();
|
|
22
24
|
const newValue = this.updateValue(prev, updater);
|
|
23
25
|
if (newValue === prev) return;
|
|
24
|
-
this.
|
|
26
|
+
this.a.set(newValue);
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
/** like the Array.prototype.filter() method, but callback is optional - (it) => !!it */
|
|
28
30
|
filter = (filter?: (value: Value, index: number, Array: Value[]) => any) => {
|
|
29
|
-
this.
|
|
31
|
+
const filtered = this.a.get().filter(filter ?? itIt);
|
|
32
|
+
if (filtered.length === this.a.get().length) return;
|
|
33
|
+
this.a.set(filtered);
|
|
30
34
|
};
|
|
31
35
|
|
|
32
36
|
/** will add value if not exists */
|
|
33
37
|
add = (value: Value) => {
|
|
34
|
-
if (this.
|
|
35
|
-
this.
|
|
38
|
+
if (this.a.get().includes(value)) return;
|
|
39
|
+
this.a.set(this.a.get().concat([value]));
|
|
36
40
|
};
|
|
37
41
|
|
|
38
42
|
/** will delete value from array */
|
|
39
|
-
|
|
40
|
-
const index = this.
|
|
43
|
+
removeFirst = (value: Value) => {
|
|
44
|
+
const index = this.a.get().indexOf(value);
|
|
41
45
|
if (index < 0) return;
|
|
42
|
-
const newArray = this.
|
|
46
|
+
const newArray = this.a.get().slice(0);
|
|
43
47
|
newArray.splice(index, 1);
|
|
44
|
-
this.
|
|
48
|
+
this.a.set(newArray);
|
|
45
49
|
};
|
|
46
50
|
|
|
47
51
|
/** will add value if it doesn't exist, otherwise delete */
|
|
48
52
|
toggle = (value: Value, isAddToStart?: boolean) => {
|
|
49
|
-
const newArray = this.
|
|
53
|
+
const newArray = this.a.get().slice();
|
|
50
54
|
const index = newArray.indexOf(value);
|
|
55
|
+
|
|
51
56
|
if (index < 0) {
|
|
52
57
|
if (isAddToStart) newArray.unshift(value);
|
|
53
58
|
else newArray.push(value);
|
|
54
59
|
} else newArray.splice(index, 1);
|
|
55
|
-
|
|
60
|
+
|
|
61
|
+
this.a.set(newArray);
|
|
56
62
|
};
|
|
57
63
|
}
|
|
58
64
|
|
|
@@ -2,12 +2,12 @@ import { Atom } from '../../types';
|
|
|
2
2
|
import { AtomDoActionsBasic } from './_Basic';
|
|
3
3
|
|
|
4
4
|
export class AtomBooleanDoActions extends AtomDoActionsBasic {
|
|
5
|
-
constructor(private
|
|
5
|
+
constructor(private a: Atom<boolean>, actions: Record<string, Function> | nil) {
|
|
6
6
|
super(actions);
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
/** toggle current value between true/false */
|
|
10
10
|
toggle = () => {
|
|
11
|
-
this.
|
|
11
|
+
this.a.set(!this.a.get());
|
|
12
12
|
};
|
|
13
13
|
}
|
package/src/do.classes/Map.ts
CHANGED
|
@@ -6,37 +6,43 @@ export class AtomMapDoActions<
|
|
|
6
6
|
Key extends MapValue extends Map<infer K, any> ? K : never,
|
|
7
7
|
Value extends MapValue extends Map<any, infer V> ? V : never,
|
|
8
8
|
> extends AtomDoActionsBasic {
|
|
9
|
-
constructor(private
|
|
9
|
+
constructor(private a: Atom<MapValue>, actions: Record<string, Function> | nil) {
|
|
10
10
|
super(actions);
|
|
11
|
-
this.
|
|
11
|
+
this.a = a;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/** like the Map.prototype.set() method */
|
|
15
15
|
setValue = (key: Key, value: Value) => {
|
|
16
|
-
|
|
16
|
+
if (this.a.get().get(key) === value) return;
|
|
17
|
+
|
|
18
|
+
const newMap = new Map(this.a.get());
|
|
17
19
|
newMap.set(key, value);
|
|
18
|
-
|
|
20
|
+
|
|
21
|
+
this.a.set(newMap as never);
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
/** like the Map.prototype.delete() method */
|
|
22
25
|
delete = (key: Key) => {
|
|
23
|
-
|
|
26
|
+
if (!this.a.get().has(key)) return;
|
|
27
|
+
|
|
28
|
+
const newMap = new Map(this.a.get());
|
|
24
29
|
newMap.delete(key);
|
|
25
|
-
|
|
30
|
+
|
|
31
|
+
this.a.set(newMap as never);
|
|
26
32
|
};
|
|
27
33
|
|
|
28
34
|
/** will add value if it doesn't exist, otherwise delete */
|
|
29
35
|
toggle = (key: Key, value: Value) => {
|
|
30
|
-
const newMap = new Map(this.
|
|
36
|
+
const newMap = new Map(this.a.get());
|
|
31
37
|
|
|
32
38
|
if (newMap.has(key)) newMap.delete(key);
|
|
33
39
|
else newMap.set(key, value);
|
|
34
40
|
|
|
35
|
-
this.
|
|
41
|
+
this.a.set(newMap as never);
|
|
36
42
|
};
|
|
37
43
|
|
|
38
44
|
/** like the Map.prototype.clear() method */
|
|
39
45
|
clear = () => {
|
|
40
|
-
this.
|
|
46
|
+
this.a.set(new Map() as never);
|
|
41
47
|
};
|
|
42
48
|
}
|
package/src/do.classes/Number.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Atom } from '../../types';
|
|
|
2
2
|
import { AtomDoActionsBasic } from './_Basic';
|
|
3
3
|
|
|
4
4
|
export class AtomNumberDoActions extends AtomDoActionsBasic {
|
|
5
|
-
constructor(private
|
|
5
|
+
constructor(private a: Atom<number>, actions: Record<string, Function> | nil) {
|
|
6
6
|
super(actions);
|
|
7
7
|
}
|
|
8
8
|
|
|
@@ -10,6 +10,6 @@ export class AtomNumberDoActions extends AtomDoActionsBasic {
|
|
|
10
10
|
* **default: 1**
|
|
11
11
|
*/
|
|
12
12
|
increment = (delta?: number) => {
|
|
13
|
-
this.
|
|
13
|
+
this.a.set(+this.a.get() + (delta ?? 1));
|
|
14
14
|
};
|
|
15
15
|
}
|
package/src/do.classes/Object.ts
CHANGED
|
@@ -3,23 +3,23 @@ import { configuredOptions } from '../lib';
|
|
|
3
3
|
import { AtomUpdateDoAction } from './_Update';
|
|
4
4
|
|
|
5
5
|
export class AtomObjectDoActions<Value extends object> extends AtomUpdateDoAction {
|
|
6
|
-
constructor(private
|
|
6
|
+
constructor(private a: Atom<Value>, actions: Record<string, Function> | nil) {
|
|
7
7
|
super(actions);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
/** pass partial object to update some field values */
|
|
11
11
|
setPartial = (value: Partial<Value> | ((value: Value) => Partial<Value>)) =>
|
|
12
|
-
this.
|
|
12
|
+
this.a.set(prev => ({
|
|
13
13
|
...prev,
|
|
14
|
-
...(typeof value === 'function' ? value(this.
|
|
14
|
+
...(typeof value === 'function' ? value(this.a.get()) : value),
|
|
15
15
|
}));
|
|
16
16
|
|
|
17
17
|
/** transform current taken value */
|
|
18
18
|
update = (updater: (value: Value) => void) => {
|
|
19
|
-
const prev = this.
|
|
19
|
+
const prev = this.a.get();
|
|
20
20
|
const newValue = this.updateValue(prev, updater);
|
|
21
21
|
if (newValue === prev) return;
|
|
22
|
-
this.
|
|
22
|
+
this.a.set(newValue);
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
/** pass partial value to update some deep values by flat path */
|
|
@@ -35,7 +35,7 @@ export class AtomObjectDoActions<Value extends object> extends AtomUpdateDoActio
|
|
|
35
35
|
let keys = path.split(separator);
|
|
36
36
|
const lastKey = keys[keys.length - 1];
|
|
37
37
|
keys = keys.slice(0, -1);
|
|
38
|
-
const newObject = { ...this.
|
|
38
|
+
const newObject = { ...this.a.get() };
|
|
39
39
|
let lastObject = newObject as Record<string, unknown>;
|
|
40
40
|
let lastDonorObject = donor as Record<string, unknown> | nil;
|
|
41
41
|
|
|
@@ -48,7 +48,7 @@ export class AtomObjectDoActions<Value extends object> extends AtomUpdateDoActio
|
|
|
48
48
|
|
|
49
49
|
const newValue = typeof value === 'function' ? (value as (val: undefined) => Value)(undefined) : value;
|
|
50
50
|
|
|
51
|
-
if (this.
|
|
51
|
+
if (this.a.get()[path as never] !== newValue) this.setPartial({ [path]: newValue } as never);
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -61,12 +61,12 @@ export class AtomObjectDoActions<Value extends object> extends AtomUpdateDoActio
|
|
|
61
61
|
lastObject[lastKey] =
|
|
62
62
|
typeof value === 'function' ? (value as (val: unknown) => Value)(lastObject[lastKey]) : value;
|
|
63
63
|
|
|
64
|
-
if (prev !== lastObject[lastKey]) this.
|
|
64
|
+
if (prev !== lastObject[lastKey]) this.a.set(newObject);
|
|
65
65
|
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
const prevValue = this.
|
|
69
|
+
const prevValue = this.a.get()[path as never];
|
|
70
70
|
const newValue = typeof value === 'function' ? (value as (val: Value) => Value)(prevValue) : value;
|
|
71
71
|
if (newValue !== prevValue) this.setPartial({ [path]: newValue } as never);
|
|
72
72
|
};
|
package/src/do.classes/Set.ts
CHANGED
|
@@ -2,35 +2,39 @@ import { Atom } from '../../types';
|
|
|
2
2
|
import { AtomDoActionsBasic } from './_Basic';
|
|
3
3
|
|
|
4
4
|
export class AtomSetDoActions<Value> extends AtomDoActionsBasic {
|
|
5
|
-
constructor(private
|
|
5
|
+
constructor(private a: Atom<Set<Value>>, actions: Record<string, Function> | nil) {
|
|
6
6
|
super(actions);
|
|
7
|
-
this.
|
|
7
|
+
this.a = a;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
/** like the Set.prototype.add() method */
|
|
11
11
|
add = (value: Value) => {
|
|
12
|
-
|
|
12
|
+
if (this.a.get().has(value)) return;
|
|
13
|
+
|
|
14
|
+
this.a.set(new Set(this.a.get()).add(value));
|
|
13
15
|
};
|
|
14
16
|
|
|
15
17
|
/** like the Set.prototype.delete() method */
|
|
16
18
|
delete = (value: Value) => {
|
|
17
|
-
|
|
19
|
+
if (!this.a.get().has(value)) return;
|
|
20
|
+
|
|
21
|
+
const newSet = new Set(this.a.get());
|
|
18
22
|
newSet.delete(value);
|
|
19
|
-
this.
|
|
23
|
+
this.a.set(newSet);
|
|
20
24
|
};
|
|
21
25
|
|
|
22
26
|
/** will add value if it doesn't exist, otherwise delete */
|
|
23
27
|
toggle = (value: Value) => {
|
|
24
|
-
const newSet = new Set(this.
|
|
28
|
+
const newSet = new Set(this.a.get());
|
|
25
29
|
|
|
26
30
|
if (newSet.has(value)) newSet.delete(value);
|
|
27
31
|
else newSet.add(value);
|
|
28
32
|
|
|
29
|
-
this.
|
|
33
|
+
this.a.set(newSet);
|
|
30
34
|
};
|
|
31
35
|
|
|
32
36
|
/** like the Set.prototype.clear() method */
|
|
33
37
|
clear = () => {
|
|
34
|
-
this.
|
|
38
|
+
this.a.set(new Set());
|
|
35
39
|
};
|
|
36
40
|
}
|
|
@@ -20,7 +20,7 @@ export class AtomUpdateDoAction extends AtomDoActionsBasic {
|
|
|
20
20
|
const nextObject = currentObject[key as never] as object;
|
|
21
21
|
|
|
22
22
|
currentObject = currentObject[key as never] = (
|
|
23
|
-
Array.isArray(nextObject) ? nextObject.slice(
|
|
23
|
+
Array.isArray(nextObject) ? nextObject.slice() : { ...nextObject }
|
|
24
24
|
) as never;
|
|
25
25
|
}
|
|
26
26
|
|