refactor: cleanup get composite schedule code
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
index 044938d432734f9762f4ffe7e4bb7244e3f74fd5..5824d8adfa21fc44e3c59d22d9c3fd94461e496e 100644 (file)
@@ -1,66 +1,59 @@
-import { DynamicThreadPool, PoolOptions } from 'poolifier';
+import { DynamicThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier';
 
-import Utils from '../utils/Utils';
-import { Worker } from 'worker_threads';
-import WorkerAbstract from './WorkerAbstract';
-import { WorkerData } from '../types/Worker';
-import { WorkerUtils } from './WorkerUtils';
+import { WorkerAbstract } from './WorkerAbstract';
+import type { WorkerData, WorkerOptions } from './WorkerTypes';
+import { sleep } from './WorkerUtils';
 
-export default class WorkerDynamicPool<T> extends WorkerAbstract {
-  private pool: DynamicThreadPool<WorkerData>;
+export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: DynamicThreadPool<WorkerData>;
 
   /**
-   * Create a new `WorkerDynamicPool`.
+   * Creates a new `WorkerDynamicPool`.
    *
-   * @param workerScript
-   * @param min
-   * @param max
-   * @param workerStartDelay
-   * @param opts
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
-    super(workerScript, workerStartDelay);
-    opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
-    this.pool = new DynamicThreadPool<WorkerData>(min, max, this.workerScript, opts);
+  constructor(workerScript: string, workerOptions: WorkerOptions) {
+    super(workerScript, workerOptions);
+    this.pool = new DynamicThreadPool<WorkerData>(
+      this.workerOptions.poolMinSize,
+      this.workerOptions.poolMaxSize,
+      this.workerScript,
+      this.workerOptions.poolOptions,
+    );
+  }
+
+  get info(): PoolInfo {
+    return this.pool.info;
   }
 
   get size(): number {
-    return this.pool.workers.length;
+    return this.pool.info.workerNodes;
   }
 
-  get maxElementsPerWorker(): number | null {
-    return null;
+  get maxElementsPerWorker(): number | undefined {
+    return undefined;
   }
 
-  /**
-   *
-   * @returns
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  get emitter(): PoolEmitter | undefined {
+    return this.pool?.emitter;
+  }
+
+  /** @inheritDoc */
   public async start(): Promise<void> {
     // This is intentional
   }
 
-  /**
-   *
-   * @returns
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/require-await
+  /** @inheritDoc */
   public async stop(): Promise<void> {
     return this.pool.destroy();
   }
 
-  /**
-   *
-   * @param elementData
-   * @returns
-   * @public
-   */
-  public async addElement(elementData: T): Promise<void> {
+  /** @inheritDoc */
+  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);
+    // Start element sequentially to optimize memory at startup
+    this.workerOptions.elementStartDelay! > 0 &&
+      (await sleep(this.workerOptions.elementStartDelay!));
   }
 }