@pdftron/webviewer-react-toolkit
Version:
A React component library for integrating with PDFTron WebViewer API.
47 lines (46 loc) • 1.93 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import { futureableOrLazyToFuturable, memoizedPromiseToFuturableOrLazy } from './futurable';
/**
* This class is responsible for wrapping tasks in a promise that won't be
* executed until the result is actually required. Calling .get() on the class
* will start the task, and resolve with the result. If the task has already
* been executed once, it will resolve immediatly with the last result.
*/
var MemoizedPromise = /** @class */ (function () {
function MemoizedPromise(futurableOrLazy, options) {
var _this = this;
if (options === void 0) { options = {}; }
/** Resolves with a promise for the value. */
this.get = function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this._done)
return [2 /*return*/, this._result];
this._result = futureableOrLazyToFuturable(this._futurableOrLazy);
this._done = true;
return [2 /*return*/, this._result];
});
}); };
if (futurableOrLazy instanceof MemoizedPromise) {
this._futurableOrLazy = memoizedPromiseToFuturableOrLazy(futurableOrLazy);
}
else {
this._futurableOrLazy = futurableOrLazy;
}
this._result = undefined;
this._done = false;
if (options.preprocess || typeof this._futurableOrLazy !== 'function') {
this._result = futureableOrLazyToFuturable(this._futurableOrLazy);
this._done = true;
}
}
Object.defineProperty(MemoizedPromise.prototype, "done", {
/** Is true if the value is memoized. */
get: function () {
return this._done;
},
enumerable: false,
configurable: true
});
return MemoizedPromise;
}());
export { MemoizedPromise };