X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=d248d3bab765b5aabad6e9d5e2afb5dc773c5df0;hb=42b8cf5cdca8eaab1e7442f7c92c2a5ed97434f6;hp=067188d846401d13c3a76c1bdff6236442de6859;hpb=144cabe085e5e1f3821aecb2ef8f388a1030b92b;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 067188d8..d248d3ba 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 './Wrk'; +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 { WorkerFixedPool } from './WorkerFixedPool'; +import { WorkerSet } from './WorkerSet'; +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.fixedPool: + workerImplementation = new WorkerFixedPool(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; } }