X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=d248d3bab765b5aabad6e9d5e2afb5dc773c5df0;hb=42b8cf5cdca8eaab1e7442f7c92c2a5ed97434f6;hp=d71e4227841d5072778c2a7a8a71d256e12ae1e3;hpb=8434025b7338b3439180ed7c2bed888fc42e04d3;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index d71e4227..d248d3ba 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,37 +1,41 @@ -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 type { WorkerAbstract } from './WorkerAbstract'; +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants'; +import { WorkerDynamicPool } from './WorkerDynamicPool'; +import { WorkerFixedPool } from './WorkerFixedPool'; +import { WorkerSet } from './WorkerSet'; +import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; -export default class WorkerFactory { - public static getWorkerImpl(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { - if (Utils.isUndefined(options)) { - options = {} as WorkerOptions; +export class WorkerFactory { + private constructor() { + // This is intentional + } + + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions, + ): WorkerAbstract | null { + if (!isMainThread) { + throw new Error('Cannot get a worker implementation outside the main thread'); } + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract | null = null; 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.elementsPerWorker = 16; - } - return new WorkerStaticPool(workerScript, options.poolMaxSize); - case WorkerProcessType.DYNAMIC_POOL: - if (Utils.isUndefined(options.poolMinSize)) { - options.elementsPerWorker = 4; - } - if (Utils.isUndefined(options.poolMaxSize)) { - options.elementsPerWorker = 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; } }