1 import { WorkerOptions
, WorkerProcessType
} from
'../types/Worker';
3 import Constants from
'../utils/Constants';
4 import WorkerAbstract from
'./WorkerAbstract';
5 import WorkerDynamicPool from
'./WorkerDynamicPool';
6 import WorkerSet from
'./WorkerSet';
7 import WorkerStaticPool from
'./WorkerStaticPool';
8 import { isMainThread
} from
'worker_threads';
10 export default class WorkerFactory
{
11 // eslint-disable-next-line @typescript-eslint/no-empty-function
12 private constructor() {}
14 public static getWorkerImplementation
<T
>(workerScript
: string, workerProcessType
: WorkerProcessType
, options
?: WorkerOptions
): WorkerAbstract
| null {
16 throw new Error('Trying to get a worker implementation outside the main thread');
18 options
= options
?? {} as WorkerOptions
;
19 options
.startDelay
= options
.startDelay
?? Constants
.WORKER_START_DELAY
;
20 let workerImplementation
: WorkerAbstract
= null;
21 switch (workerProcessType
) {
22 case WorkerProcessType
.WORKER_SET
:
23 options
.elementsPerWorker
= options
.elementsPerWorker
?? Constants
.DEFAULT_CHARGING_STATIONS_PER_WORKER
;
24 workerImplementation
= new WorkerSet
<T
>(workerScript
, options
.elementsPerWorker
, options
.startDelay
);
26 case WorkerProcessType
.STATIC_POOL
:
27 options
.poolMaxSize
= options
.poolMaxSize
?? Constants
.DEFAULT_WORKER_POOL_MAX_SIZE
;
28 workerImplementation
= new WorkerStaticPool
<T
>(workerScript
, options
.poolMaxSize
, options
.startDelay
, options
.poolOptions
);
30 case WorkerProcessType
.DYNAMIC_POOL
:
31 options
.poolMinSize
= options
.poolMinSize
?? Constants
.DEFAULT_WORKER_POOL_MIN_SIZE
;
32 options
.poolMaxSize
= options
.poolMaxSize
?? Constants
.DEFAULT_WORKER_POOL_MAX_SIZE
;
33 workerImplementation
= new WorkerDynamicPool
<T
>(workerScript
, options
.poolMinSize
, options
.poolMaxSize
, options
.startDelay
, options
.poolOptions
);
36 return workerImplementation
;