2020-11-06 08:10:30 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-06 09:07:28
|
|
|
|
* @modify date 2020-11-06 09:09:33
|
|
|
|
* @desc [description]
|
|
|
|
*/
|
2020-11-04 16:32:44 +00:00
|
|
|
|
2020-11-06 08:10:30 +00:00
|
|
|
import { INopePromise } from "../types/nope/nopePromise.interface";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Custom Implementation of Nope-Promise.
|
|
|
|
* They are cancelable.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class NopePromise
|
|
|
|
* @extends {Promise<T>}
|
|
|
|
* @implements {INopePromise<T>}
|
|
|
|
* @template T Type of the Default Promise
|
|
|
|
* @template E Type of the Cancelation Data.
|
|
|
|
*/
|
2021-12-04 07:25:26 +00:00
|
|
|
export class NopePromise<T, E = any>
|
|
|
|
extends Promise<T>
|
|
|
|
implements INopePromise<T>
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Function used to cancel the Element.
|
|
|
|
*
|
|
|
|
* @param {E} reason
|
|
|
|
* @memberof NopePromise
|
|
|
|
*/
|
|
|
|
cancel(reason: E): void {
|
|
|
|
throw new Error("Method has to be overwritten");
|
|
|
|
}
|
2020-11-04 16:32:44 +00:00
|
|
|
|
2021-12-04 07:25:26 +00:00
|
|
|
/**
|
|
|
|
* Attribute holding the Task-ID assinged by a dispatcher.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
* @memberof NopePromise
|
|
|
|
*/
|
|
|
|
taskId: string;
|
2020-11-04 16:32:44 +00:00
|
|
|
|
2021-12-04 07:25:26 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of NopePromise.
|
|
|
|
* @param {((resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void)} executor
|
|
|
|
* @param {(reason: E) => void} [cancel]
|
|
|
|
* @param {string} [taskId]
|
|
|
|
* @memberof NopePromise
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
executor: (
|
|
|
|
resolve: (value?: T | PromiseLike<T>) => void,
|
|
|
|
reject: (reason?: any) => void
|
|
|
|
) => void,
|
|
|
|
cancel?: (reason: E) => void,
|
|
|
|
taskId?: string
|
|
|
|
) {
|
|
|
|
super(executor);
|
2020-11-04 16:32:44 +00:00
|
|
|
|
2021-12-04 07:25:26 +00:00
|
|
|
if (typeof cancel === "function") {
|
|
|
|
this.cancel = cancel;
|
|
|
|
}
|
2020-11-04 16:32:44 +00:00
|
|
|
|
2021-12-04 07:25:26 +00:00
|
|
|
if (typeof taskId === "string") {
|
|
|
|
this.taskId = taskId;
|
2020-11-04 16:32:44 +00:00
|
|
|
}
|
2021-12-04 07:25:26 +00:00
|
|
|
}
|
|
|
|
}
|