@web-atoms/core-docs
Version:
76 lines • 2.58 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 });
exports.AtomOnce = void 0;
/**
* 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);
* });
* }
*/
class 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);
* });
* }
*/
run(f) {
if (this.isRunning) {
return;
}
let isAsync = false;
try {
this.isRunning = true;
const p = f();
if (p && p.then && p.catch) {
isAsync = true;
p.then(() => {
this.isRunning = false;
}).catch(() => {
this.isRunning = false;
});
}
}
finally {
if (!isAsync) {
this.isRunning = false;
}
}
}
}
exports.AtomOnce = AtomOnce;
});
//# sourceMappingURL=AtomOnce.js.map