Commit | Line | Data |
---|---|---|
b8da29bc JB |
1 | import { WorkerOptions, WorkerProcessType } from '../types/Worker'; |
2 | ||
3 | import Utils from '../utils/Utils'; | |
a4624c96 | 4 | import WorkerDynamicPool from './WorkerDynamicPool'; |
6013bc53 | 5 | import WorkerSet from './WorkerSet'; |
a4624c96 | 6 | import WorkerStaticPool from './WorkerStaticPool'; |
144cabe0 | 7 | import Wrk from './Wrk'; |
6013bc53 JB |
8 | |
9 | export default class WorkerFactory { | |
8434025b | 10 | public static getWorkerImpl<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { |
b8da29bc JB |
11 | if (Utils.isUndefined(options)) { |
12 | options = {} as WorkerOptions; | |
13 | } | |
14 | switch (workerProcessType) { | |
a4624c96 | 15 | case WorkerProcessType.WORKER_SET: |
b8da29bc JB |
16 | if (Utils.isUndefined(options.elementsPerWorker)) { |
17 | options.elementsPerWorker = 1; | |
18 | } | |
8434025b | 19 | return new WorkerSet<T>(workerScript, options.elementsPerWorker); |
a4624c96 | 20 | case WorkerProcessType.STATIC_POOL: |
b8da29bc JB |
21 | if (Utils.isUndefined(options.poolMaxSize)) { |
22 | options.elementsPerWorker = 16; | |
23 | } | |
8434025b | 24 | return new WorkerStaticPool<T>(workerScript, options.poolMaxSize); |
a4624c96 | 25 | case WorkerProcessType.DYNAMIC_POOL: |
b8da29bc JB |
26 | if (Utils.isUndefined(options.poolMinSize)) { |
27 | options.elementsPerWorker = 4; | |
28 | } | |
29 | if (Utils.isUndefined(options.poolMaxSize)) { | |
30 | options.elementsPerWorker = 16; | |
31 | } | |
8434025b | 32 | return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize); |
a4624c96 JB |
33 | default: |
34 | return null; | |
6013bc53 | 35 | } |
6013bc53 JB |
36 | } |
37 | } |