Commit | Line | Data |
---|---|---|
ffd71f2c | 1 | import { Worker, isMainThread } from 'worker_threads'; |
c3ee95af | 2 | import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; |
b8da29bc | 3 | |
ffd71f2c | 4 | import { PoolOptions } from 'poolifier'; |
73b9adec | 5 | import type WorkerAbstract from './WorkerAbstract'; |
3fa0f0ed | 6 | import WorkerConstants from './WorkerConstants'; |
a4624c96 | 7 | import WorkerDynamicPool from './WorkerDynamicPool'; |
6013bc53 | 8 | import WorkerSet from './WorkerSet'; |
a4624c96 | 9 | import WorkerStaticPool from './WorkerStaticPool'; |
6013bc53 JB |
10 | |
11 | export default class WorkerFactory { | |
6c3cfef8 JB |
12 | private constructor() { |
13 | // This is intentional | |
14 | } | |
8df3f0a9 | 15 | |
e7aeea18 JB |
16 | public static getWorkerImplementation<T extends WorkerData>( |
17 | workerScript: string, | |
18 | workerProcessType: WorkerProcessType, | |
19 | workerOptions?: WorkerOptions | |
20 | ): WorkerAbstract<T> | null { | |
ded13d97 JB |
21 | if (!isMainThread) { |
22 | throw new Error('Trying to get a worker implementation outside the main thread'); | |
23 | } | |
6fee1791 | 24 | workerOptions = workerOptions ?? ({} as WorkerOptions); |
e7aeea18 | 25 | workerOptions.workerStartDelay = |
6fee1791 | 26 | workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; |
e7aeea18 | 27 | workerOptions.elementStartDelay = |
3fa0f0ed | 28 | workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; |
e7aeea18 JB |
29 | workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions<Worker>); |
30 | workerOptions?.messageHandler && | |
3fa0f0ed | 31 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
e7aeea18 | 32 | (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); |
a0239c1f | 33 | console.log('before'); |
6fee1791 | 34 | let workerImplementation: WorkerAbstract<T> = null; |
a0239c1f | 35 | console.log(workerImplementation); |
535aaa27 JB |
36 | switch (workerProcessType) { |
37 | case WorkerProcessType.WORKER_SET: | |
e7aeea18 | 38 | workerOptions.elementsPerWorker = |
3fa0f0ed | 39 | workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; |
d070d967 | 40 | workerImplementation = new WorkerSet(workerScript, workerOptions); |
535aaa27 JB |
41 | break; |
42 | case WorkerProcessType.STATIC_POOL: | |
e7aeea18 | 43 | workerOptions.poolMaxSize = |
3fa0f0ed | 44 | workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; |
d070d967 | 45 | workerImplementation = new WorkerStaticPool(workerScript, workerOptions); |
535aaa27 JB |
46 | break; |
47 | case WorkerProcessType.DYNAMIC_POOL: | |
e7aeea18 | 48 | workerOptions.poolMinSize = |
3fa0f0ed | 49 | workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; |
e7aeea18 | 50 | workerOptions.poolMaxSize = |
3fa0f0ed | 51 | workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; |
d070d967 | 52 | workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); |
535aaa27 | 53 | break; |
fb226c9b | 54 | default: |
3fa0f0ed | 55 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
fb226c9b | 56 | throw new Error(`Worker implementation type '${workerProcessType}' not found`); |
6013bc53 | 57 | } |
535aaa27 | 58 | return workerImplementation; |
6013bc53 JB |
59 | } |
60 | } |