feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
index 2043297318c38e8d298a76c9d660b4d215bc8d71..5217d7fe16a6772a81b946969702fe1189f0d5dc 100644 (file)
@@ -6,8 +6,11 @@ import { WorkerAbstract } from './WorkerAbstract.js'
 import type { WorkerData, WorkerOptions } from './WorkerTypes.js'
 import { randomizeDelay, sleep } from './WorkerUtils.js'
 
-export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
-  private readonly pool: DynamicThreadPool<WorkerData>
+export class WorkerDynamicPool<D extends WorkerData, R extends WorkerData> extends WorkerAbstract<
+D,
+R
+> {
+  private readonly pool: DynamicThreadPool<D, R>
 
   /**
    * Creates a new `WorkerDynamicPool`.
@@ -17,7 +20,7 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
    */
   constructor (workerScript: string, workerOptions: WorkerOptions) {
     super(workerScript, workerOptions)
-    this.pool = new DynamicThreadPool<WorkerData>(
+    this.pool = new DynamicThreadPool<D, R>(
       this.workerOptions.poolMinSize,
       this.workerOptions.poolMaxSize,
       this.workerScript,
@@ -52,12 +55,13 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
   }
 
   /** @inheritDoc */
-  public async addElement (elementData: WorkerData): Promise<void> {
-    await this.pool.execute(elementData)
+  public async addElement (elementData: D): Promise<R> {
+    const response = await this.pool.execute(elementData)
     // Start element sequentially to optimize memory at startup
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     this.workerOptions.elementAddDelay! > 0 &&
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       (await sleep(randomizeDelay(this.workerOptions.elementAddDelay!)))
+    return response
   }
 }