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