X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=59fbf3a44fe46409a0d84c5a11ed0abcf7fce7f1;hb=20558952fb18725ad134a5148a873a30fcced31d;hp=3a2d3fa79b153c96424d97ad3820374ee2772042;hpb=4d7227e61934a6b082a4d89268c454f7ee3605e1;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 3a2d3fa7..59fbf3a4 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 { WorkerSet } from './WorkerSet'; +import { WorkerStaticPool } from './WorkerStaticPool'; +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, options?: WorkerOptions): WorkerAbstract | null { + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions, + ): WorkerAbstract | null { 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'); } - options = options ?? {} as WorkerOptions; - options.workerStartDelay = options?.workerStartDelay ?? Constants.WORKER_START_DELAY; - options.elementStartDelay = options?.elementStartDelay ?? Constants.ELEMENT_START_DELAY; - options.poolOptions = options?.poolOptions ?? {} as PoolOptions; - options?.messageHandler && (options.poolOptions.messageHandler = options.messageHandler); - let workerImplementation: WorkerAbstract = null; + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: - options.elementsPerWorker = options?.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; - workerImplementation = new WorkerSet(workerScript, options); + case WorkerProcessType.workerSet: + workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: - options.poolMaxSize = options?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - workerImplementation = new WorkerStaticPool(workerScript, options); + case WorkerProcessType.staticPool: + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; - case WorkerProcessType.DYNAMIC_POOL: - options.poolMinSize = options?.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; - options.poolMaxSize = options?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - workerImplementation = new WorkerDynamicPool(workerScript, options); + 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;