From: Jérôme Benoit Date: Tue, 15 Aug 2023 19:30:46 +0000 (+0200) Subject: refactor: cleanup handlers X-Git-Tag: v2.6.28~21 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=fd04474e1c355504be97747915708645fd338b6c;p=poolifier.git refactor: cleanup handlers Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 8fb25b64..06e69a6e 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -933,6 +933,7 @@ export abstract class AbstractPool< protected createAndSetupWorkerNode (): number { const worker = this.createWorker() + worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION) worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION) worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION) worker.on('error', (error) => { @@ -952,7 +953,6 @@ export abstract class AbstractPool< this.redistributeQueuedTasks(workerNodeKey) } }) - worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION) worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION) worker.once('exit', () => { this.removeWorkerNode(worker) diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 24b506ca..f4d9c7bb 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -111,6 +111,10 @@ export interface TasksQueueOptions { * @typeParam Worker - Type of worker. */ export interface PoolOptions { + /** + * A function that will listen for online event on each worker. + */ + onlineHandler?: OnlineHandler /** * A function that will listen for message event on each worker. */ @@ -119,10 +123,6 @@ export interface PoolOptions { * A function that will listen for error event on each worker. */ errorHandler?: ErrorHandler - /** - * A function that will listen for online event on each worker. - */ - onlineHandler?: OnlineHandler /** * A function that will listen for exit event on each worker. */ diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 2da91457..5dbb6ac5 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -182,9 +182,9 @@ export interface IWorker { * @param event - The event. * @param handler - The event handler. */ - readonly on: ((event: 'message', handler: MessageHandler) => void) & + readonly on: ((event: 'online', handler: OnlineHandler) => void) & + ((event: 'message', handler: MessageHandler) => void) & ((event: 'error', handler: ErrorHandler) => void) & - ((event: 'online', handler: OnlineHandler) => void) & ((event: 'exit', handler: ExitHandler) => void) /** * Registers a listener to the exit event that will only be performed once.