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 public static getWorkerImplementation
<T
>(workerScript
: string, workerProcessType
: WorkerProcessType
, options
?: WorkerOptions
): WorkerAbstract
{
13 throw new Error('Trying to get a worker implementation outside the main thread');
15 options
= options
?? {} as WorkerOptions
;
16 options
.startDelay
= options
.startDelay
?? Constants
.WORKER_START_DELAY
;
17 switch (workerProcessType
) {
18 case WorkerProcessType
.WORKER_SET
:
19 options
.elementsPerWorker
= options
.elementsPerWorker
?? 1;
20 return new WorkerSet
<T
>(workerScript
, options
.elementsPerWorker
, options
.startDelay
);
21 case WorkerProcessType
.STATIC_POOL
:
22 options
.poolMaxSize
= options
.poolMaxSize
?? 16;
23 return new WorkerStaticPool
<T
>(workerScript
, options
.poolMaxSize
, options
.startDelay
);
24 case WorkerProcessType
.DYNAMIC_POOL
:
25 options
.poolMinSize
= options
.poolMinSize
?? 4;
26 options
.poolMaxSize
= options
.poolMaxSize
?? 16;
27 return new WorkerDynamicPool
<T
>(workerScript
, options
.poolMinSize
, options
.poolMaxSize
, options
.startDelay
);