From: Jérôme Benoit Date: Thu, 8 Jun 2023 10:37:14 +0000 (+0200) Subject: refactor: consolidate worker events enum X-Git-Tag: v1.2.17~38 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=810f4caf2bf0b12d5c429d1d21af62b1fe67f965;p=e-mobility-charging-stations-simulator.git refactor: consolidate worker events enum Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 68160958..9efc88c1 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -12,6 +12,7 @@ import { WorkerMessageEvents, type WorkerOptions, type WorkerSetElement, + WorkerSetEvents, } from './WorkerTypes'; import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; @@ -95,7 +96,7 @@ export class WorkerSet extends WorkerAbstract { ); worker.on('error', defaultErrorHandler.bind(this) as (err: Error) => void); worker.on('error', (error) => { - this.emitter.emit('error', error); + this.emitter.emit(WorkerSetEvents.error, error); this.addWorkerSetElement(); }); worker.on('exit', defaultExitHandler.bind(this) as (exitCode: number) => void); diff --git a/src/worker/WorkerTypes.ts b/src/worker/WorkerTypes.ts index 6f780bce..fa1c82b0 100644 --- a/src/worker/WorkerTypes.ts +++ b/src/worker/WorkerTypes.ts @@ -1,6 +1,6 @@ import type { Worker } from 'node:worker_threads'; -import type { PoolOptions } from 'poolifier'; +import { type PoolEvent, PoolEvents, type PoolOptions } from 'poolifier'; export enum WorkerProcessType { workerSet = 'workerSet', @@ -14,6 +14,16 @@ export type SetInfo = { elementsPerWorker: number; }; +export enum WorkerSetEvents { + error = 'error', +} + +export const WorkerEvents = { + ...PoolEvents, + ...WorkerSetEvents, +} as const; +export type WorkerEvents = PoolEvent | WorkerSetEvents; + export type MessageHandler = (this: T, message: unknown) => void; export type WorkerOptions = { diff --git a/src/worker/index.ts b/src/worker/index.ts index 2ad6671b..5c8a2534 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -4,6 +4,7 @@ export { WorkerFactory } from './WorkerFactory'; export { type MessageHandler, type WorkerData, + type WorkerEvents, type WorkerMessage, WorkerMessageEvents, WorkerProcessType,