Refine TS and linter configuration
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
index cf46cfa55f7662f671a5a24635c1f9566feb55f4..6fc91524173e614e1fb4ff1a7bae586c5e216b22 100644 (file)
@@ -1,25 +1,28 @@
-import { FixedThreadPool, PoolOptions } from 'poolifier';
+import { FixedThreadPool } from 'poolifier';
 
+import type { WorkerData, WorkerOptions } from '../types/Worker';
 import Utils from '../utils/Utils';
-import { Worker } from 'worker_threads';
 import WorkerAbstract from './WorkerAbstract';
-import { WorkerData } from '../types/Worker';
 import { WorkerUtils } from './WorkerUtils';
 
-export default class WorkerStaticPool<T> extends WorkerAbstract {
-  private pool: StaticPool;
+export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: FixedThreadPool<WorkerData>;
 
   /**
    * Create a new `WorkerStaticPool`.
    *
-   * @param {string} workerScript
-   * @param {number} numberOfThreads
-   * @param {number} startWorkerDelay
-   * @param {PoolOptions} opts
+   * @param workerScript
+   * @param workerOptions
    */
-  constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions<Worker>) {
-    super(workerScript, startWorkerDelay);
-    this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript, opts);
+  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+    super(workerScript, workerOptions);
+    this.workerOptions.poolOptions.exitHandler =
+      this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
+    this.pool = new FixedThreadPool(
+      this.workerOptions.poolMaxSize,
+      this.workerScript,
+      this.workerOptions.poolOptions
+    );
   }
 
   get size(): number {
@@ -32,15 +35,16 @@ export default class WorkerStaticPool<T> extends WorkerAbstract {
 
   /**
    *
-   * @returns {Promise<void>}
+   * @returns
    * @public
    */
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
-  public async start(): Promise<void> { }
+  public async start(): Promise<void> {
+    // This is intentional
+  }
 
   /**
    *
-   * @returns {Promise<void>}
+   * @returns
    * @public
    */
   public async stop(): Promise<void> {
@@ -49,29 +53,14 @@ export default class WorkerStaticPool<T> extends WorkerAbstract {
 
   /**
    *
-   * @param {T} elementData
-   * @returns {Promise<void>}
+   * @param elementData
+   * @returns
    * @public
    */
-  public async addElement(elementData: T): Promise<void> {
+  public async addElement(elementData: WorkerData): Promise<void> {
     await this.pool.execute(elementData);
-    // Start worker sequentially to optimize memory at startup
-    await Utils.sleep(this.workerStartDelay);
-  }
-}
-
-class StaticPool extends FixedThreadPool<WorkerData> {
-  private static instance: StaticPool;
-
-  private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>) {
-    super(numberOfThreads, workerScript, opts);
-  }
-
-  public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>): StaticPool {
-    if (!StaticPool.instance) {
-      opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
-      StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts);
-    }
-    return StaticPool.instance;
+    // Start element sequentially to optimize memory at startup
+    this.workerOptions.elementStartDelay > 0 &&
+      (await Utils.sleep(this.workerOptions.elementStartDelay));
   }
 }