feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerFactory.ts
index fcfe4f80677820266f8438aab242907d281de1ff..73bcd56dc62467c7bb942157d88554014b8240af 100644 (file)
@@ -1,41 +1,39 @@
-import { WorkerOptions, WorkerProcessType } from '../types/Worker';
+import { isMainThread } from 'node:worker_threads'
 
-import Utils from '../utils/Utils';
-import WorkerDynamicPool from './WorkerDynamicPool';
-import WorkerSet from './WorkerSet';
-import WorkerStaticPool from './WorkerStaticPool';
-import Wrk from './Wrk';
-import { isMainThread } from 'worker_threads';
+import { mergeDeepRight } from 'rambda'
 
-export default class WorkerFactory {
-  public static getWorkerImpl<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk {
+import type { WorkerAbstract } from './WorkerAbstract.js'
+import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants.js'
+import { WorkerDynamicPool } from './WorkerDynamicPool.js'
+import { WorkerFixedPool } from './WorkerFixedPool.js'
+import { WorkerSet } from './WorkerSet.js'
+import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes.js'
+
+// eslint-disable-next-line @typescript-eslint/no-extraneous-class
+export class WorkerFactory {
+  private constructor () {
+    // This is intentional
+  }
+
+  public static getWorkerImplementation<D extends WorkerData, R extends WorkerData>(
+    workerScript: string,
+    workerProcessType: WorkerProcessType,
+    workerOptions?: WorkerOptions
+  ): WorkerAbstract<D, R> {
     if (!isMainThread) {
-      throw new Error('Trying to get a worker implementation outside the main thread');
-    }
-    if (Utils.isUndefined(options)) {
-      options = {} as WorkerOptions;
+      throw new Error('Cannot get a worker implementation outside the main thread')
     }
+    workerOptions = mergeDeepRight<WorkerOptions>(DEFAULT_WORKER_OPTIONS, workerOptions ?? {})
     switch (workerProcessType) {
-      case WorkerProcessType.WORKER_SET:
-        if (Utils.isUndefined(options.elementsPerWorker)) {
-          options.elementsPerWorker = 1;
-        }
-        return new WorkerSet<T>(workerScript, options.elementsPerWorker);
-      case WorkerProcessType.STATIC_POOL:
-        if (Utils.isUndefined(options.poolMaxSize)) {
-          options.elementsPerWorker = 16;
-        }
-        return new WorkerStaticPool<T>(workerScript, options.poolMaxSize);
-      case WorkerProcessType.DYNAMIC_POOL:
-        if (Utils.isUndefined(options.poolMinSize)) {
-          options.elementsPerWorker = 4;
-        }
-        if (Utils.isUndefined(options.poolMaxSize)) {
-          options.elementsPerWorker = 16;
-        }
-        return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize);
+      case WorkerProcessType.workerSet:
+        return new WorkerSet<D, R>(workerScript, workerOptions)
+      case WorkerProcessType.fixedPool:
+        return new WorkerFixedPool<D, R>(workerScript, workerOptions)
+      case WorkerProcessType.dynamicPool:
+        return new WorkerDynamicPool<D, R>(workerScript, workerOptions)
       default:
-        return null;
+        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
+        throw new Error(`Worker implementation type '${workerProcessType}' not found`)
     }
   }
 }