refactor: cleanup default worker options handling
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerSet.ts
index 61118a0d0a274d17871a71ec8e2ebec3efa2c2a9..ce25e7e3a6e4ec3428072a05fc6e13d90e14c0fd 100644 (file)
@@ -3,6 +3,8 @@
 import { EventEmitter } from 'node:events';
 import { SHARE_ENV, Worker } from 'node:worker_threads';
 
+import type { ThreadPoolOptions } from 'poolifier';
+
 import { WorkerAbstract } from './WorkerAbstract';
 import { WorkerConstants } from './WorkerConstants';
 import {
@@ -15,6 +17,11 @@ import {
 } from './WorkerTypes';
 import { sleep } from './WorkerUtils';
 
+const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = {
+  enableEvents: true,
+  restartWorkerOnError: true,
+};
+
 export class WorkerSet extends WorkerAbstract<WorkerData> {
   public readonly emitter!: EventEmitter;
   private readonly workerSet: Set<WorkerSetElement>;
@@ -25,13 +32,10 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
    * @param workerScript -
    * @param workerOptions -
    */
-  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+  constructor(workerScript: string, workerOptions: WorkerOptions) {
     super(workerScript, workerOptions);
     this.workerOptions.poolOptions = {
-      ...{
-        enableEvents: true,
-        restartWorkerOnError: true,
-      },
+      ...DEFAULT_POOL_OPTIONS,
       ...this.workerOptions.poolOptions,
     };
     this.workerSet = new Set<WorkerSetElement>();