X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fworker%2FWorkerFactory.ts;h=833c939f1767e832b021f152a5d2725caf221f72;hb=a807045be19c1ed4996a44d8c2c8774e926dc6dc;hp=fdfd0cb4a799b844b4bfd467bb7f6bc3c430e9b7;hpb=fb226c9b812c3db625996c5fe29c667be15c388c;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index fdfd0cb4..833c939f 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,43 +1,42 @@ -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 { WorkerAbstract } from './WorkerAbstract.js' +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants.js' +import { WorkerDynamicPool } from './WorkerDynamicPool.js' +import { WorkerFixedPool } from './WorkerFixedPool.js' +import { WorkerSet } from './WorkerSet.js' +import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes.js' -export default class WorkerFactory { - // eslint-disable-next-line @typescript-eslint/no-empty-function - private constructor() { +// eslint-disable-next-line @typescript-eslint/no-extraneous-class +export class WorkerFactory { + 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, + workerOptions?: WorkerOptions + ): WorkerAbstract | undefined { 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') } - options = options ?? {} as WorkerOptions; - options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY; - let workerImplementation: WorkerAbstract = null; + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions } + let workerImplementation: WorkerAbstract 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); - 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); - 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); - break; + case WorkerProcessType.workerSet: + workerImplementation = new WorkerSet(workerScript, workerOptions) + break + case WorkerProcessType.fixedPool: + workerImplementation = new WorkerFixedPool(workerScript, workerOptions) + break + case WorkerProcessType.dynamicPool: + workerImplementation = new WorkerDynamicPool(workerScript, workerOptions) + break default: - throw new Error(`Worker implementation type '${workerProcessType}' not found`); + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`) } - return workerImplementation; + return workerImplementation } }