X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=5ef2eed9f39ed7dd83f0744e75d89725b490c7f3;hb=3832ad95c98b136ef703a29685fedebe4a5e3ba2;hp=0685f998d4aaf11d0cb93d86fcdb24b0e3476fbc;hpb=225e05865f45147095b94c5d9ef75536cb5631b3;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 0685f998..5ef2eed9 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -21,9 +21,33 @@ export type ExitHandler = (this: Worker, code: number) => void * Basic interface that describes the minimum required implementation of listener events for a pool-worker. */ export interface IWorker { + /** + * Register a listener to the error event. + * + * @param event `'error'`. + * @param handler The error handler. + */ on(event: 'error', handler: ErrorHandler): void + /** + * Register a listener to the online event. + * + * @param event `'online'`. + * @param handler The online handler. + */ on(event: 'online', handler: OnlineHandler): void + /** + * Register a listener to the exit event. + * + * @param event `'exit'`. + * @param handler The exit handler. + */ on(event: 'exit', handler: ExitHandler): void + /** + * Register a listener to the exit event that will only performed once. + * + * @param event `'exit'`. + * @param handler The exit handler. + */ once(event: 'exit', handler: ExitHandler): void } @@ -82,6 +106,8 @@ export abstract class AbstractPool< public nextWorkerIndex: number = 0 /** + * The tasks map. + * * - `key`: The `Worker` * - `value`: Number of tasks currently in progress on the worker. */ @@ -150,6 +176,12 @@ export abstract class AbstractPool< return this.nextWorkerIndex } + /** + * Perform the task specified in the constructor with the data parameter. + * + * @param data The input for the specified task. + * @returns Promise that will be resolved when the task is successfully completed. + */ public execute (data: Data): Promise { // Configure worker to handle message with the specified task const worker = this.chooseWorker() @@ -160,6 +192,9 @@ export abstract class AbstractPool< return res } + /** + * Shut down every current worker in this pool. + */ public async destroy (): Promise { await Promise.all(this.workers.map(worker => this.destroyWorker(worker))) }