X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=8b979d06ca2d76f8e4d0220132b03dafd8a6bf84;hb=1f5df42ad17d09d3a1f53f6618eba325a403d7ad;hp=af2c53f9b3d5c86817a4204bb806fe53a3917f50;hpb=81797102d5214fea2fc58eff2666fe8b8d9a5a11;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index af2c53f9..8b979d06 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,40 +1,43 @@ +import { Worker, isMainThread } from 'worker_threads'; import { WorkerOptions, WorkerProcessType } from '../types/Worker'; import Constants from '../utils/Constants'; +import { PoolOptions } from 'poolifier'; import WorkerAbstract from './WorkerAbstract'; import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; -import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - // eslint-disable-next-line @typescript-eslint/no-empty-function private constructor() { // This is intentional } - public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions, - messageListenerCallback: (message: any) => void = () => { /* This is intentional */ }): WorkerAbstract | null { + public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract | null { if (!isMainThread) { throw new Error('Trying to get a worker implementation outside the main thread'); } options = options ?? {} as WorkerOptions; - options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY; + options.startDelay = options?.startDelay ?? Constants.WORKER_START_DELAY; + options.poolOptions = options?.poolOptions ?? {} as PoolOptions; + options?.messageHandler && (options.poolOptions.messageHandler = options.messageHandler); let workerImplementation: WorkerAbstract = null; switch (workerProcessType) { case WorkerProcessType.WORKER_SET: options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; - workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, options.startDelay, messageListenerCallback); + workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, options.startDelay, options); break; case WorkerProcessType.STATIC_POOL: options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions, messageListenerCallback); + 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; - workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions, messageListenerCallback); + workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions); break; + default: + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } return workerImplementation; }