X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=35fe82eb4c73a017d01316e265ddc7348fc72025;hb=c36e3cf0312f553b4b8c2b716da0d55cc87450cf;hp=b5cf327c12d57ab7935b27aefb4a26cf2b79ca05;hpb=8df3f0a9d9fb9796c093a0e040b13dca8aaa3234;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index b5cf327c..35fe82eb 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,41 +1,56 @@ -import { WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { isMainThread } from 'node:worker_threads'; -import Constants from '../utils/Constants'; -import WorkerAbstract from './WorkerAbstract'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; -import { isMainThread } from 'worker_threads'; +import type { ThreadPoolOptions } from 'poolifier'; -export default class WorkerFactory { - private static workerImplementation: WorkerAbstract | null; +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'; - private constructor() {} +export class WorkerFactory { + private constructor() { + // This is intentional + } - public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions, forceInstantiation = false): 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'); } - if (!WorkerFactory.workerImplementation || forceInstantiation) { - options = options ?? {} as WorkerOptions; - options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY; - WorkerFactory.workerImplementation = null; - switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: - options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; - WorkerFactory.workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, options.startDelay); - break; - case WorkerProcessType.STATIC_POOL: - options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - WorkerFactory.workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions); - 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; - WorkerFactory.workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions); - break; - } + 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 ThreadPoolOptions); + let workerImplementation: WorkerAbstract | null = null; + switch (workerProcessType) { + case WorkerProcessType.workerSet: + workerOptions.elementsPerWorker = + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; + case WorkerProcessType.staticPool: + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; + case WorkerProcessType.dynamicPool: + workerOptions.poolMinSize = + workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; + workerOptions.poolMaxSize = + 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 WorkerFactory.workerImplementation; + return workerImplementation; } }