UNPKG

atomaric

Version:

Manage your project state

392 lines (391 loc) 12.9 kB
let k = () => { throw "call configureAtomaric() before all!"; }; const _ = {}, Se = (t) => { _.useSyncExternalStore = k = t.useSyncExternalStore, _.keyPathSeparator = t.keyPathSeparator, _.securifyKeyLevel = t.securifyKeyLevel, _.securifyValueLevel = t.securifyValueLevel; }, 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)], _e = (t, e) => new pe(t, e); class V { constructor(e) { if (e) return new Proxy(this, { get: (i, s) => s in this ? i[s] : e[s] }); } } const ce = (t, e) => { const i = (s, n, c) => new Proxy(s, { get: (l, h, p) => { const g = c === 0 && e.get != null ? e.get(l, h, p) : l[h]; return typeof g == "object" && g !== null ? i( Array.isArray(g) ? g.slice(0) : { ...g }, n.concat(Array.isArray(l) ? +h : h), c + 1 ) : g; }, set: (l, h, p) => (e.onSet(l, n, h, p, l[h]) && (l[h] = p), !0) }); return i(t, [], 0); }; class O extends V { constructor() { super(...arguments), this.updateValue = (e, i) => { const s = Array.isArray(e) ? e.slice(0) : { ...e }; let n = !1; const c = ce(e, { onSet: (l, h, p, g, w) => { if (g === w) return !0; let m = s; n = !0; for (const f of h) { const d = m[f]; m = m[f] = Array.isArray(d) ? d.slice() : { ...d }; } return m[p] = g, !0; } }); return i(c), n ? s : e; }; } } class oe extends O { 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 n = this.a.get(), c = this.updateValue(n, s); c !== n && this.a.set(c); }, this.filter = (s) => { const n = this.a.get().filter(s ?? ae); n.length !== this.a.get().length && this.a.set(n); }, this.add = (s) => { this.a.get().includes(s) || this.a.set(this.a.get().concat([s])); }, this.removeFirst = (s) => { const n = this.a.get().indexOf(s); if (n < 0) return; const c = this.a.get().slice(0); c.splice(n, 1), this.a.set(c); }, this.toggle = (s, n) => { const c = this.a.get().slice(), l = c.indexOf(s); l < 0 ? n ? c.unshift(s) : c.push(s) : c.splice(l, 1), this.a.set(c); }; } } const ae = (t) => t; class ue extends V { constructor(e, i) { super(i), this.a = e, this.toggle = () => { this.a.set(!this.a.get()); }; } } class le extends V { constructor(e, i) { super(i), this.a = e, this.setValue = (s, n) => { if (this.a.get().get(s) === n) return; const c = new Map(this.a.get()); c.set(s, n), this.a.set(c); }, this.setIfNo = (s, n) => { if (this.a.get().has(s)) return; const c = new Map(this.a.get()); c.set(s, n), this.a.set(c); }, this.delete = (s) => { if (!this.a.get().has(s)) return; const n = new Map(this.a.get()); n.delete(s), this.a.set(n); }, this.toggle = (s, n) => { const c = new Map(this.a.get()); c.has(s) ? c.delete(s) : c.set(s, n), this.a.set(c); }, this.clear = () => { this.a.set(/* @__PURE__ */ new Map()); }, this.a = e; } } class fe extends V { constructor(e, i) { super(i), this.a = e, this.increment = (s) => { this.a.set(+this.a.get() + (s ?? 1)); }; } } class he extends O { constructor(e, i) { super(i), this.a = e, this.setPartial = (s) => this.a.set((n) => ({ ...n, ...typeof s == "function" ? s(this.a.get()) : s })), this.update = (s) => { const n = this.a.get(), c = this.updateValue(n, s); c !== n && this.a.set(c); }, this.setDeepPartial = (s, n, c, l = _.keyPathSeparator || ".") => { if (!l) return; if (s.includes(l)) { let g = s.split(l); const w = g[g.length - 1]; g = g.slice(0, -1); const m = { ...this.a.get() }; let f = m, d = c; for (const y of g) { d = d == null ? void 0 : d[Array.isArray(d) ? "0" : y]; const D = f[v(f, y)] ?? (Array.isArray(d) ? [] : {}); if (D == null || typeof D != "object") { if (c == null) throw "Incorrect path for setDeepPartial"; const S = typeof n == "function" ? n(void 0) : n; this.a.get()[s] !== S && this.setPartial({ [s]: S }); return; } f = f[v(f, y)] = Array.isArray(D) ? [...D] : { ...D }; } const E = f[w]; f[w] = typeof n == "function" ? n(f[w]) : n, E !== f[w] && this.a.set(m); return; } const h = this.a.get()[s], p = typeof n == "function" ? n(h) : n; p !== h && this.setPartial({ [s]: p }); }; } } const v = (t, e) => Array.isArray(t) ? `${+e}` : e; class de extends V { 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 n = new Set(this.a.get()); n.delete(s), this.a.set(n); }, this.toggle = (s) => { const n = new Set(this.a.get()); n.has(s) ? n.delete(s) : n.add(s), this.a.set(n); }, this.clear = () => { this.a.set(/* @__PURE__ */ new Set()); }, this.a = e; } } const ge = (t, e, i) => { const s = typeof i == "object" && i != null && "do" in i ? i.do( (c, l) => e.set(c, l), () => e.get(), e, (c, l, h) => e.setDeferred(c, l, h) ) : null, n = {}; 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 V(s); }; class pe { constructor(e, i) { e = typeof e == "function" ? e() : e; const s = (r) => g = r, n = () => g, c = /* @__PURE__ */ new Set(), l = (r) => r(f()); let h = !0, p = !1, g = e, w, m = () => { }, f = () => n(), d = null, E = () => { const r = ge(e, y, i); return E = () => r, r; }; const y = new Proxy(this, { get: (r, u) => u === "do" ? E() : r[u], set: we }), D = () => { h = !0, p !== !0 && m(f()), p = !1; try { H.postMessage({ key: a, value: n() }); } catch { } }, S = (r, u) => { const x = typeof r == "function" ? r(f()) : r; x !== f() && (s(x), p = u, h && (h = !1, c.forEach(l), queueMicrotask(D))); }; this.set = (r, u) => S(r, u), this.get = () => f(), this.initialValue = e, this.isInitialValue = () => e === n(), this.subscribe = (r) => (c.add(r), () => { c.delete(r); }), this.reset = () => { S(e, !0); }; const re = (r, u) => { S(r, u), w = void 0; }; if (this.setDeferred = (r, u = 500, x, b = !0) => { b && w === void 0 && S(r, x), clearTimeout(w), w = setTimeout(re, u, r, x); }, i == null) return y; let A = null, j = null, q = !0, F = !0, N = !1, $ = 0, M = 0, K = -1, T = e instanceof Set ? (r) => new Set(r) : e instanceof Map ? (r) => new Map(r) : (r) => r, I = e instanceof Set ? (r) => { if (r instanceof Set) return Array.from(r); throw console.error(r), "The value is not Set instance"; } : e instanceof Map ? (r) => { if (r instanceof Map) { const u = []; return r.forEach((x, b) => u.push([b, x])), u; } throw console.error(r), "The value is not Set instance"; } : (r) => r; if (typeof i == "string") A = i; else if ("storeKey" in i) q = i.warnOnDuplicateStoreKey ?? q, F = i.listenStorageChanges ?? F, A = i.storeKey, T = i.unzipValue ?? T, I = i.zipValue ?? I, N = i.unchangable ?? N, $ = i.securifyKeyLevel ?? _.securifyKeyLevel ?? $, M = i.securifyValueLevel ?? _.securifyValueLevel ?? M, j = i.exp ?? j; else return y; const B = $ ? L(A, $) : A, a = `${M ? J : C}${B}`; if ($) { const r = `${C}${A}`; r in o && (o[a] = o[r], delete o[r]); } else { const r = `${C}${L(A, $)}`; r in o && (o[a] = o[r], delete o[r]); } const W = j === null || !(j(y, a in o) instanceof Date) ? (r) => L([I(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), L([I(r), d], 0)); if (M) { const r = `${C}${B}`, u = I, x = T; if (I = (b) => { try { return L([u(b)], M); } catch { return delete o[a], ""; } }, r in o) { const b = `${C}${L(A, $)}`; try { o[b] = W(T(z(o[r], 0)[0])), delete o[r]; } catch { } } T = (b) => { try { return x(z(b, M)[0]); } catch { return delete o[a], ""; } }; } else delete o[`${J}${B}`]; const X = (r) => { const u = z(r, 0); return d = u[1], T(u[0]); }; let Y = !0; if (G[a] = y, o[`atom/${A}`] && (o[a] || (o[a] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => { if (f = n, Y) { Y = !1; try { s(a in o ? X(o[a]) : e); } catch { console.warn("Invalid json value", o[a]); } } return n(); }, m = (r) => { if (r === e) { this.reset(); return; } o[a] = W(r); }, this.reset = () => { delete o[a], S(e, !0); }, q && P[a] !== void 0 && console.warn("Duplicate Atom key", A), F) if (N) { let r = !1, u; U[a] = this, P[a] = () => { clearTimeout(u), u = setTimeout(() => r = !1, 10), !r && (r = !0, o[a] = W(n())); }; } else P[a] = (r) => { if (r.newValue === null) { this.reset(); return; } try { S(X(r.newValue)); } catch { console.warn("Invalid json value", r.newValue); } }; return y; } } let H; try { H = new BroadcastChannel("updateHere"), H.addEventListener("message", (t) => { var e; (e = U[t.data.key]) == null || e.set(t.data.value, !0); }); } catch { } const o = localStorage, P = {}, U = {}, we = (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 ye = o.setItem.bind(o), Ae = o.removeItem.bind(o); o.setItem = (t, e) => { U[t] === void 0 && ye.call(o, t, e); }; o.removeItem = (t) => { U[t] === void 0 && Ae.call(o, t); }; const me = /"exp":\s*(\d+)/, C = "atom\\", J = "atom`s\\", G = {}, ee = {}, te = /[a-z]/gi, R = 5, Q = {}; 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(); Q[e] = i, Q[i] = e; } const se = (t) => Q[t] ?? t, L = /* @__PURE__ */ (() => { const t = [ (e) => JSON.stringify(e), (e) => btoa(encodeURI(t[0](e))), (e) => { const i = t[1](e); return `${i.slice(0, R)}${i.slice(R).replace(te, se)}`; }, (e) => btoa(t[2](e)) ]; return (e, i) => { try { return t[i](e); } catch (s) { if (i === 0) throw s; return t[0](e); } }; })(), z = /* @__PURE__ */ (() => { const t = [ (e) => JSON.parse(e), (e) => t[0](decodeURI(atob(e))), (e) => t[1]( `${e.slice(0, R)}${e.slice(R).replace(te, se)}` ), (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(o).forEach((t) => { var s; if (typeof o[t] != "string" || !t.startsWith(C) && !t.startsWith(J)) return; const e = (s = o[t].match(me)) == null ? void 0 : s[1]; if (!e || +e * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return; const i = z(o[t], 0); !Array.isArray(i) || i[1] == null || !("exp" in i[1]) || i[1].exp !== e || (ee[t] = setTimeout(() => { G[t] ? G[t].reset() : delete o[t]; }, +e * 1e3 - Date.now())); }); }, 1e3); export { pe as Atom, oe as AtomArrayDoActions, ue as AtomBooleanDoActions, le as AtomMapDoActions, fe as AtomNumberDoActions, he as AtomObjectDoActions, de as AtomSetDoActions, _e as atom, Se as configureAtomaric, _ as configuredOptions, $e as useAtom, De as useAtomDo, be as useAtomGet, ie as useAtomSet, xe as useAtomSetDeferred, ne as useAtomValue };