feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
index fa28597c55784d585fcec7231f0aea69e298bee1..f6713f8976d2cdda47d74f5951880f8d1fdfd48d 100644 (file)
@@ -1,24 +1,53 @@
-import Constants from '../utils/Constants';
-import { WorkerData } from '../types/Worker';
+import type { EventEmitterAsyncResource } from 'node:events'
+import { existsSync } from 'node:fs'
 
-export default abstract class WorkerAbstract {
-  protected readonly workerScript: string;
-  protected readonly workerStartDelay: number;
-  public abstract readonly size: number;
-  public abstract readonly maxElementsPerWorker: number | null;
+import type { PoolInfo } from 'poolifier'
+
+import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js'
+
+export abstract class WorkerAbstract<D extends WorkerData, R extends WorkerData> {
+  protected readonly workerScript: string
+  protected readonly workerOptions: WorkerOptions
+  public abstract readonly info: PoolInfo | SetInfo
+  public abstract readonly size: number
+  public abstract readonly maxElementsPerWorker: number | undefined
+  public abstract readonly emitter: EventEmitterAsyncResource | undefined
 
   /**
    * `WorkerAbstract` constructor.
    *
-   * @param workerScript
-   * @param workerStartDelay
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, workerStartDelay: number = Constants.WORKER_START_DELAY) {
-    this.workerScript = workerScript;
-    this.workerStartDelay = workerStartDelay;
+  constructor (workerScript: string | undefined, workerOptions: WorkerOptions) {
+    if (workerScript == null) {
+      throw new TypeError('Worker script is not defined')
+    }
+    if (typeof workerScript !== 'string') {
+      throw new TypeError('Worker script must be a string')
+    }
+    if (workerScript.trim().length === 0) {
+      throw new Error('Worker script is an empty string')
+    }
+    if (!existsSync(workerScript)) {
+      throw new Error('Worker script file does not exist')
+    }
+    this.workerScript = workerScript
+    this.workerOptions = workerOptions
   }
 
-  public abstract start(): Promise<void>;
-  public abstract stop(): Promise<void>;
-  public abstract addElement(elementData: WorkerData): Promise<void>;
+  /**
+   * Starts the worker pool/set.
+   */
+  public abstract start (): void | Promise<void>
+  /**
+   * Stops the worker pool/set.
+   */
+  public abstract stop (): Promise<void>
+  /**
+   * Adds a task element to the worker pool/set.
+   *
+   * @param elementData -
+   */
+  public abstract addElement (elementData: D): Promise<R>
 }