1 import { WorkerOptions
, WorkerProcessType
} from
'../types/Worker';
3 import Utils from
'../utils/Utils';
4 import WorkerDynamicPool from
'./WorkerDynamicPool';
5 import WorkerSet from
'./WorkerSet';
6 import WorkerStaticPool from
'./WorkerStaticPool';
7 import Wrk from
'./Wrk';
8 import { isMainThread
} from
'worker_threads';
10 export default class WorkerFactory
{
11 public static getWorkerImpl
<T
>(workerScript
: string, workerProcessType
: WorkerProcessType
, options
?: WorkerOptions
): Wrk
{
13 throw new Error('Trying to get a worker implementation outside the main thread');
15 if (Utils
.isUndefined(options
)) {
16 options
= {} as WorkerOptions
;
18 switch (workerProcessType
) {
19 case WorkerProcessType
.WORKER_SET
:
20 if (Utils
.isUndefined(options
.elementsPerWorker
)) {
21 options
.elementsPerWorker
= 1;
23 return new WorkerSet
<T
>(workerScript
, options
.elementsPerWorker
);
24 case WorkerProcessType
.STATIC_POOL
:
25 if (Utils
.isUndefined(options
.poolMaxSize
)) {
26 options
.elementsPerWorker
= 16;
28 return new WorkerStaticPool
<T
>(workerScript
, options
.poolMaxSize
);
29 case WorkerProcessType
.DYNAMIC_POOL
:
30 if (Utils
.isUndefined(options
.poolMinSize
)) {
31 options
.elementsPerWorker
= 4;
33 if (Utils
.isUndefined(options
.poolMaxSize
)) {
34 options
.elementsPerWorker
= 16;
36 return new WorkerDynamicPool
<T
>(workerScript
, options
.poolMinSize
, options
.poolMaxSize
);