refactor: add type alias for worker event handler
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 1 Jan 2024 21:10:22 +0000 (22:10 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 1 Jan 2024 21:10:22 +0000 (22:10 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/worker-node.ts
src/pools/worker.ts

index 6a5881da097bbbd727806a024edf9c6ab06eb7c3..73b2c70c3f3b05ce1031bf57fe5522c40874cfa6 100644 (file)
@@ -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<Worker extends IWorker, Data = unknown>
   /** @inheritdoc */
   public registerWorkerEventHandler (
     event: string,
-    handler:
-    | OnlineHandler<Worker>
-    | MessageHandler<Worker>
-    | ErrorHandler<Worker>
-    | ExitHandler<Worker>
+    handler: EventHandler<Worker>
   ): void {
     this.worker.on(event, handler)
   }
@@ -163,11 +156,7 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
   /** @inheritdoc */
   public registerOnceWorkerEventHandler (
     event: string,
-    handler:
-    | OnlineHandler<Worker>
-    | MessageHandler<Worker>
-    | ErrorHandler<Worker>
-    | ExitHandler<Worker>
+    handler: EventHandler<Worker>
   ): void {
     this.worker.once(event, handler)
   }
index c7a9dcb3cecba710cf62a345f4efd9f029d2f2fb..f439c882a66a703e23cffafc6d02b5f0fcb7fef1 100644 (file)
@@ -40,6 +40,17 @@ export type ExitHandler<Worker extends IWorker> = (
   exitCode: number
 ) => void
 
+/**
+ * Worker event handler.
+ *
+ * @typeParam Worker - Type of worker.
+ */
+export type EventHandler<Worker extends IWorker> =
+  | OnlineHandler<Worker>
+  | MessageHandler<Worker>
+  | ErrorHandler<Worker>
+  | ExitHandler<Worker>
+
 /**
  * Measurement statistics.
  *
@@ -216,28 +227,14 @@ export interface IWorker {
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly on: (
-    event: string,
-    handler:
-    | OnlineHandler<this>
-    | MessageHandler<this>
-    | ErrorHandler<this>
-    | ExitHandler<this>
-  ) => void
+  readonly on: (event: string, handler: EventHandler<this>) => void
   /**
    * Registers once an event handler.
    *
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly once: (
-    event: string,
-    handler:
-    | OnlineHandler<this>
-    | MessageHandler<this>
-    | ErrorHandler<this>
-    | ExitHandler<this>
-  ) => void
+  readonly once: (event: string, handler: EventHandler<this>) => 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<Worker extends IWorker, Data = unknown>
    */
   readonly registerWorkerEventHandler: (
     event: string,
-    handler:
-    | OnlineHandler<Worker>
-    | MessageHandler<Worker>
-    | ErrorHandler<Worker>
-    | ExitHandler<Worker>
+    handler: EventHandler<Worker>
   ) => void
   /**
    * Registers once a worker event handler.
@@ -371,11 +364,7 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
    */
   readonly registerOnceWorkerEventHandler: (
     event: string,
-    handler:
-    | OnlineHandler<Worker>
-    | MessageHandler<Worker>
-    | ErrorHandler<Worker>
-    | ExitHandler<Worker>
+    handler: EventHandler<Worker>
   ) => void
   /**
    * Gets task function worker usage statistics.