From fd04474e1c355504be97747915708645fd338b6c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 15 Aug 2023 21:30:46 +0200 Subject: [PATCH] refactor: cleanup handlers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 2 +- src/pools/pool.ts | 8 ++++---- src/pools/worker.ts | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) 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. -- 2.34.1