X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fworker%2FWorkerFactory.ts;h=668d447a7f4c23a1574bbb2c8ffc50260eb910f9;hb=e0b0ee2176a4dc5a8914a3fb094ec16b4966a740;hp=1bc6786d0cc991993e7809f52022a4a5a212dedd;hpb=efa0d4320f8c6941d0b257318d0ca752b3c964fc;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 1bc6786d..668d447a 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,41 +1,58 @@ -import { WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { type Worker, isMainThread } from 'worker_threads'; -import Utils from '../utils/Utils'; +import type { PoolOptions } from 'poolifier'; + +import { type WorkerData, type WorkerOptions, WorkerProcessType } from '../types/Worker'; +import type WorkerAbstract from './WorkerAbstract'; +import WorkerConstants from './WorkerConstants'; import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; -import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { + private constructor() { + // This is intentional + } + + 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'); - } - if (Utils.isUndefined(options)) { - options = {} as WorkerOptions; + 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 && + (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); + let workerImplementation: WorkerAbstract = null; switch (workerProcessType) { case WorkerProcessType.WORKER_SET: - if (Utils.isUndefined(options.elementsPerWorker)) { - options.elementsPerWorker = 1; - } - return new WorkerSet(workerScript, options.elementsPerWorker); + workerOptions.elementsPerWorker = + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; case WorkerProcessType.STATIC_POOL: - if (Utils.isUndefined(options.poolMaxSize)) { - options.poolMaxSize = 16; - } - return new WorkerStaticPool(workerScript, options.poolMaxSize); + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; case WorkerProcessType.DYNAMIC_POOL: - if (Utils.isUndefined(options.poolMinSize)) { - options.poolMinSize = 4; - } - if (Utils.isUndefined(options.poolMaxSize)) { - options.poolMaxSize = 16; - } - return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize); + 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: - return null; + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } + return workerImplementation; } }