X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=59fbf3a44fe46409a0d84c5a11ed0abcf7fce7f1;hb=20558952fb18725ad134a5148a873a30fcced31d;hp=0a532681cf9a12d41ffb0c73b13b3fd71c8643e7;hpb=abe9e9dd3d72acc384fa2d3acd434f0053d8c61e;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 0a532681..59fbf3a4 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,9 +1,7 @@ -import { type Worker, isMainThread } from 'node:worker_threads'; - -import type { PoolOptions } from 'poolifier'; +import { isMainThread } from 'node:worker_threads'; import type { WorkerAbstract } from './WorkerAbstract'; -import { WorkerConstants } from './WorkerConstants'; +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants'; import { WorkerDynamicPool } from './WorkerDynamicPool'; import { WorkerSet } from './WorkerSet'; import { WorkerStaticPool } from './WorkerStaticPool'; @@ -17,36 +15,21 @@ export class WorkerFactory { public static getWorkerImplementation( workerScript: string, workerProcessType: WorkerProcessType, - workerOptions?: WorkerOptions + workerOptions?: WorkerOptions, ): WorkerAbstract | null { if (!isMainThread) { throw new Error('Cannot get a worker implementation outside the main thread'); } - workerOptions = workerOptions ?? ({} as WorkerOptions); - workerOptions.workerStartDelay = - workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; - workerOptions.elementStartDelay = - workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions); - workerOptions?.messageHandler && - (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { case WorkerProcessType.workerSet: - workerOptions.elementsPerWorker = - workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; workerImplementation = new WorkerSet(workerScript, workerOptions); break; case WorkerProcessType.staticPool: - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; case WorkerProcessType.dynamicPool: - workerOptions.poolMinSize = - workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); break; default: