X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=d248d3bab765b5aabad6e9d5e2afb5dc773c5df0;hb=42b8cf5cdca8eaab1e7442f7c92c2a5ed97434f6;hp=01adb96e795eb1e93e74e94b5d8b9bb4c56f9489;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 01adb96e..d248d3ba 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,15 +1,13 @@ -import { Worker, isMainThread } from 'worker_threads'; +import { isMainThread } from 'node:worker_threads'; -import { PoolOptions } from 'poolifier'; +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'; -import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; -import type WorkerAbstract from './WorkerAbstract'; -import WorkerConstants from './WorkerConstants'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; - -export default class WorkerFactory { +export class WorkerFactory { private constructor() { // This is intentional } @@ -17,37 +15,21 @@ export default 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 && - // eslint-disable-next-line @typescript-eslint/no-misused-promises - (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); - let workerImplementation: WorkerAbstract = null; + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: - workerOptions.elementsPerWorker = - workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + case WorkerProcessType.workerSet: workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; - workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + case WorkerProcessType.fixedPool: + workerImplementation = new WorkerFixedPool(workerScript, workerOptions); break; - case WorkerProcessType.DYNAMIC_POOL: - workerOptions.poolMinSize = - workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + case WorkerProcessType.dynamicPool: workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); break; default: