]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blame - src/worker/WorkerAbstract.ts
chore(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
CommitLineData
66a7748d 1import type { EventEmitterAsyncResource } from 'node:events'
66a7748d 2import type { PoolInfo } from 'poolifier'
b779c0f8 3
0749233f
JB
4import { existsSync } from 'node:fs'
5
66a7748d 6import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js'
7dde0b73 7
3b09e788 8export abstract class WorkerAbstract<D extends WorkerData, R extends WorkerData> {
0749233f 9 public abstract readonly emitter: EventEmitterAsyncResource | undefined
66a7748d 10 public abstract readonly info: PoolInfo | SetInfo
66a7748d 11 public abstract readonly maxElementsPerWorker: number | undefined
0749233f 12 public abstract readonly size: number
6af9012e 13
c4a89082
JB
14 protected readonly workerOptions: WorkerOptions
15 protected readonly workerScript: string
16
7dde0b73 17 /**
e71cccf3 18 * `WorkerAbstract` constructor.
0e4fa348
JB
19 * @param workerScript -
20 * @param workerOptions -
7dde0b73 21 */
5199f9fd 22 constructor (workerScript: string | undefined, workerOptions: WorkerOptions) {
a78c196b 23 if (workerScript == null) {
66a7748d 24 throw new TypeError('Worker script is not defined')
44a95b7f 25 }
49e2c1e5 26 if (typeof workerScript !== 'string') {
66a7748d 27 throw new TypeError('Worker script must be a string')
49e2c1e5
JB
28 }
29 if (workerScript.trim().length === 0) {
66a7748d 30 throw new Error('Worker script is an empty string')
ba516f9c 31 }
d972af76 32 if (!existsSync(workerScript)) {
66a7748d 33 throw new Error('Worker script file does not exist')
44a95b7f 34 }
66a7748d
JB
35 this.workerScript = workerScript
36 this.workerOptions = workerOptions
7dde0b73
JB
37 }
38
0749233f
JB
39 /**
40 * Adds a task element to the worker pool/set.
41 * @param elementData -
42 */
43 public abstract addElement (elementData: D): Promise<R>
8baf3f8f 44 /**
361c98f5 45 * Starts the worker pool/set.
8baf3f8f 46 */
0749233f 47 public abstract start (): Promise<void> | void
8baf3f8f 48 /**
361c98f5 49 * Stops the worker pool/set.
8baf3f8f 50 */
66a7748d 51 public abstract stop (): Promise<void>
7dde0b73 52}