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