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,
/** @inheritdoc */
public registerWorkerEventHandler (
event: string,
- handler:
- | OnlineHandler<Worker>
- | MessageHandler<Worker>
- | ErrorHandler<Worker>
- | ExitHandler<Worker>
+ handler: EventHandler<Worker>
): void {
this.worker.on(event, handler)
}
/** @inheritdoc */
public registerOnceWorkerEventHandler (
event: string,
- handler:
- | OnlineHandler<Worker>
- | MessageHandler<Worker>
- | ErrorHandler<Worker>
- | ExitHandler<Worker>
+ handler: EventHandler<Worker>
): void {
this.worker.once(event, handler)
}
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.
*
* @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.
*/
readonly registerWorkerEventHandler: (
event: string,
- handler:
- | OnlineHandler<Worker>
- | MessageHandler<Worker>
- | ErrorHandler<Worker>
- | ExitHandler<Worker>
+ handler: EventHandler<Worker>
) => void
/**
* Registers once a worker event handler.
*/
readonly registerOnceWorkerEventHandler: (
event: string,
- handler:
- | OnlineHandler<Worker>
- | MessageHandler<Worker>
- | ErrorHandler<Worker>
- | ExitHandler<Worker>
+ handler: EventHandler<Worker>
) => void
/**
* Gets task function worker usage statistics.