UNPKG

@dhmk/atom

Version:

Lightweight mobx-like observable values, computed values and side-effects

41 lines (40 loc) 1.07 kB
export var runtime = { currentAtom: undefined, counter: 0, requireAction: true, effects: new Set(), addEffect: function (x) { runtime.effects.add(x); }, runEffects: function () { if (runtime.counter > 0) return; runtime.counter++; var errors = Array(); runtime.effects.forEach(function (x) { runtime.effects.delete(x); try { x.actualize(); } catch (e) { errors.push(e); } }); runtime.counter--; if (errors.length) throw errors.length === 1 ? errors[0] : new AggregateError(errors); }, act: function (fn) { var prevAtom = runtime.currentAtom; runtime.currentAtom = undefined; runtime.counter++; try { return fn(); } finally { runtime.counter--; runtime.currentAtom = prevAtom; runtime.runEffects(); } }, };