]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blame_incremental - src/worker/WorkerAbstract.ts
chore(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
... / ...
CommitLineData
1import type { EventEmitterAsyncResource } from 'node:events'
2import type { PoolInfo } from 'poolifier'
3
4import { existsSync } from 'node:fs'
5
6import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js'
7
8export 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}