X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=35fe82eb4c73a017d01316e265ddc7348fc72025;hb=c36e3cf0312f553b4b8c2b716da0d55cc87450cf;hp=b297f42b231715643ef14761ca1c45a93295a5cf;hpb=35639ced7a1a8e9cfb7b0db675fc2757ea902d04;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index b297f42b..35fe82eb 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 { isMainThread } from 'node:worker_threads'; -import { PoolOptions } from 'poolifier'; -import type WorkerAbstract from './WorkerAbstract'; -import WorkerConstants from './WorkerConstants'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; +import type { ThreadPoolOptions } 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,30 +20,27 @@ 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 ?? 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.poolOptions = workerOptions?.poolOptions ?? ({} as ThreadPoolOptions); + let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: + case WorkerProcessType.workerSet: workerOptions.elementsPerWorker = workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: + case WorkerProcessType.staticPool: workerOptions.poolMaxSize = workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; - case WorkerProcessType.DYNAMIC_POOL: + case WorkerProcessType.dynamicPool: workerOptions.poolMinSize = workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; workerOptions.poolMaxSize =