X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=242121fbb289b64f54b45de0aa510146a96ccffc;hb=863cef2221eeab3c92506cc02dec1abd87abaa36;hp=13880b2f81dd8f16c99a97f26949bc44c51f8be6;hpb=6013bc53ce820bacf728a4d85d875c3317ff2442;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 13880b2f..242121fb 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,33 @@ -import Configuration from '../utils/Configuration'; -import WorkerPool from './WorkerPool'; +import { WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import Utils from '../utils/Utils'; +import WorkerAbstract from './WorkerAbstract'; +import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; -import Wrk from './Worker'; +import WorkerStaticPool from './WorkerStaticPool'; +import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - if (Configuration.useWorkerPool()) { - return new WorkerPool(workerScript); + public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract { + if (!isMainThread) { + throw new Error('Trying to get a worker implementation outside the main thread'); + } + if (Utils.isUndefined(options)) { + options = {} as WorkerOptions; + } + switch (workerProcessType) { + case WorkerProcessType.WORKER_SET: + options.elementsPerWorker = options.elementsPerWorker ?? 1; + return new WorkerSet(workerScript, options.elementsPerWorker); + case WorkerProcessType.STATIC_POOL: + options.poolMaxSize = options.poolMaxSize ?? 16; + return new WorkerStaticPool(workerScript, options.poolMaxSize); + case WorkerProcessType.DYNAMIC_POOL: + options.poolMinSize = options.poolMinSize ?? 4; + options.poolMaxSize = options.poolMaxSize ?? 16; + return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize); + default: + return null; } - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); } }