Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
c8eeb62b | 2 | |
66a7748d | 3 | import { parentPort } from 'node:worker_threads' |
8114d10e | 4 | |
66a7748d | 5 | import { ThreadWorker } from 'poolifier' |
8114d10e | 6 | |
66a7748d | 7 | import { BaseError } from '../exception/index.js' |
65d22502 | 8 | import type { ChargingStationInfo, ChargingStationWorkerData } from '../types/index.js' |
3b09e788 | 9 | import { Configuration } from '../utils/index.js' |
65d22502 | 10 | import { type WorkerDataError, type WorkerMessage, WorkerMessageEvents } from '../worker/index.js' |
4c3f6c20 | 11 | import { ChargingStation } from './ChargingStation.js' |
11353865 | 12 | |
244c1396 | 13 | export let chargingStationWorker: object |
aa7d6d95 | 14 | if (Configuration.workerPoolInUse()) { |
3b09e788 JB |
15 | chargingStationWorker = new ThreadWorker< |
16 | ChargingStationWorkerData, | |
17 | ChargingStationInfo | undefined | |
18 | >((data?: ChargingStationWorkerData): ChargingStationInfo | undefined => { | |
65d22502 JB |
19 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
20 | const { index, templateFile, options } = data! | |
21 | return new ChargingStation(index, templateFile, options).stationInfo | |
3b09e788 | 22 | }) |
74bbc59b | 23 | } else { |
244c1396 JB |
24 | // eslint-disable-next-line @typescript-eslint/no-extraneous-class |
25 | class ChargingStationWorker<Data extends ChargingStationWorkerData> { | |
26 | constructor () { | |
27 | parentPort?.on('message', (message: WorkerMessage<Data>) => { | |
65d22502 JB |
28 | const { uuid, event, data } = message |
29 | switch (event) { | |
244c1396 JB |
30 | case WorkerMessageEvents.addWorkerElement: |
31 | try { | |
32 | const chargingStation = new ChargingStation( | |
65d22502 JB |
33 | data.index, |
34 | data.templateFile, | |
35 | data.options | |
244c1396 | 36 | ) |
244c1396 | 37 | parentPort?.postMessage({ |
65d22502 | 38 | uuid, |
244c1396 | 39 | event: WorkerMessageEvents.addedWorkerElement, |
3b09e788 JB |
40 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
41 | data: chargingStation.stationInfo! | |
65d22502 | 42 | } satisfies WorkerMessage<ChargingStationInfo>) |
244c1396 JB |
43 | } catch (error) { |
44 | parentPort?.postMessage({ | |
65d22502 | 45 | uuid, |
244c1396 JB |
46 | event: WorkerMessageEvents.workerElementError, |
47 | data: { | |
65d22502 | 48 | event, |
244c1396 JB |
49 | name: (error as Error).name, |
50 | message: (error as Error).message, | |
51 | stack: (error as Error).stack | |
52 | } | |
65d22502 | 53 | } satisfies WorkerMessage<WorkerDataError>) |
244c1396 JB |
54 | } |
55 | break | |
56 | default: | |
57 | throw new BaseError( | |
65d22502 JB |
58 | `Unknown worker event: '${event}' received with data: '${JSON.stringify( |
59 | data, | |
244c1396 JB |
60 | undefined, |
61 | 2 | |
62 | )}'` | |
63 | ) | |
64 | } | |
65 | }) | |
66 | } | |
67 | } | |
66a7748d | 68 | chargingStationWorker = new ChargingStationWorker<ChargingStationWorkerData>() |
3d2ff9e4 | 69 | } |