X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=608e879859b9662cb093241aec71fccd7d0a9ea1;hb=de7b9e0583275225509c099f75a2801371da54d0;hp=debd3f6a21342797ac15a6ae23eb5f0e664edce5;hpb=d070d967782492d71c5716d2560177531f826f53;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index debd3f6a..608e8798 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,43 +1,39 @@ -import { Worker, isMainThread } from 'worker_threads'; -import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { isMainThread } from 'node:worker_threads'; -import Constants from '../utils/Constants'; -import { PoolOptions } from 'poolifier'; -import type WorkerAbstract from './WorkerAbstract'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; +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 default class WorkerFactory { +export class WorkerFactory { private constructor() { // This is intentional } - public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, workerOptions?: WorkerOptions): WorkerAbstract | null { + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions, + ): WorkerAbstract | undefined { if (!isMainThread) { - throw new Error('Trying to get a worker implementation outside the main thread'); + throw new Error('Cannot get a worker implementation outside the main thread'); } - workerOptions = workerOptions ?? {} as WorkerOptions; - workerOptions.workerStartDelay = workerOptions?.workerStartDelay ?? Constants.WORKER_START_DELAY; - workerOptions.elementStartDelay = workerOptions?.elementStartDelay ?? Constants.ELEMENT_START_DELAY; - workerOptions.poolOptions = workerOptions?.poolOptions ?? {} as PoolOptions; - workerOptions?.messageHandler && (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); - let workerImplementation: WorkerAbstract = null; + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: - workerOptions.elementsPerWorker = workerOptions?.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; + case WorkerProcessType.workerSet: workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: - workerOptions.poolMaxSize = workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_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 ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; - workerOptions.poolMaxSize = workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; + 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 workerImplementation;