fix: brown paper bag issue at referencing the same literal object instance
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
CommitLineData
130783a7 1import fs from 'node:fs';
4bfd80fa 2
268a74bb
JB
3import { WorkerConstants } from './WorkerConstants';
4import type { WorkerData, WorkerOptions } from './WorkerTypes';
7dde0b73 5
268a74bb 6export abstract class WorkerAbstract<T extends WorkerData> {
322c9192 7 protected readonly workerScript: string;
4d7227e6 8 protected readonly workerOptions: WorkerOptions;
f2bf9948 9 public abstract readonly size: number;
72092cfc 10 public abstract readonly maxElementsPerWorker: number | undefined;
6af9012e 11
7dde0b73 12 /**
e71cccf3 13 * `WorkerAbstract` constructor.
7dde0b73 14 *
0e4fa348
JB
15 * @param workerScript -
16 * @param workerOptions -
7dde0b73 17 */
e7aeea18
JB
18 constructor(
19 workerScript: string,
20 workerOptions: WorkerOptions = {
3fa0f0ed
JB
21 workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
22 elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
23 poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
24 poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
25 elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
abe9e9dd 26 poolOptions: {},
59b6ed8d 27 messageHandler: WorkerConstants.EMPTY_FUNCTION,
e7aeea18
JB
28 }
29 ) {
44a95b7f
JB
30 if (!workerScript) {
31 throw new Error('Worker script is not defined');
32 }
33 if (!fs.existsSync(workerScript)) {
34 throw new Error('Worker script file does not exist');
35 }
ad2f27c3 36 this.workerScript = workerScript;
4d7227e6 37 this.workerOptions = workerOptions;
7dde0b73
JB
38 }
39
418106c8 40 public abstract start(): Promise<void>;
ded13d97 41 public abstract stop(): Promise<void>;
c3ee95af 42 public abstract addElement(elementData: T): Promise<void>;
7dde0b73 43}