2022-08-17 12:39:41 +00:00
|
|
|
import {TimeoutError} from '../common/Errors.js';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface DeferredPromise<T> extends Promise<T> {
|
|
|
|
finished: () => boolean;
|
|
|
|
resolved: () => boolean;
|
|
|
|
resolve: (_: T) => void;
|
|
|
|
reject: (_: Error) => void;
|
|
|
|
}
|
2022-08-24 10:05:32 +00:00
|
|
|
|
2022-09-05 08:30:47 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface DeferredPromiseOptions {
|
|
|
|
message: string;
|
|
|
|
timeout: number;
|
2022-08-30 14:24:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
/**
|
2022-09-05 08:30:47 +00:00
|
|
|
* Creates and returns a promise along with the resolve/reject functions.
|
2022-08-17 12:39:41 +00:00
|
|
|
*
|
2022-08-30 14:24:51 +00:00
|
|
|
* If the promise has not been resolved/rejected within the `timeout` period,
|
2022-09-05 08:30:47 +00:00
|
|
|
* the promise gets rejected with a timeout error. `timeout` has to be greater than 0 or
|
|
|
|
* it is ignored.
|
2022-08-17 12:39:41 +00:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-09-05 08:30:47 +00:00
|
|
|
export function createDeferredPromise<T>(
|
|
|
|
opts?: DeferredPromiseOptions
|
|
|
|
): DeferredPromise<T> {
|
2022-08-17 12:39:41 +00:00
|
|
|
let isResolved = false;
|
|
|
|
let isRejected = false;
|
|
|
|
let resolver = (_: T): void => {};
|
|
|
|
let rejector = (_: Error) => {};
|
|
|
|
const taskPromise = new Promise<T>((resolve, reject) => {
|
|
|
|
resolver = resolve;
|
|
|
|
rejector = reject;
|
|
|
|
});
|
2022-09-05 08:30:47 +00:00
|
|
|
const timeoutId =
|
|
|
|
opts && opts.timeout > 0
|
|
|
|
? setTimeout(() => {
|
|
|
|
isRejected = true;
|
|
|
|
rejector(new TimeoutError(opts.message));
|
|
|
|
}, opts.timeout)
|
|
|
|
: undefined;
|
2022-08-17 12:39:41 +00:00
|
|
|
return Object.assign(taskPromise, {
|
|
|
|
resolved: () => {
|
|
|
|
return isResolved;
|
|
|
|
},
|
|
|
|
finished: () => {
|
|
|
|
return isResolved || isRejected;
|
|
|
|
},
|
|
|
|
resolve: (value: T) => {
|
2022-08-30 14:24:51 +00:00
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
isResolved = true;
|
|
|
|
resolver(value);
|
|
|
|
},
|
|
|
|
reject: (err: Error) => {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
isRejected = true;
|
|
|
|
rejector(err);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|