UNPKG

@dhmk/atom

Version:

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

93 lines (92 loc) 3.72 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { AtomState, defaultEffectOptions, } from "../types"; import { runtime } from "../runtime"; import { reportError } from "../shared"; import { ComputedAtom } from "./computed"; var EffectAtom = /** @class */ (function (_super) { __extends(EffectAtom, _super); function EffectAtom(fn, effectOptions) { var _this = _super.call(this, fn, { equals: function () { return false; }, onBecomeObserved: effectOptions === null || effectOptions === void 0 ? void 0 : effectOptions.onBecomeObserved, onBecomeUnobserved: effectOptions === null || effectOptions === void 0 ? void 0 : effectOptions.onBecomeUnobserved, }) || this; _this.isObserved = true; _this.isCalculating = false; _this.isDisposed = false; _this.shouldRecalc = false; _this.effectOptions = __assign(__assign({}, defaultEffectOptions), effectOptions); var actualize = _super.prototype.actualize.bind(_this); _this.run = function () { if (!_this.isObserved) return; runtime.addEffect({ actualize: actualize }); runtime.runEffects(); }; return _this; } EffectAtom.prototype.invalidate = function (state, isValueAtom) { _super.prototype.invalidate.call(this, state, isValueAtom); runtime.addEffect(this); if (isValueAtom) this.shouldRecalc = true; }; EffectAtom.prototype.calculate = function () { if (this.isDisposed) return; this.shouldRecalc = false; this.isCalculating = true; _super.prototype.calculate.call(this); this.isCalculating = false; if (this.isError) { runtime.addEffect({ actualize: reportError(this.value) }); } if (this.shouldRecalc) { this.state = AtomState.Stale; } if (this.isDisposed) { this.dispose(); } }; EffectAtom.prototype.actualize = function () { this.effectOptions.scheduler(this.run); }; EffectAtom.prototype.dispose = function () { this.isDisposed = true; if (this.isCalculating) return; _super.prototype.dispose.call(this); runtime.runEffects(); }; EffectAtom.prototype.start = function () { this.invalidate(AtomState.Stale, false); runtime.runEffects(); }; return EffectAtom; }(ComputedAtom)); export { EffectAtom };