feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerFactory.ts
CommitLineData
66a7748d 1import { isMainThread } from 'node:worker_threads'
b8da29bc 2
56f94590
JB
3import { mergeDeepRight } from 'rambda'
4
66a7748d
JB
5import type { WorkerAbstract } from './WorkerAbstract.js'
6import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants.js'
7import { WorkerDynamicPool } from './WorkerDynamicPool.js'
8import { WorkerFixedPool } from './WorkerFixedPool.js'
9import { WorkerSet } from './WorkerSet.js'
10import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes.js'
6013bc53 11
66a7748d 12// eslint-disable-next-line @typescript-eslint/no-extraneous-class
268a74bb 13export class WorkerFactory {
66a7748d 14 private constructor () {
6c3cfef8
JB
15 // This is intentional
16 }
8df3f0a9 17
3b09e788 18 public static getWorkerImplementation<D extends WorkerData, R extends WorkerData>(
e7aeea18
JB
19 workerScript: string,
20 workerProcessType: WorkerProcessType,
66a7748d 21 workerOptions?: WorkerOptions
3b09e788 22 ): WorkerAbstract<D, R> {
ded13d97 23 if (!isMainThread) {
66a7748d 24 throw new Error('Cannot get a worker implementation outside the main thread')
ded13d97 25 }
56f94590 26 workerOptions = mergeDeepRight<WorkerOptions>(DEFAULT_WORKER_OPTIONS, workerOptions ?? {})
535aaa27 27 switch (workerProcessType) {
721646e9 28 case WorkerProcessType.workerSet:
3b09e788 29 return new WorkerSet<D, R>(workerScript, workerOptions)
1d8f226b 30 case WorkerProcessType.fixedPool:
3b09e788 31 return new WorkerFixedPool<D, R>(workerScript, workerOptions)
721646e9 32 case WorkerProcessType.dynamicPool:
3b09e788 33 return new WorkerDynamicPool<D, R>(workerScript, workerOptions)
fb226c9b 34 default:
3fa0f0ed 35 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
66a7748d 36 throw new Error(`Worker implementation type '${workerProcessType}' not found`)
6013bc53 37 }
6013bc53
JB
38 }
39}