UNPKG

computed-async-mobx

Version:

Define a computed by returning a Promise

38 lines 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.autorunThrottled = void 0; var mobx_1 = require("mobx"); /** * Closely based on autorunAsync but with difference that the first execution * happens synchronously. This allows `delayedComputed` to have a simpler * type signature: the value is never `undefined`. * * @param func The function to execute in reaction * @param delay The minimum delay between executions * @param name (optional) For MobX debug purposes */ function autorunThrottled(func, delay, name) { if (!name) { name = "autorunThrottled"; } var isScheduled = false, isStarted = false; var r = new mobx_1.Reaction(name, function () { if (!isStarted) { isStarted = true; r.track(func); } else if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; if (!r.isDisposed) { r.track(func); } }, delay || 1); } }); r.runReaction(); return r.getDisposer(); } exports.autorunThrottled = autorunThrottled; //# sourceMappingURL=autorunThrottled.js.map