UNPKG

@blabbing/signals

Version:

A reactive state management tool

40 lines (39 loc) 978 B
import { Signal as s } from "signal-polyfill"; import { Signal as _ } from "signal-polyfill"; let e = null; const i = () => { e = []; }, a = (t) => { e.forEach((o) => o(t)), e = null; }, l = (t) => (...r) => { if (e !== null) throw new Error("Actions cannot be used inside other actions."); try { i(); const n = t(...r); return a(!0), n; } catch (n) { throw a(!1), n; } }, f = (t) => Object.defineProperties({}, { _s: { value: new s.State(t) }, _c: { value: new s.State(t) } }), p = (t) => Object.defineProperties({}, { _s: { value: new s.Computed(t) }, _c: { value: new s.Computed(t) } }), d = (t) => e === null ? t._c.get() : t._s.get(), w = (t, o) => { const { _s: r, _c: n } = t; if (e === null) throw new Error("Atoms can only be updated in an action()."); r.set(o), e.push((c) => { c ? n.set(o) : r.set(n.get()); }); }; export { _ as Signal, l as action, f as atom, p as computed, d as get, w as swap };