1 import type { EventEmitterAsyncResource
} from
'node:events'
2 import type { PoolInfo
} from
'poolifier'
4 import { existsSync
} from
'node:fs'
6 import type { SetInfo
, WorkerData
, WorkerOptions
} from
'./WorkerTypes.js'
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
14 protected readonly workerOptions
: WorkerOptions
15 protected readonly workerScript
: string
18 * `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 * Adds a task element to the worker pool/set.
41 * @param elementData -
43 public abstract addElement (elementData
: D
): Promise
<R
>
45 * Starts the worker pool/set.
47 public abstract start (): Promise
<void> | void
49 * Stops the worker pool/set.
51 public abstract stop (): Promise
<void>