Linter fixes
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
index 7c662d9dead4aa46e519b2519d1e560c774615b8..0fccdb812d7ed4c2a40b28596a3cfc883ed606e3 100644 (file)
@@ -1,21 +1,42 @@
-import Constants from '../utils/Constants';
-import { WorkerData } from '../types/Worker';
+import fs from 'fs';
+
+import WorkerConstants from './WorkerConstants';
+import type { WorkerData, WorkerOptions } from '../types/Worker';
 
 export default abstract class WorkerAbstract<T extends WorkerData> {
   protected readonly workerScript: string;
-  protected readonly workerStartDelay: number;
+  protected readonly workerOptions: WorkerOptions;
   public abstract readonly size: number;
   public abstract readonly maxElementsPerWorker: number | null;
 
   /**
    * `WorkerAbstract` constructor.
    *
-   * @param workerScript
-   * @param workerStartDelay
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, workerStartDelay: number = Constants.WORKER_START_DELAY) {
+  constructor(
+    workerScript: string,
+    workerOptions: WorkerOptions = {
+      workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
+      elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
+      poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
+      poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
+      elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
+      poolOptions: {},
+      messageHandler: () => {
+        /* This is intentional */
+      },
+    }
+  ) {
+    if (!workerScript) {
+      throw new Error('Worker script is not defined');
+    }
+    if (!fs.existsSync(workerScript)) {
+      throw new Error('Worker script file does not exist');
+    }
     this.workerScript = workerScript;
-    this.workerStartDelay = workerStartDelay;
+    this.workerOptions = workerOptions;
   }
 
   public abstract start(): Promise<void>;