@pdftron/webviewer-react-toolkit
Version:
A React component library for integrating with PDFTron WebViewer API.
36 lines (35 loc) • 1.39 kB
JavaScript
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.
*/
export class MemoizedPromise {
constructor(futurableOrLazy, options = {}) {
/** Resolves with a promise for the value. */
this.get = async () => {
if (this._done)
return this._result;
this._result = futureableOrLazyToFuturable(this._futurableOrLazy);
this._done = true;
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;
}
}
/** Is true if the value is memoized. */
get done() {
return this._done;
}
}