UNPKG

atomaric

Version:

Manage your project state

407 lines (406 loc) 11.8 kB
let O = () => { throw "call configureAtomaric() before all!"; }; const T = {}, ye = (e) => { T.useSyncExternalStore = O = e.useSyncExternalStore, T.keyPathSeparator = e.keyPathSeparator, T.securifyKeyLevel = e.securifyKeyLevel, T.securifyValueLevel = e.securifyValueLevel; }, ne = (e) => O(e.subscribe, e.get), se = (e) => e.set, he = (e) => e.setDeferred, pe = (e) => e.get, we = (e) => e.do, Se = (e) => [ne(e), se(e)], Ae = (e, t) => new ue(e, t), ce = (e, t) => { const c = (u, y, S) => new Proxy(u, { get: (s, n, i) => { const l = S === 0 && t.get != null ? t.get(s, n, i) : s[n]; return typeof l == "object" && l !== null ? c( Array.isArray(l) ? l.slice(0) : { ...l }, y.concat(Array.isArray(s) ? +n : n), S + 1 ) : l; }, set: (s, n, i) => (t.onSet(s, y, n, i, s[n]) && (s[n] = i), !0) }); return c(e, [], 0); }, ie = (e, t, c) => { let u = null; if (typeof e == "number") u = L( t, (s) => ({ increment: (n) => { s.set(+s.get() + (n ?? 0)); } }) ); else if (typeof e == "boolean") u = L( t, (s) => ({ toggle: () => { s.set(!s.get()); } }) ); else if (Array.isArray(e)) u = L( t, (s) => ({ push: (...n) => { s.set(s.get().concat(n)); }, unshift: (...n) => { s.set(n.concat(s.get())); }, update: (n) => { const i = s.get(), l = v(i, n); l !== i && s.set(l); }, filter: (n) => { s.set(s.get().filter(n ?? oe)); }, add: (n) => { s.get().includes(n) || s.set(s.get().concat([n])); }, remove: (n) => { const i = s.get().indexOf(n); if (i < 0) return; const l = s.get().slice(0); l.splice(i, 1), s.set(l); }, toggle: (n, i) => { const l = s.get().slice(), d = l.indexOf(n); d < 0 ? i ? l.unshift(n) : l.push(n) : l.splice(d, 1), s.set(l); } }) ); else if (e instanceof Set) u = L( t, (s) => ({ add: (n) => { s.set(new Set(s.get()).add(n)); }, delete: (n) => { const i = new Set(s.get()); i.delete(n), s.set(i); }, toggle: (n) => { const i = new Set(s.get()); i.has(n) ? i.delete(n) : i.add(n), s.set(i); }, clear: () => { s.set(/* @__PURE__ */ new Set()); } }) ); else if (e instanceof Object) { const s = T.keyPathSeparator || "."; u = L( t, (n) => ({ setPartial: (i) => n.set((l) => ({ ...l, ...typeof i == "function" ? i(n.get()) : i })), update: (i) => { const l = n.get(), d = v(l, i); d !== l && n.set(d); }, setDeepPartial: (i, l, d, b = s) => { if (!b) return; if (i.includes(b)) { let V = i.split(b); const p = V[V.length - 1]; V = V.slice(0, -1); const M = { ...n.get() }; let g = M, P = d; for (const x of V) { P = P == null ? void 0 : P[Array.isArray(P) ? "0" : x]; const $ = g[K(g, x)] ?? (Array.isArray(P) ? [] : {}); if ($ == null || typeof $ != "object") { if (d == null) throw "Incorrect path for setDeepPartial"; const C = typeof l == "function" ? l(void 0) : l; n.get()[i] !== C && n.do.setPartial({ [i]: C }); return; } g = g[K(g, x)] = Array.isArray($) ? [...$] : { ...$ }; } const w = g[p]; g[p] = typeof l == "function" ? l(g[p]) : l, w !== g[p] && n.set(M); return; } const h = n.get()[i], A = typeof l == "function" ? l(h) : l; A !== h && n.do.setPartial({ [i]: A }); } }) ); } const y = typeof c == "object" && c != null && "do" in c ? c.do( (s, n) => t.set(s, n), () => t.get(), t, (s, n, i) => t.setDeferred(s, n, i) ) : null, S = {}; return y && Object.keys(y).forEach((s) => Object.defineProperty(S, s, { get: () => y[s] })), u && Object.keys(u).forEach( (s) => Object.defineProperty(S, s, { get: () => u[s] }) ), S; }, oe = (e) => e, L = (e, t, c) => t(e), v = (e, t) => { const c = Array.isArray(e) ? e.slice(0) : { ...e }; let u = !1; const y = ce(e, { onSet: (S, s, n, i, l) => { if (i === l) return !0; let d = c; u = !0; for (const b of s) { const h = d[b]; d = d[b] = Array.isArray(h) ? h.slice(0) : { ...h }; } return d[n] = i, !0; } }); return t(y), u ? c : e; }, K = (e, t) => Array.isArray(e) ? `${+t}` : t; var le = /* @__PURE__ */ ((e) => (e[e.None = 0] = "None", e[e.Simple = 1] = "Simple", e[e.Middle = 2] = "Middle", e[e.Strong = 3] = "Strong", e))(le || {}); class ue { constructor(t, c) { const u = (r) => l = r, y = () => l, S = /* @__PURE__ */ new Set(), s = (r) => r(h()); let n = !0, i = !1, l = t, d, b = () => { }, h = () => y(), A = null, V = () => { const r = ie(t, p, c); return V = () => r, r; }; const p = new Proxy(this, { get: (r, a) => a === "do" ? V() : r[a], set: fe }), M = () => { n = !0, i !== !0 && b(h()), i = !1; try { H.postMessage({ key: f, value: y() }); } catch { } }, g = (r, a) => { const D = typeof r == "function" ? r(h()) : r; D !== h() && (u(D), i = a, n && (n = !1, S.forEach(s), queueMicrotask(M))); }; this.set = (r, a) => g(r, a), this.get = () => h(), this.initialValue = t, this.isInitialValue = () => t === y(), this.subscribe = (r) => (S.add(r), () => { S.delete(r); }), this.reset = () => { g(t, !0); }; const P = (r, a) => { g(r, a), d = void 0; }; if (this.setDeferred = (r, a = 500, D, W = !0) => { W && d === void 0 && g(r, D), clearTimeout(d), d = setTimeout(P, a, r, D); }, c == null) return p; let w = null, x = null, $ = !0, C = !0, U = !1, R = 0, I = 0, k = -1, E = t instanceof Set ? (r) => new Set(r) : (r) => r, _ = t instanceof Set ? (r) => { if (r instanceof Set) return Array.from(r); throw console.error(r), "The value is not Set instance"; } : (r) => r; if (typeof c == "string") w = c; else if ("storeKey" in c) $ = c.warnOnDuplicateStoreKey ?? $, C = c.listenStorageChanges ?? C, w = c.storeKey, E = c.unzipValue ?? E, _ = c.zipValue ?? _, U = c.unchangable ?? U, R = c.securifyKeyLevel ?? T.securifyKeyLevel ?? R, I = c.securifyValueLevel ?? T.securifyValueLevel ?? I, x = c.exp ?? x; else return p; const F = R ? m( w, 3 /* Strong */ ) : w, f = `${I ? J : z}${F}`; if (R) { const r = `${z}${w}`; r in o && (o[f] = o[r], delete o[r]); } else { const r = `${z}${m( w, 3 /* Strong */ )}`; r in o && (o[f] = o[r], delete o[r]); } if (I) { delete o[`${z}${F}`]; const r = _, a = E; E = (D) => { try { return a(Q(D, I)[0]); } catch { return delete o[f], ""; } }, _ = (D) => { try { return m([r(D)], I); } catch { return delete o[f], ""; } }; } else delete o[`${J}${F}`]; const X = x === null || !(x(p, f in o) instanceof Date) ? (r) => m( [_(r)], 0 /* None */ ) : (r) => (A ?? (A = {}), A.exp = x(p, f in o).getTime(), A.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(k), clearTimeout(ee[f]), k = setTimeout(() => this.reset(), A.exp - Date.now())), A.exp = Math.trunc(A.exp / 1e3), m( [_(r), A], 0 /* None */ )), Y = (r) => { const a = Q( r, 0 /* None */ ); return A = a[1], E(a[0]); }; let Z = !0; if (B[f] = p, o[`atom/${w}`] && (o[f] || (o[f] = `[${o[`atom/${w}`]}]`), delete o[`atom/${w}`]), h = () => { if (h = y, Z) { Z = !1; try { u(f in o ? Y(o[f]) : t); } catch { console.warn("Invalid json value", o[f]); } } return y(); }, b = (r) => { if (r === t) { this.reset(); return; } o[f] = X(r); }, this.reset = () => { delete o[f], g(t, !0); }, $ && j[f] !== void 0 && console.warn("Duplicate Atom key", w), C) if (U) { let r = !1, a; N[f] = this, j[f] = () => { clearTimeout(a), a = setTimeout(() => r = !1, 10), !r && (r = !0, o[f] = X(y())); }; } else j[f] = (r) => { if (r.newValue === null) { this.reset(); return; } try { g(Y(r.newValue)); } catch { console.warn("Invalid json value", r.newValue); } }; return p; } } let H; try { H = new BroadcastChannel("updateHere"), H.addEventListener("message", (e) => { var t; (t = N[e.data.key]) == null || t.set(e.data.value, !0); }); } catch { } const o = localStorage, j = {}, N = {}, fe = (e, t) => { throw `${t} is readonly property`; }; window.addEventListener("storage", (e) => { var t; e.key === null || e.newValue === e.oldValue || (t = j[e.key]) == null || t.call(j, e); }); const ae = o.setItem.bind(o), de = o.removeItem.bind(o); o.setItem = (e, t) => { N[e] === void 0 && ae.call(o, e, t); }; o.removeItem = (e) => { N[e] === void 0 && de.call(o, e); }; const ge = /"exp":\s*(\d+)/, z = "atom\\", J = "atom`s\\", B = {}, ee = {}, te = /[a-z]/gi, q = 5, G = {}; for (let e = 54; e < 80; e++) { if (e === 68 || e === 72 || e === 55) continue; const t = String.fromCharCode(e + 43).toUpperCase(), c = String.fromCharCode(e + 43).toLowerCase(); G[t] = c, G[c] = t; } const re = (e) => G[e] ?? e, m = /* @__PURE__ */ (() => { const e = { 0: (t) => JSON.stringify(t), 1: (t) => btoa(encodeURI(e[ 0 /* None */ ](t))), 2: (t) => { const c = e[ 1 /* Simple */ ](t); return `${c.slice(0, q)}${c.slice(q).replace(te, re)}`; }, 3: (t) => btoa(e[ 2 /* Middle */ ](t)) }; return (t, c) => { try { return e[c](t); } catch (u) { if (c === 0) throw u; return e[ 0 /* None */ ](t); } }; })(), Q = /* @__PURE__ */ (() => { const e = { 0: (t) => JSON.parse(t), 1: (t) => e[ 0 /* None */ ](decodeURI(atob(t))), 2: (t) => e[ 1 /* Simple */ ]( `${t.slice(0, q)}${t.slice(q).replace(te, re)}` ), 3: (t) => e[ 2 /* Middle */ ](atob(t)) }; return (t, c) => { try { return e[c](t); } catch (u) { if (c === 0) throw u; return e[ 0 /* None */ ](t); } }; })(); setTimeout(() => { Object.keys(o).forEach((e) => { var u; if (typeof o[e] != "string" || !e.startsWith(z) && !e.startsWith(J)) return; const t = +((u = o[e].match(ge)) == null ? void 0 : u[1]); if (!t || t * 1e3 - Date.now() > 24 * 60 * 60 * 1e3) return; const c = Q( o[e], 0 /* None */ ); !Array.isArray(c) || c[1] == null || !("exp" in c[1]) || c[1].exp !== t || (ee[e] = setTimeout(() => { B[e] ? B[e].reset() : delete o[e]; }, t * 1e3 - Date.now())); }); }, 1e3); export { ue as Atom, le as AtomSecureLevel, Ae as atom, ye as configureAtomaric, T as configuredOptions, Se as useAtom, we as useAtomDo, pe as useAtomGet, se as useAtomSet, he as useAtomSetDeferred, ne as useAtomValue };