From: Jérôme Benoit Date: Mon, 1 Jan 2024 21:10:22 +0000 (+0100) Subject: refactor: add type alias for worker event handler X-Git-Tag: v3.1.15-0~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3bcbd4c5be5678988281342a1fc1f6a62f9c77d7;p=poolifier.git refactor: add type alias for worker event handler Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 6a5881da..73b2c70c 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -5,12 +5,9 @@ import type { Task } from '../utility-types.js' import { DEFAULT_TASK_NAME, getWorkerId, getWorkerType } from '../utils.js' import { Deque } from '../deque.js' import { - type ErrorHandler, - type ExitHandler, + type EventHandler, type IWorker, type IWorkerNode, - type MessageHandler, - type OnlineHandler, type StrategyData, type WorkerInfo, type WorkerNodeOptions, @@ -151,11 +148,7 @@ export class WorkerNode /** @inheritdoc */ public registerWorkerEventHandler ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ): void { this.worker.on(event, handler) } @@ -163,11 +156,7 @@ export class WorkerNode /** @inheritdoc */ public registerOnceWorkerEventHandler ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ): void { this.worker.once(event, handler) } diff --git a/src/pools/worker.ts b/src/pools/worker.ts index c7a9dcb3..f439c882 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -40,6 +40,17 @@ export type ExitHandler = ( exitCode: number ) => void +/** + * Worker event handler. + * + * @typeParam Worker - Type of worker. + */ +export type EventHandler = + | OnlineHandler + | MessageHandler + | ErrorHandler + | ExitHandler + /** * Measurement statistics. * @@ -216,28 +227,14 @@ export interface IWorker { * @param event - The event. * @param handler - The event handler. */ - readonly on: ( - event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler - ) => void + readonly on: (event: string, handler: EventHandler) => void /** * Registers once an event handler. * * @param event - The event. * @param handler - The event handler. */ - readonly once: ( - event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler - ) => void + readonly once: (event: string, handler: EventHandler) => void /** * Stop all JavaScript execution in the worker thread as soon as possible. * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted. @@ -357,11 +354,7 @@ export interface IWorkerNode */ readonly registerWorkerEventHandler: ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ) => void /** * Registers once a worker event handler. @@ -371,11 +364,7 @@ export interface IWorkerNode */ readonly registerOnceWorkerEventHandler: ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ) => void /** * Gets task function worker usage statistics.