Use object factory design pattern for code handling workers.
[e-mobility-charging-stations-simulator.git] / src / charging-station / StationWorker.ts
... / ...
CommitLineData
1import { isMainThread, parentPort, workerData } from 'worker_threads';
2
3import ChargingStation from './ChargingStation';
4import Constants from '../utils/Constants';
5import Utils from '../utils/Utils';
6
7if (!isMainThread) {
8 // Add listener to start charging station from main thread
9 addListener();
10 if (!Utils.isUndefined(workerData)) {
11 startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
12 }
13}
14
15function addListener() {
16 parentPort.on('message', (e) => {
17 if (e.id === Constants.START_WORKER_ELEMENT) {
18 startChargingStation(e.workerData);
19 }
20 });
21}
22
23function startChargingStation(data: any) {
24 const station = new ChargingStation(data.index as number, data.templateFile as string);
25 station.start();
26}