X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=73bcd56dc62467c7bb942157d88554014b8240af;hb=8fb8f42ace8b9295865adebaeb7845e064c05e71;hp=ae27d1dfd56fa5e0fd14d4daba074d35afb32c0c;hpb=4a3807d16c54137840a60af41877f3b21de12950;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index ae27d1df..73bcd56d 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,50 +1,39 @@ -import { isMainThread } from 'node:worker_threads'; +import { isMainThread } from 'node:worker_threads' -import type { WorkerAbstract } from './WorkerAbstract'; -import { WorkerConstants } from './WorkerConstants'; -import { WorkerDynamicPool } from './WorkerDynamicPool'; -import { WorkerSet } from './WorkerSet'; -import { WorkerStaticPool } from './WorkerStaticPool'; -import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; +import { mergeDeepRight } from 'rambda' -const DEFAULT_WORKER_OPTIONS: WorkerOptions = { - workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, - elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, - poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, - poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, - elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, - poolOptions: {}, -}; +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() { + private constructor () { // This is intentional } - public static getWorkerImplementation( + public static getWorkerImplementation( workerScript: string, workerProcessType: WorkerProcessType, - workerOptions?: WorkerOptions, - ): WorkerAbstract | null { + workerOptions?: WorkerOptions + ): WorkerAbstract { if (!isMainThread) { - throw new Error('Cannot get a worker implementation outside the main thread'); + throw new Error('Cannot get a worker implementation outside the main thread') } - workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; - let workerImplementation: WorkerAbstract | null = null; + workerOptions = mergeDeepRight(DEFAULT_WORKER_OPTIONS, workerOptions ?? {}) switch (workerProcessType) { case WorkerProcessType.workerSet: - workerImplementation = new WorkerSet(workerScript, workerOptions); - break; - case WorkerProcessType.staticPool: - workerImplementation = new WorkerStaticPool(workerScript, workerOptions); - break; + return new WorkerSet(workerScript, workerOptions) + case WorkerProcessType.fixedPool: + return new WorkerFixedPool(workerScript, workerOptions) case WorkerProcessType.dynamicPool: - workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); - break; + return new WorkerDynamicPool(workerScript, workerOptions) default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - throw new Error(`Worker implementation type '${workerProcessType}' not found`); + throw new Error(`Worker implementation type '${workerProcessType}' not found`) } - return workerImplementation; } }