web-atoms-core
Version:
79 lines • 2.72 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* AtomOnce will execute given method only once and it will prevent recursive calls.
* This is important when you want to update source and destination and prevent recursive calls.
* @example
* private valueOnce: AtomOnce = new AtomOnce();
*
* private onValueChanged(): void {
* valueOnce.run(()=> {
* this.value = compute(this.target);
* });
* }
*
* private onTargetChanged(): void {
* valueOnce.run(() => {
* this.target = computeInverse(this.value);
* });
* }
*/
var AtomOnce = /** @class */ (function () {
function AtomOnce() {
}
/**
* AtomOnce will execute given method only once and it will prevent recursive calls.
* This is important when you want to update source and destination and prevent recursive calls.
* @example
* private valueOnce: AtomOnce = new AtomOnce();
*
* private onValueChanged(): void {
* valueOnce.run(()=> {
* this.value = compute(this.target);
* });
* }
*
* private onTargetChanged(): void {
* valueOnce.run(() => {
* this.target = computeInverse(this.value);
* });
* }
*/
AtomOnce.prototype.run = function (f) {
var _this = this;
if (this.isRunning) {
return;
}
var isAsync = false;
try {
this.isRunning = true;
var p = f();
if (p && p.then && p.catch) {
isAsync = true;
p.then(function () {
_this.isRunning = false;
}).catch(function () {
_this.isRunning = false;
});
}
}
finally {
if (!isAsync) {
this.isRunning = false;
}
}
};
return AtomOnce;
}());
exports.AtomOnce = AtomOnce;
});
//# sourceMappingURL=AtomOnce.js.map