computed-async-mobx
Version:
Define a computed by returning a Promise
59 lines • 1.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.throttledComputed = void 0;
var mobxShim_1 = require("./mobxShim");
var autorunThrottled_1 = require("./autorunThrottled");
/**
* Like computed, except that after creation, subsequent re-evaluations
* are throttled to occur at the specified minimum interval.
*
* @param compute The function to evaluate in reaction
* @param delay The minimum delay between evaluations
* @param name (optional) For MobX debug purposes
*/
function throttledComputed(compute, delay, name) {
"use strict";
var monitor;
var latestValue;
var latestError;
function wake() {
sleep();
monitor = autorunThrottled_1.autorunThrottled(observe, delay, name);
}
function observe() {
try {
var newValue = compute();
if (latestError || newValue !== latestValue) {
latestValue = newValue;
latestError = undefined;
atom.reportChanged();
}
}
catch (x) {
latestError = x;
atom.reportChanged();
}
}
function sleep() {
var dispose = monitor;
monitor = undefined;
if (dispose) {
dispose();
}
}
var atom = mobxShim_1.createAtom(name || "DelayedComputedAtom", wake, sleep);
return {
get: function () {
atom.reportObserved();
if (latestError) {
throw latestError;
}
return latestValue;
},
refresh: function () {
wake();
}
};
}
exports.throttledComputed = throttledComputed;
//# sourceMappingURL=throttledComputed.js.map
;