Storage: use worker threads message passing to store performance records from
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerFactory.ts
index 2cda0ca2420f3ad4ae49fcfa64dcc55d7b7a261d..af2c53f9b3d5c86817a4204bb806fe53a3917f50 100644 (file)
@@ -8,25 +8,34 @@ import WorkerStaticPool from './WorkerStaticPool';
 import { isMainThread } from 'worker_threads';
 
 export default class WorkerFactory {
-  public static getWorkerImplementation<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract {
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  private constructor() {
+    // This is intentional
+  }
+
+  public static getWorkerImplementation<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions,
+      messageListenerCallback: (message: any) => void = () => { /* This is intentional */ }): WorkerAbstract | null {
     if (!isMainThread) {
       throw new Error('Trying to get a worker implementation outside the main thread');
     }
     options = options ?? {} as WorkerOptions;
     options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY;
+    let workerImplementation: WorkerAbstract = null;
     switch (workerProcessType) {
       case WorkerProcessType.WORKER_SET:
-        options.elementsPerWorker = options.elementsPerWorker ?? 1;
-        return new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
+        options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+        workerImplementation = new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay, messageListenerCallback);
+        break;
       case WorkerProcessType.STATIC_POOL:
-        options.poolMaxSize = options.poolMaxSize ?? 16;
-        return new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
+        options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+        workerImplementation = new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions, messageListenerCallback);
+        break;
       case WorkerProcessType.DYNAMIC_POOL:
-        options.poolMinSize = options.poolMinSize ?? 4;
-        options.poolMaxSize = options.poolMaxSize ?? 16;
-        return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
-      default:
-        return null;
+        options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+        options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+        workerImplementation = new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions, messageListenerCallback);
+        break;
     }
+    return workerImplementation;
   }
 }