atomaric 0.0.72 → 0.0.74
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 +75 -71
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/src/do.classes/Map.ts +11 -1
package/build/atomaric.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
let k = () => {
|
|
2
2
|
throw "call configureAtomaric() before all!";
|
|
3
3
|
};
|
|
4
|
-
const
|
|
5
|
-
|
|
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)],
|
|
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)
|
|
@@ -13,13 +13,13 @@ class _ {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
const ce = (t, e) => {
|
|
16
|
-
const i = (s, n,
|
|
16
|
+
const i = (s, n, c) => new Proxy(s, {
|
|
17
17
|
get: (l, h, p) => {
|
|
18
|
-
const g =
|
|
18
|
+
const g = c === 0 && e.get != null ? e.get(l, h, p) : l[h];
|
|
19
19
|
return typeof g == "object" && g !== null ? i(
|
|
20
20
|
Array.isArray(g) ? g.slice(0) : { ...g },
|
|
21
21
|
n.concat(Array.isArray(l) ? +h : h),
|
|
22
|
-
|
|
22
|
+
c + 1
|
|
23
23
|
) : g;
|
|
24
24
|
},
|
|
25
25
|
set: (l, h, p) => (e.onSet(l, n, h, p, l[h]) && (l[h] = p), !0)
|
|
@@ -31,7 +31,7 @@ class O extends _ {
|
|
|
31
31
|
super(...arguments), this.updateValue = (e, i) => {
|
|
32
32
|
const s = Array.isArray(e) ? e.slice(0) : { ...e };
|
|
33
33
|
let n = !1;
|
|
34
|
-
const
|
|
34
|
+
const c = ce(e, {
|
|
35
35
|
onSet: (l, h, p, g, w) => {
|
|
36
36
|
if (g === w) return !0;
|
|
37
37
|
let m = s;
|
|
@@ -43,7 +43,7 @@ class O extends _ {
|
|
|
43
43
|
return m[p] = g, !0;
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
|
-
return i(
|
|
46
|
+
return i(c), n ? s : e;
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -54,8 +54,8 @@ class oe extends O {
|
|
|
54
54
|
}, this.unshift = (...s) => {
|
|
55
55
|
s.length !== 0 && this.a.set(s.concat(this.a.get()));
|
|
56
56
|
}, this.update = (s) => {
|
|
57
|
-
const n = this.a.get(),
|
|
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
60
|
const n = this.a.get().filter(s ?? ae);
|
|
61
61
|
n.length !== this.a.get().length && this.a.set(n);
|
|
@@ -64,11 +64,11 @@ class oe extends O {
|
|
|
64
64
|
}, this.removeFirst = (s) => {
|
|
65
65
|
const n = this.a.get().indexOf(s);
|
|
66
66
|
if (n < 0) return;
|
|
67
|
-
const
|
|
68
|
-
|
|
67
|
+
const c = this.a.get().slice(0);
|
|
68
|
+
c.splice(n, 1), this.a.set(c);
|
|
69
69
|
}, this.toggle = (s, n) => {
|
|
70
|
-
const
|
|
71
|
-
l < 0 ? 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);
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -84,15 +84,19 @@ class le extends _ {
|
|
|
84
84
|
constructor(e, i) {
|
|
85
85
|
super(i), this.a = e, this.setValue = (s, n) => {
|
|
86
86
|
if (this.a.get().get(s) === n) return;
|
|
87
|
-
const
|
|
88
|
-
|
|
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);
|
|
89
93
|
}, this.delete = (s) => {
|
|
90
94
|
if (!this.a.get().has(s)) return;
|
|
91
95
|
const n = new Map(this.a.get());
|
|
92
96
|
n.delete(s), this.a.set(n);
|
|
93
97
|
}, this.toggle = (s, n) => {
|
|
94
|
-
const
|
|
95
|
-
|
|
98
|
+
const c = new Map(this.a.get());
|
|
99
|
+
c.has(s) ? c.delete(s) : c.set(s, n), this.a.set(c);
|
|
96
100
|
}, this.clear = () => {
|
|
97
101
|
this.a.set(/* @__PURE__ */ new Map());
|
|
98
102
|
}, this.a = e;
|
|
@@ -111,21 +115,21 @@ class he extends O {
|
|
|
111
115
|
...n,
|
|
112
116
|
...typeof s == "function" ? s(this.a.get()) : s
|
|
113
117
|
})), this.update = (s) => {
|
|
114
|
-
const n = this.a.get(),
|
|
115
|
-
|
|
116
|
-
}, this.setDeepPartial = (s, n,
|
|
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 || ".") => {
|
|
117
121
|
if (!l) return;
|
|
118
122
|
if (s.includes(l)) {
|
|
119
123
|
let g = s.split(l);
|
|
120
124
|
const w = g[g.length - 1];
|
|
121
125
|
g = g.slice(0, -1);
|
|
122
126
|
const m = { ...this.a.get() };
|
|
123
|
-
let f = m, d =
|
|
127
|
+
let f = m, d = c;
|
|
124
128
|
for (const y of g) {
|
|
125
129
|
d = d == null ? void 0 : d[Array.isArray(d) ? "0" : y];
|
|
126
130
|
const D = f[v(f, y)] ?? (Array.isArray(d) ? [] : {});
|
|
127
131
|
if (D == null || typeof D != "object") {
|
|
128
|
-
if (
|
|
132
|
+
if (c == null) throw "Incorrect path for setDeepPartial";
|
|
129
133
|
const S = typeof n == "function" ? n(void 0) : n;
|
|
130
134
|
this.a.get()[s] !== S && this.setPartial({ [s]: S });
|
|
131
135
|
return;
|
|
@@ -160,17 +164,17 @@ class de extends _ {
|
|
|
160
164
|
}
|
|
161
165
|
const ge = (t, e, i) => {
|
|
162
166
|
const s = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
163
|
-
(
|
|
167
|
+
(c, l) => e.set(c, l),
|
|
164
168
|
() => e.get(),
|
|
165
169
|
e,
|
|
166
|
-
(
|
|
170
|
+
(c, l, h) => e.setDeferred(c, l, h)
|
|
167
171
|
) : null, n = {};
|
|
168
|
-
return s && Object.keys(s).forEach((
|
|
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);
|
|
169
173
|
};
|
|
170
174
|
class pe {
|
|
171
175
|
constructor(e, i) {
|
|
172
176
|
e = typeof e == "function" ? e() : e;
|
|
173
|
-
const s = (r) => g = r, n = () => g,
|
|
177
|
+
const s = (r) => g = r, n = () => g, c = /* @__PURE__ */ new Set(), l = (r) => r(f());
|
|
174
178
|
let h = !0, p = !1, g = e, w, m = () => {
|
|
175
179
|
}, f = () => n(), d = null, E = () => {
|
|
176
180
|
const r = ge(e, y, i);
|
|
@@ -187,10 +191,10 @@ class pe {
|
|
|
187
191
|
}
|
|
188
192
|
}, S = (r, u) => {
|
|
189
193
|
const x = typeof r == "function" ? r(f()) : r;
|
|
190
|
-
x !== f() && (s(x), p = u, h && (h = !1,
|
|
194
|
+
x !== f() && (s(x), p = u, h && (h = !1, c.forEach(l), queueMicrotask(D)));
|
|
191
195
|
};
|
|
192
|
-
this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === n(), this.subscribe = (r) => (
|
|
193
|
-
|
|
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);
|
|
194
198
|
}), this.reset = () => {
|
|
195
199
|
S(e, !0);
|
|
196
200
|
};
|
|
@@ -200,7 +204,7 @@ class pe {
|
|
|
200
204
|
if (this.setDeferred = (r, u = 500, x, b = !0) => {
|
|
201
205
|
b && w === void 0 && S(r, x), clearTimeout(w), w = setTimeout(re, u, r, x);
|
|
202
206
|
}, i == null) return y;
|
|
203
|
-
let A = null, j = null, q = !0, F = !0,
|
|
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) => {
|
|
204
208
|
if (r instanceof Set) return Array.from(r);
|
|
205
209
|
throw console.error(r), "The value is not Set instance";
|
|
206
210
|
} : e instanceof Map ? (r) => {
|
|
@@ -213,52 +217,52 @@ class pe {
|
|
|
213
217
|
if (typeof i == "string")
|
|
214
218
|
A = i;
|
|
215
219
|
else if ("storeKey" in i)
|
|
216
|
-
q = i.warnOnDuplicateStoreKey ?? q, F = i.listenStorageChanges ?? F, A = i.storeKey,
|
|
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;
|
|
217
221
|
else return y;
|
|
218
|
-
const
|
|
222
|
+
const B = $ ? C(A, $) : A, a = `${T ? J : P}${B}`;
|
|
219
223
|
if ($) {
|
|
220
224
|
const r = `${P}${A}`;
|
|
221
|
-
r in
|
|
225
|
+
r in o && (o[a] = o[r], delete o[r]);
|
|
222
226
|
} else {
|
|
223
|
-
const r = `${P}${
|
|
224
|
-
r in
|
|
227
|
+
const r = `${P}${C(A, $)}`;
|
|
228
|
+
r in o && (o[a] = o[r], delete o[r]);
|
|
225
229
|
}
|
|
226
|
-
const W = j === null || !(j(y, a in
|
|
227
|
-
if (
|
|
228
|
-
const r = `${P}${
|
|
229
|
-
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) => {
|
|
230
234
|
try {
|
|
231
|
-
return
|
|
235
|
+
return C([u(b)], T);
|
|
232
236
|
} catch {
|
|
233
|
-
return delete
|
|
237
|
+
return delete o[a], "";
|
|
234
238
|
}
|
|
235
|
-
}, r in
|
|
236
|
-
const b = `${P}${
|
|
239
|
+
}, r in o) {
|
|
240
|
+
const b = `${P}${C(A, $)}`;
|
|
237
241
|
try {
|
|
238
|
-
|
|
242
|
+
o[b] = W(I(z(o[r], 0)[0])), delete o[r];
|
|
239
243
|
} catch {
|
|
240
244
|
}
|
|
241
245
|
}
|
|
242
|
-
|
|
246
|
+
I = (b) => {
|
|
243
247
|
try {
|
|
244
|
-
return x(z(b,
|
|
248
|
+
return x(z(b, T)[0]);
|
|
245
249
|
} catch {
|
|
246
|
-
return delete
|
|
250
|
+
return delete o[a], "";
|
|
247
251
|
}
|
|
248
252
|
};
|
|
249
|
-
} else delete
|
|
253
|
+
} else delete o[`${J}${B}`];
|
|
250
254
|
const X = (r) => {
|
|
251
255
|
const u = z(r, 0);
|
|
252
|
-
return d = u[1],
|
|
256
|
+
return d = u[1], I(u[0]);
|
|
253
257
|
};
|
|
254
258
|
let Y = !0;
|
|
255
|
-
if (G[a] = y,
|
|
259
|
+
if (G[a] = y, o[`atom/${A}`] && (o[a] || (o[a] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => {
|
|
256
260
|
if (f = n, Y) {
|
|
257
261
|
Y = !1;
|
|
258
262
|
try {
|
|
259
|
-
s(a in
|
|
263
|
+
s(a in o ? X(o[a]) : e);
|
|
260
264
|
} catch {
|
|
261
|
-
console.warn("Invalid json value",
|
|
265
|
+
console.warn("Invalid json value", o[a]);
|
|
262
266
|
}
|
|
263
267
|
}
|
|
264
268
|
return n();
|
|
@@ -267,14 +271,14 @@ class pe {
|
|
|
267
271
|
this.reset();
|
|
268
272
|
return;
|
|
269
273
|
}
|
|
270
|
-
|
|
274
|
+
o[a] = W(r);
|
|
271
275
|
}, this.reset = () => {
|
|
272
|
-
delete
|
|
276
|
+
delete o[a], S(e, !0);
|
|
273
277
|
}, q && V[a] !== void 0 && console.warn("Duplicate Atom key", A), F)
|
|
274
|
-
if (
|
|
278
|
+
if (N) {
|
|
275
279
|
let r = !1, u;
|
|
276
280
|
U[a] = this, V[a] = () => {
|
|
277
|
-
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()));
|
|
278
282
|
};
|
|
279
283
|
} else
|
|
280
284
|
V[a] = (r) => {
|
|
@@ -299,19 +303,19 @@ try {
|
|
|
299
303
|
});
|
|
300
304
|
} catch {
|
|
301
305
|
}
|
|
302
|
-
const
|
|
306
|
+
const o = localStorage, V = {}, U = {}, we = (t, e) => {
|
|
303
307
|
throw `${e} is readonly property`;
|
|
304
308
|
};
|
|
305
309
|
window.addEventListener("storage", (t) => {
|
|
306
310
|
var e;
|
|
307
311
|
t.key === null || t.newValue === t.oldValue || (e = V[t.key]) == null || e.call(V, t);
|
|
308
312
|
});
|
|
309
|
-
const ye =
|
|
310
|
-
|
|
311
|
-
U[t] === void 0 && ye.call(
|
|
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);
|
|
312
316
|
};
|
|
313
|
-
|
|
314
|
-
U[t] === void 0 && Ae.call(
|
|
317
|
+
o.removeItem = (t) => {
|
|
318
|
+
U[t] === void 0 && Ae.call(o, t);
|
|
315
319
|
};
|
|
316
320
|
const me = /"exp":\s*(\d+)/, P = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {};
|
|
317
321
|
for (let t = 54; t < 80; t++) {
|
|
@@ -319,7 +323,7 @@ for (let t = 54; t < 80; t++) {
|
|
|
319
323
|
const e = String.fromCharCode(t + 43).toUpperCase(), i = String.fromCharCode(t + 43).toLowerCase();
|
|
320
324
|
Q[e] = i, Q[i] = e;
|
|
321
325
|
}
|
|
322
|
-
const se = (t) => Q[t] ?? t,
|
|
326
|
+
const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
|
|
323
327
|
const t = [
|
|
324
328
|
(e) => JSON.stringify(e),
|
|
325
329
|
(e) => btoa(encodeURI(t[0](e))),
|
|
@@ -356,14 +360,14 @@ const se = (t) => Q[t] ?? t, I = /* @__PURE__ */ (() => {
|
|
|
356
360
|
};
|
|
357
361
|
})();
|
|
358
362
|
setTimeout(() => {
|
|
359
|
-
Object.keys(
|
|
363
|
+
Object.keys(o).forEach((t) => {
|
|
360
364
|
var s;
|
|
361
|
-
if (typeof
|
|
362
|
-
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]);
|
|
363
367
|
if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
|
|
364
|
-
const i = z(
|
|
368
|
+
const i = z(o[t], 0);
|
|
365
369
|
!Array.isArray(i) || i[1] == null || !("exp" in i[1]) || i[1].exp !== e || (ee[t] = setTimeout(() => {
|
|
366
|
-
G[t] ? G[t].reset() : delete
|
|
370
|
+
G[t] ? G[t].reset() : delete o[t];
|
|
367
371
|
}, e * 1e3 - Date.now()));
|
|
368
372
|
});
|
|
369
373
|
}, 1e3);
|
|
@@ -375,9 +379,9 @@ export {
|
|
|
375
379
|
fe as AtomNumberDoActions,
|
|
376
380
|
he as AtomObjectDoActions,
|
|
377
381
|
de as AtomSetDoActions,
|
|
378
|
-
|
|
382
|
+
Me as atom,
|
|
379
383
|
Se as configureAtomaric,
|
|
380
|
-
|
|
384
|
+
M as configuredOptions,
|
|
381
385
|
$e as useAtom,
|
|
382
386
|
De as useAtomDo,
|
|
383
387
|
be as useAtomGet,
|
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
package/src/do.classes/Map.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class AtomMapDoActions<
|
|
|
11
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
17
|
|
|
@@ -21,6 +21,16 @@ export class AtomMapDoActions<
|
|
|
21
21
|
this.a.set(newMap as never);
|
|
22
22
|
};
|
|
23
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);
|
|
32
|
+
};
|
|
33
|
+
|
|
24
34
|
/** like the Map.prototype.delete() method */
|
|
25
35
|
delete = (key: Key) => {
|
|
26
36
|
if (!this.a.get().has(key)) return;
|