X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=59fbf3a44fe46409a0d84c5a11ed0abcf7fce7f1;hb=d632062f209c41719300e32cf0e4c06e151ecc4b;hp=13880b2f81dd8f16c99a97f26949bc44c51f8be6;hpb=6013bc53ce820bacf728a4d85d875c3317ff2442;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 13880b2f..59fbf3a4 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,41 @@ -import Configuration from '../utils/Configuration'; -import WorkerPool from './WorkerPool'; -import WorkerSet from './WorkerSet'; -import Wrk from './Worker'; +import { isMainThread } from 'node:worker_threads'; -export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - if (Configuration.useWorkerPool()) { - return new WorkerPool(workerScript); +import type { WorkerAbstract } from './WorkerAbstract'; +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants'; +import { WorkerDynamicPool } from './WorkerDynamicPool'; +import { WorkerSet } from './WorkerSet'; +import { WorkerStaticPool } from './WorkerStaticPool'; +import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; + +export class WorkerFactory { + private constructor() { + // This is intentional + } + + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions, + ): WorkerAbstract | null { + if (!isMainThread) { + throw new Error('Cannot get a worker implementation outside the main thread'); + } + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract | null = null; + switch (workerProcessType) { + case WorkerProcessType.workerSet: + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; + case WorkerProcessType.staticPool: + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; + case WorkerProcessType.dynamicPool: + workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); + break; + default: + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); + return workerImplementation; } }