Apply prettier formating
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
CommitLineData
4d7227e6 1import { WorkerData, WorkerOptions } from '../types/Worker';
a4624c96 2
4d7227e6 3import { FixedThreadPool } from 'poolifier';
a4624c96 4import Utils from '../utils/Utils';
fd1fdf1b 5import WorkerAbstract from './WorkerAbstract';
7874b0b1 6import { WorkerUtils } from './WorkerUtils';
a4624c96 7
c3ee95af 8export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
f2bf9948 9 private readonly pool: FixedThreadPool<WorkerData>;
a4624c96
JB
10
11 /**
12 * Create a new `WorkerStaticPool`.
13 *
81797102 14 * @param workerScript
4d7227e6 15 * @param workerOptions
a4624c96 16 */
4d7227e6
JB
17 constructor(workerScript: string, workerOptions?: WorkerOptions) {
18 super(workerScript, workerOptions);
e7aeea18
JB
19 this.workerOptions.poolOptions.exitHandler =
20 this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
21 this.pool = new FixedThreadPool(
22 this.workerOptions.poolMaxSize,
23 this.workerScript,
24 this.workerOptions.poolOptions
25 );
a4624c96
JB
26 }
27
28 get size(): number {
29 return this.pool.workers.length;
30 }
31
6e0964c8 32 get maxElementsPerWorker(): number | null {
85f78bc0 33 return null;
a4624c96
JB
34 }
35
36 /**
37 *
81797102 38 * @returns
a4624c96
JB
39 * @public
40 */
81797102
JB
41 public async start(): Promise<void> {
42 // This is intentional
43 }
a4624c96 44
ded13d97
JB
45 /**
46 *
81797102 47 * @returns
ded13d97
JB
48 * @public
49 */
50 public async stop(): Promise<void> {
51 return this.pool.destroy();
52 }
53
a4624c96
JB
54 /**
55 *
81797102
JB
56 * @param elementData
57 * @returns
a4624c96
JB
58 * @public
59 */
c3ee95af 60 public async addElement(elementData: WorkerData): Promise<void> {
a4624c96 61 await this.pool.execute(elementData);
4bfd80fa 62 // Start element sequentially to optimize memory at startup
e7aeea18
JB
63 this.workerOptions.elementStartDelay > 0 &&
64 (await Utils.sleep(this.workerOptions.elementStartDelay));
a4624c96
JB
65 }
66}