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