]>
Commit | Line | Data |
---|---|---|
1 | import type { EventEmitterAsyncResource } from 'node:events' | |
2 | import type { PoolInfo } from 'poolifier' | |
3 | ||
4 | import { existsSync } from 'node:fs' | |
5 | ||
6 | import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js' | |
7 | ||
8 | export abstract class WorkerAbstract<D extends WorkerData, R extends WorkerData> { | |
9 | public abstract readonly emitter: EventEmitterAsyncResource | undefined | |
10 | public abstract readonly info: PoolInfo | SetInfo | |
11 | public abstract readonly maxElementsPerWorker: number | undefined | |
12 | public abstract readonly size: number | |
13 | ||
14 | protected readonly workerOptions: WorkerOptions | |
15 | protected readonly workerScript: string | |
16 | ||
17 | /** | |
18 | * `WorkerAbstract` constructor. | |
19 | * @param workerScript - | |
20 | * @param workerOptions - | |
21 | */ | |
22 | constructor (workerScript: string | undefined, workerOptions: WorkerOptions) { | |
23 | if (workerScript == null) { | |
24 | throw new TypeError('Worker script is not defined') | |
25 | } | |
26 | if (typeof workerScript !== 'string') { | |
27 | throw new TypeError('Worker script must be a string') | |
28 | } | |
29 | if (workerScript.trim().length === 0) { | |
30 | throw new Error('Worker script is an empty string') | |
31 | } | |
32 | if (!existsSync(workerScript)) { | |
33 | throw new Error('Worker script file does not exist') | |
34 | } | |
35 | this.workerScript = workerScript | |
36 | this.workerOptions = workerOptions | |
37 | } | |
38 | ||
39 | /** | |
40 | * Adds a task element to the worker pool/set. | |
41 | * @param elementData - | |
42 | */ | |
43 | public abstract addElement (elementData: D): Promise<R> | |
44 | /** | |
45 | * Starts the worker pool/set. | |
46 | */ | |
47 | public abstract start (): Promise<void> | void | |
48 | /** | |
49 | * Stops the worker pool/set. | |
50 | */ | |
51 | public abstract stop (): Promise<void> | |
52 | } |