atomaric 0.0.70 → 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 +84 -98
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/src/do.classes/Map.test.ts +28 -22
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), oe = (t) => t.set, xe = (t) => t.setDeferred, be = (t) => t.get, De = (t) => t.do, $e = (t) => [ne(t), oe(t)],
|
|
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
7
|
class _ {
|
|
8
8
|
constructor(e) {
|
|
9
9
|
if (e)
|
|
@@ -32,15 +32,15 @@ class O extends _ {
|
|
|
32
32
|
const s = Array.isArray(e) ? e.slice(0) : { ...e };
|
|
33
33
|
let o = !1;
|
|
34
34
|
const i = ce(e, {
|
|
35
|
-
onSet: (l, h, p, d,
|
|
36
|
-
if (d ===
|
|
37
|
-
let
|
|
35
|
+
onSet: (l, h, p, d, g) => {
|
|
36
|
+
if (d === g) return !0;
|
|
37
|
+
let A = s;
|
|
38
38
|
o = !0;
|
|
39
39
|
for (const f of h) {
|
|
40
|
-
const m =
|
|
41
|
-
|
|
40
|
+
const m = A[f];
|
|
41
|
+
A = A[f] = Array.isArray(m) ? m.slice(0) : { ...m };
|
|
42
42
|
}
|
|
43
|
-
return
|
|
43
|
+
return A[p] = d, !0;
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
46
|
return n(i), o ? s : e;
|
|
@@ -110,27 +110,27 @@ class he extends O {
|
|
|
110
110
|
})), this.update = (s) => {
|
|
111
111
|
const o = this.atom.get(), i = this.updateValue(o, s);
|
|
112
112
|
i !== o && this.atom.set(i);
|
|
113
|
-
}, this.setDeepPartial = (s, o, i, l =
|
|
113
|
+
}, this.setDeepPartial = (s, o, i, l = T.keyPathSeparator || ".") => {
|
|
114
114
|
if (!l) return;
|
|
115
115
|
if (s.includes(l)) {
|
|
116
116
|
let d = s.split(l);
|
|
117
|
-
const
|
|
117
|
+
const g = d[d.length - 1];
|
|
118
118
|
d = d.slice(0, -1);
|
|
119
|
-
const
|
|
120
|
-
let f =
|
|
119
|
+
const A = { ...this.atom.get() };
|
|
120
|
+
let f = A, m = i;
|
|
121
121
|
for (const w of d) {
|
|
122
122
|
m = m == null ? void 0 : m[Array.isArray(m) ? "0" : w];
|
|
123
123
|
const D = f[Z(f, w)] ?? (Array.isArray(m) ? [] : {});
|
|
124
124
|
if (D == null || typeof D != "object") {
|
|
125
125
|
if (i == null) throw "Incorrect path for setDeepPartial";
|
|
126
|
-
const
|
|
127
|
-
this.atom.get()[s] !==
|
|
126
|
+
const S = typeof o == "function" ? o(void 0) : o;
|
|
127
|
+
this.atom.get()[s] !== S && this.setPartial({ [s]: S });
|
|
128
128
|
return;
|
|
129
129
|
}
|
|
130
130
|
f = f[Z(f, w)] = Array.isArray(D) ? [...D] : { ...D };
|
|
131
131
|
}
|
|
132
|
-
const E = f[
|
|
133
|
-
f[
|
|
132
|
+
const E = f[g];
|
|
133
|
+
f[g] = typeof o == "function" ? o(f[g]) : o, E !== f[g] && this.atom.set(A);
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
136
|
const h = this.atom.get()[s], p = typeof o == "function" ? o(h) : o;
|
|
@@ -167,133 +167,119 @@ class pe {
|
|
|
167
167
|
constructor(e, n) {
|
|
168
168
|
e = typeof e == "function" ? e() : e;
|
|
169
169
|
const s = (r) => d = r, o = () => d, i = /* @__PURE__ */ new Set(), l = (r) => r(f());
|
|
170
|
-
let h = !0, p = !1, d = e,
|
|
170
|
+
let h = !0, p = !1, d = e, g, A = () => {
|
|
171
171
|
}, f = () => o(), m = null, E = () => {
|
|
172
172
|
const r = de(e, w, n);
|
|
173
173
|
return E = () => r, r;
|
|
174
174
|
};
|
|
175
175
|
const w = new Proxy(this, {
|
|
176
|
-
get: (r,
|
|
176
|
+
get: (r, u) => u === "do" ? E() : r[u],
|
|
177
177
|
set: ge
|
|
178
178
|
}), D = () => {
|
|
179
|
-
h = !0, p !== !0 &&
|
|
179
|
+
h = !0, p !== !0 && A(f()), p = !1;
|
|
180
180
|
try {
|
|
181
|
-
v.postMessage({ key:
|
|
181
|
+
v.postMessage({ key: a, value: o() });
|
|
182
182
|
} catch {
|
|
183
183
|
}
|
|
184
|
-
},
|
|
185
|
-
const
|
|
186
|
-
|
|
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)));
|
|
187
187
|
};
|
|
188
|
-
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), () => {
|
|
189
189
|
i.delete(r);
|
|
190
190
|
}), this.reset = () => {
|
|
191
|
-
|
|
191
|
+
S(e, !0);
|
|
192
192
|
};
|
|
193
|
-
const re = (r,
|
|
194
|
-
|
|
193
|
+
const re = (r, u) => {
|
|
194
|
+
S(r, u), g = void 0;
|
|
195
195
|
};
|
|
196
|
-
if (this.setDeferred = (r,
|
|
197
|
-
|
|
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
198
|
}, n == null) return w;
|
|
199
|
-
let
|
|
200
|
-
if (!r || typeof r != "object") return /* @__PURE__ */ new Map();
|
|
201
|
-
const a = /* @__PURE__ */ new Map();
|
|
202
|
-
for (const g in r)
|
|
203
|
-
try {
|
|
204
|
-
a.set(JSON.parse(g), r[g]);
|
|
205
|
-
} catch {
|
|
206
|
-
}
|
|
207
|
-
return a;
|
|
208
|
-
} : (r) => r, j = e instanceof Set ? (r) => {
|
|
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) => {
|
|
209
200
|
if (r instanceof Set) return Array.from(r);
|
|
210
201
|
throw console.error(r), "The value is not Set instance";
|
|
211
202
|
} : e instanceof Map ? (r) => {
|
|
212
203
|
if (r instanceof Map) {
|
|
213
|
-
const
|
|
214
|
-
return r.forEach((
|
|
215
|
-
try {
|
|
216
|
-
a[JSON.stringify(x)] = g;
|
|
217
|
-
} catch {
|
|
218
|
-
}
|
|
219
|
-
}), a;
|
|
204
|
+
const u = [];
|
|
205
|
+
return r.forEach((x, b) => u.push([b, x])), u;
|
|
220
206
|
}
|
|
221
207
|
throw console.error(r), "The value is not Set instance";
|
|
222
208
|
} : (r) => r;
|
|
223
209
|
if (typeof n == "string")
|
|
224
|
-
|
|
210
|
+
y = n;
|
|
225
211
|
else if ("storeKey" in n)
|
|
226
|
-
|
|
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;
|
|
227
213
|
else return w;
|
|
228
|
-
const
|
|
214
|
+
const N = $ ? I(y, $) : y, a = `${L ? H : P}${N}`;
|
|
229
215
|
if ($) {
|
|
230
|
-
const r = `${
|
|
231
|
-
r in c && (c[
|
|
216
|
+
const r = `${P}${y}`;
|
|
217
|
+
r in c && (c[a] = c[r], delete c[r]);
|
|
232
218
|
} else {
|
|
233
|
-
const r = `${
|
|
234
|
-
r in c && (c[
|
|
219
|
+
const r = `${P}${I(y, $)}`;
|
|
220
|
+
r in c && (c[a] = c[r], delete c[r]);
|
|
235
221
|
}
|
|
236
|
-
const
|
|
237
|
-
if (
|
|
238
|
-
const r = `${
|
|
239
|
-
if (
|
|
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) => {
|
|
240
226
|
try {
|
|
241
|
-
return
|
|
227
|
+
return I([u(b)], L);
|
|
242
228
|
} catch {
|
|
243
|
-
return delete c[
|
|
229
|
+
return delete c[a], "";
|
|
244
230
|
}
|
|
245
231
|
}, r in c) {
|
|
246
|
-
const
|
|
232
|
+
const b = `${P}${I(y, $)}`;
|
|
247
233
|
try {
|
|
248
|
-
c[
|
|
234
|
+
c[b] = W(M(z(c[r], 0)[0])), delete c[r];
|
|
249
235
|
} catch {
|
|
250
236
|
}
|
|
251
237
|
}
|
|
252
|
-
|
|
238
|
+
M = (b) => {
|
|
253
239
|
try {
|
|
254
|
-
return
|
|
240
|
+
return x(z(b, L)[0]);
|
|
255
241
|
} catch {
|
|
256
|
-
return delete c[
|
|
242
|
+
return delete c[a], "";
|
|
257
243
|
}
|
|
258
244
|
};
|
|
259
|
-
} else delete c[`${H}${
|
|
260
|
-
const
|
|
261
|
-
const
|
|
262
|
-
return m =
|
|
245
|
+
} else delete c[`${H}${N}`];
|
|
246
|
+
const K = (r) => {
|
|
247
|
+
const u = z(r, 0);
|
|
248
|
+
return m = u[1], M(u[0]);
|
|
263
249
|
};
|
|
264
|
-
let
|
|
265
|
-
if (
|
|
266
|
-
if (f = o,
|
|
267
|
-
|
|
250
|
+
let X = !0;
|
|
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) {
|
|
253
|
+
X = !1;
|
|
268
254
|
try {
|
|
269
|
-
s(
|
|
255
|
+
s(a in c ? K(c[a]) : e);
|
|
270
256
|
} catch {
|
|
271
|
-
console.warn("Invalid json value", c[
|
|
257
|
+
console.warn("Invalid json value", c[a]);
|
|
272
258
|
}
|
|
273
259
|
}
|
|
274
260
|
return o();
|
|
275
|
-
},
|
|
261
|
+
}, A = (r) => {
|
|
276
262
|
if (r === e) {
|
|
277
263
|
this.reset();
|
|
278
264
|
return;
|
|
279
265
|
}
|
|
280
|
-
c[
|
|
266
|
+
c[a] = W(r);
|
|
281
267
|
}, this.reset = () => {
|
|
282
|
-
delete c[
|
|
283
|
-
},
|
|
284
|
-
if (
|
|
285
|
-
let r = !1,
|
|
286
|
-
|
|
287
|
-
clearTimeout(
|
|
268
|
+
delete c[a], S(e, !0);
|
|
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()));
|
|
288
274
|
};
|
|
289
275
|
} else
|
|
290
|
-
|
|
276
|
+
V[a] = (r) => {
|
|
291
277
|
if (r.newValue === null) {
|
|
292
278
|
this.reset();
|
|
293
279
|
return;
|
|
294
280
|
}
|
|
295
281
|
try {
|
|
296
|
-
|
|
282
|
+
S(K(r.newValue));
|
|
297
283
|
} catch {
|
|
298
284
|
console.warn("Invalid json value", r.newValue);
|
|
299
285
|
}
|
|
@@ -305,31 +291,31 @@ let v;
|
|
|
305
291
|
try {
|
|
306
292
|
v = new BroadcastChannel("updateHere"), v.addEventListener("message", (t) => {
|
|
307
293
|
var e;
|
|
308
|
-
(e =
|
|
294
|
+
(e = U[t.data.key]) == null || e.set(t.data.value, !0);
|
|
309
295
|
});
|
|
310
296
|
} catch {
|
|
311
297
|
}
|
|
312
|
-
const c = localStorage,
|
|
298
|
+
const c = localStorage, V = {}, U = {}, ge = (t, e) => {
|
|
313
299
|
throw `${e} is readonly property`;
|
|
314
300
|
};
|
|
315
301
|
window.addEventListener("storage", (t) => {
|
|
316
302
|
var e;
|
|
317
|
-
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);
|
|
318
304
|
});
|
|
319
|
-
const
|
|
305
|
+
const we = c.setItem.bind(c), ye = c.removeItem.bind(c);
|
|
320
306
|
c.setItem = (t, e) => {
|
|
321
|
-
|
|
307
|
+
U[t] === void 0 && we.call(c, t, e);
|
|
322
308
|
};
|
|
323
309
|
c.removeItem = (t) => {
|
|
324
|
-
|
|
310
|
+
U[t] === void 0 && ye.call(c, t);
|
|
325
311
|
};
|
|
326
|
-
const Ae = /"exp":\s*(\d+)/,
|
|
312
|
+
const Ae = /"exp":\s*(\d+)/, P = "atom\\", H = "atom`s\\", J = {}, ee = {}, te = /[a-z]/gi, R = 5, G = {};
|
|
327
313
|
for (let t = 54; t < 80; t++) {
|
|
328
314
|
if (t === 68 || t === 72 || t === 55) continue;
|
|
329
315
|
const e = String.fromCharCode(t + 43).toUpperCase(), n = String.fromCharCode(t + 43).toLowerCase();
|
|
330
|
-
|
|
316
|
+
G[e] = n, G[n] = e;
|
|
331
317
|
}
|
|
332
|
-
const se = (t) =>
|
|
318
|
+
const se = (t) => G[t] ?? t, I = /* @__PURE__ */ (() => {
|
|
333
319
|
const t = [
|
|
334
320
|
(e) => JSON.stringify(e),
|
|
335
321
|
(e) => btoa(encodeURI(t[0](e))),
|
|
@@ -368,12 +354,12 @@ const se = (t) => Q[t] ?? t, C = /* @__PURE__ */ (() => {
|
|
|
368
354
|
setTimeout(() => {
|
|
369
355
|
Object.keys(c).forEach((t) => {
|
|
370
356
|
var s;
|
|
371
|
-
if (typeof c[t] != "string" || !t.startsWith(
|
|
357
|
+
if (typeof c[t] != "string" || !t.startsWith(P) && !t.startsWith(H)) return;
|
|
372
358
|
const e = +((s = c[t].match(Ae)) == null ? void 0 : s[1]);
|
|
373
359
|
if (!e || e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return;
|
|
374
360
|
const n = z(c[t], 0);
|
|
375
361
|
!Array.isArray(n) || n[1] == null || !("exp" in n[1]) || n[1].exp !== e || (ee[t] = setTimeout(() => {
|
|
376
|
-
|
|
362
|
+
J[t] ? J[t].reset() : delete c[t];
|
|
377
363
|
}, e * 1e3 - Date.now()));
|
|
378
364
|
});
|
|
379
365
|
}, 1e3);
|
|
@@ -385,9 +371,9 @@ export {
|
|
|
385
371
|
fe as AtomNumberDoActions,
|
|
386
372
|
he as AtomObjectDoActions,
|
|
387
373
|
me as AtomSetDoActions,
|
|
388
|
-
|
|
374
|
+
Te as atom,
|
|
389
375
|
Se as configureAtomaric,
|
|
390
|
-
|
|
376
|
+
T as configuredOptions,
|
|
391
377
|
$e as useAtom,
|
|
392
378
|
De as useAtomDo,
|
|
393
379
|
be as useAtomGet,
|
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 $={},me=t=>{$.useSyncExternalStore=j=t.useSyncExternalStore,$.keyPathSeparator=t.keyPathSeparator,$.securifyKeyLevel=t.securifyKeyLevel,$.securifyValueLevel=t.securifyValueLevel},
|
|
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
|
@@ -8,30 +8,28 @@ configureAtomaric({
|
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
describe('Map', () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
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
|
+
});
|
|
25
24
|
|
|
25
|
+
test('do.setValue()', async () => {
|
|
26
26
|
testAtom.do.setValue('!', 1);
|
|
27
27
|
testAtom.do.setValue('@', 0);
|
|
28
28
|
testAtom.do.setValue('#', { asasa: '' });
|
|
29
29
|
testAtom.do.setValue('', 123);
|
|
30
30
|
await wait();
|
|
31
31
|
|
|
32
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual(
|
|
33
|
-
'[{"\\"!\\"":1,"\\"@\\"":0,"\\"#\\"":{"asasa":""},"\\"\\"":123}]',
|
|
34
|
-
);
|
|
32
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["@",0],["#",{"asasa":""}],["",123]]]');
|
|
35
33
|
|
|
36
34
|
expect(testAtom.get()).toEqual(
|
|
37
35
|
new Map<string, Value>([
|
|
@@ -41,31 +39,39 @@ describe('Map', () => {
|
|
|
41
39
|
['', 123],
|
|
42
40
|
]),
|
|
43
41
|
);
|
|
42
|
+
});
|
|
44
43
|
|
|
44
|
+
test('do.<filterKeyValues>()', async () => {
|
|
45
45
|
testAtom.do.filterKeyValues();
|
|
46
46
|
await wait();
|
|
47
47
|
|
|
48
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual('[
|
|
48
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["#",{"asasa":""}],["",123]]]');
|
|
49
|
+
});
|
|
49
50
|
|
|
51
|
+
test('do.delete()', async () => {
|
|
50
52
|
testAtom.do.delete('#');
|
|
51
53
|
await wait();
|
|
52
54
|
|
|
53
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual('[
|
|
55
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123]]]');
|
|
56
|
+
});
|
|
54
57
|
|
|
58
|
+
test('do.toggle()', async () => {
|
|
55
59
|
testAtom.do.toggle('#', 555);
|
|
56
60
|
await wait();
|
|
57
61
|
|
|
58
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual('[
|
|
62
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[["!",1],["",123],["#",555]]]');
|
|
59
63
|
|
|
60
64
|
testAtom.do.toggle('', 999);
|
|
61
65
|
testAtom.do.toggle('!', 1234567890);
|
|
62
66
|
await wait();
|
|
63
67
|
|
|
64
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual('[
|
|
68
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[["#",555]]]');
|
|
69
|
+
});
|
|
65
70
|
|
|
71
|
+
test('do.clear()', async () => {
|
|
66
72
|
testAtom.do.clear();
|
|
67
73
|
await wait();
|
|
68
74
|
|
|
69
|
-
expect(localStorage[makeFullKey('map:test')]).toEqual('[
|
|
75
|
+
expect(localStorage[makeFullKey('map:test')]).toEqual('[[]]');
|
|
70
76
|
});
|
|
71
77
|
});
|