Use singleton design pattern directly in the worker object factory
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerFactory.ts
index 3784e7ab0c572f36a30de55d1181309fa4e2191e..b5cf327c12d57ab7935b27aefb4a26cf2b79ca05 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 | null {
+  private static workerImplementation: WorkerAbstract | null;
+
+  private constructor() {}
+
+  public static getWorkerImplementation<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions, forceInstantiation = false): 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;
-    switch (workerProcessType) {
-      case WorkerProcessType.WORKER_SET:
-        options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
-        return new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
-      case WorkerProcessType.STATIC_POOL:
-        options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
-        return new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
-      case WorkerProcessType.DYNAMIC_POOL:
-        options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
-        options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
-        return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
-      default:
-        return null;
+    if (!WorkerFactory.workerImplementation || forceInstantiation) {
+      options = options ?? {} as WorkerOptions;
+      options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY;
+      WorkerFactory.workerImplementation = null;
+      switch (workerProcessType) {
+        case WorkerProcessType.WORKER_SET:
+          options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+          WorkerFactory.workerImplementation = new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
+          break;
+        case WorkerProcessType.STATIC_POOL:
+          options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+          WorkerFactory.workerImplementation = new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
+          break;
+        case WorkerProcessType.DYNAMIC_POOL:
+          options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+          options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+          WorkerFactory.workerImplementation = new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
+          break;
+      }
     }
+    return WorkerFactory.workerImplementation;
   }
 }