@dhmk/atom
Version:
Lightweight mobx-like observable values, computed values and side-effects
44 lines (43 loc) • 1.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.runtime = void 0;
exports.runtime = {
currentAtom: undefined,
counter: 0,
requireAction: true,
effects: new Set(),
addEffect: function (x) {
exports.runtime.effects.add(x);
},
runEffects: function () {
if (exports.runtime.counter > 0)
return;
exports.runtime.counter++;
var errors = Array();
exports.runtime.effects.forEach(function (x) {
exports.runtime.effects.delete(x);
try {
x.actualize();
}
catch (e) {
errors.push(e);
}
});
exports.runtime.counter--;
if (errors.length)
throw errors.length === 1 ? errors[0] : new AggregateError(errors);
},
act: function (fn) {
var prevAtom = exports.runtime.currentAtom;
exports.runtime.currentAtom = undefined;
exports.runtime.counter++;
try {
return fn();
}
finally {
exports.runtime.counter--;
exports.runtime.currentAtom = prevAtom;
exports.runtime.runEffects();
}
},
};
;