X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=833c939f1767e832b021f152a5d2725caf221f72;hb=17ba3be0bb5c0bcb7fff2cefa877af975c0d043a;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..833c939f 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,41 +1,42 @@ -import { WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { isMainThread } from 'node:worker_threads' -import Utils from '../utils/Utils'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; -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 { - public static getWorkerImpl(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { +// eslint-disable-next-line @typescript-eslint/no-extraneous-class +export class WorkerFactory { + private constructor () { + // This is intentional + } + + 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'); - } - if (Utils.isUndefined(options)) { - options = {} as WorkerOptions; + throw new Error('Cannot get a worker implementation outside the main thread') } + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions } + let workerImplementation: WorkerAbstract switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: - if (Utils.isUndefined(options.elementsPerWorker)) { - options.elementsPerWorker = 1; - } - return new WorkerSet(workerScript, options.elementsPerWorker); - case WorkerProcessType.STATIC_POOL: - if (Utils.isUndefined(options.poolMaxSize)) { - options.poolMaxSize = 16; - } - return new WorkerStaticPool(workerScript, options.poolMaxSize); - 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); + 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: - return null; + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`) } + return workerImplementation } }