build(simulator): fix assets directory cleanup
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerFactory.ts
CommitLineData
01f4001e 1import { type Worker, isMainThread } from 'node:worker_threads';
b8da29bc 2
6c1761d4 3import type { PoolOptions } from 'poolifier';
8114d10e 4
268a74bb
JB
5import type { WorkerAbstract } from './WorkerAbstract';
6import { WorkerConstants } from './WorkerConstants';
7import { WorkerDynamicPool } from './WorkerDynamicPool';
8import { WorkerSet } from './WorkerSet';
9import { WorkerStaticPool } from './WorkerStaticPool';
10import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes';
6013bc53 11
268a74bb 12export class WorkerFactory {
6c3cfef8
JB
13 private constructor() {
14 // This is intentional
15 }
8df3f0a9 16
e7aeea18
JB
17 public static getWorkerImplementation<T extends WorkerData>(
18 workerScript: string,
19 workerProcessType: WorkerProcessType,
20 workerOptions?: WorkerOptions
21 ): WorkerAbstract<T> | null {
ded13d97 22 if (!isMainThread) {
44a95b7f 23 throw new Error('Cannot get a worker implementation outside the main thread');
ded13d97 24 }
59b6ed8d 25 workerOptions = workerOptions ?? (WorkerConstants.EMPTY_OBJECT as WorkerOptions);
e7aeea18 26 workerOptions.workerStartDelay =
3fa0f0ed 27 workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY;
e7aeea18 28 workerOptions.elementStartDelay =
3fa0f0ed 29 workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
59b6ed8d
JB
30 workerOptions.poolOptions =
31 workerOptions?.poolOptions ?? (WorkerConstants.EMPTY_OBJECT as PoolOptions<Worker>);
e7aeea18
JB
32 workerOptions?.messageHandler &&
33 (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler);
1895299d 34 let workerImplementation: WorkerAbstract<T> | null = null;
535aaa27 35 switch (workerProcessType) {
721646e9 36 case WorkerProcessType.workerSet:
e7aeea18 37 workerOptions.elementsPerWorker =
3fa0f0ed 38 workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
d070d967 39 workerImplementation = new WorkerSet(workerScript, workerOptions);
535aaa27 40 break;
721646e9 41 case WorkerProcessType.staticPool:
e7aeea18 42 workerOptions.poolMaxSize =
3fa0f0ed 43 workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
d070d967 44 workerImplementation = new WorkerStaticPool(workerScript, workerOptions);
535aaa27 45 break;
721646e9 46 case WorkerProcessType.dynamicPool:
e7aeea18 47 workerOptions.poolMinSize =
3fa0f0ed 48 workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE;
e7aeea18 49 workerOptions.poolMaxSize =
3fa0f0ed 50 workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
d070d967 51 workerImplementation = new WorkerDynamicPool(workerScript, workerOptions);
535aaa27 52 break;
fb226c9b 53 default:
3fa0f0ed 54 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
fb226c9b 55 throw new Error(`Worker implementation type '${workerProcessType}' not found`);
6013bc53 56 }
535aaa27 57 return workerImplementation;
6013bc53
JB
58 }
59}