X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=0a532681cf9a12d41ffb0c73b13b3fd71c8643e7;hb=aa49f5ec9d64b41b50509b41abde34aa0affe8c1;hp=1c4b81c8ad6ab5d7c0e35a9b89e46d39b4f19c7e;hpb=e7aeea18e189dd087c8f951cf77a253e2818ae90;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 1c4b81c8..0a532681 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,14 +1,15 @@ -import { Worker, isMainThread } from 'worker_threads'; -import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { type Worker, 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 { PoolOptions } from 'poolifier'; -export default class WorkerFactory { +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'; + +export class WorkerFactory { private constructor() { // This is intentional } @@ -19,36 +20,37 @@ export default class WorkerFactory { 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'); } workerOptions = workerOptions ?? ({} as WorkerOptions); workerOptions.workerStartDelay = - workerOptions?.workerStartDelay ?? Constants.WORKER_START_DELAY; + workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; workerOptions.elementStartDelay = - workerOptions?.elementStartDelay ?? Constants.ELEMENT_START_DELAY; + workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions); workerOptions?.messageHandler && (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); - let workerImplementation: WorkerAbstract = null; + let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: + case WorkerProcessType.workerSet: workerOptions.elementsPerWorker = - workerOptions?.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: + case WorkerProcessType.staticPool: workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; - case WorkerProcessType.DYNAMIC_POOL: + case WorkerProcessType.dynamicPool: workerOptions.poolMinSize = - workerOptions?.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; + workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; 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;