1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { parentPort
, workerData
} from
'worker_threads';
5 import { ThreadWorker
} from
'poolifier';
7 import { ChargingStation
} from
'./ChargingStation';
8 import { ChargingStationUtils
} from
'./ChargingStationUtils';
9 import type { ChargingStationWorkerData
} from
'../types';
10 import { Utils
} from
'../utils/Utils';
11 import { WorkerConstants
, type WorkerMessage
, WorkerMessageEvents
} from
'../worker';
13 // Conditionally export ThreadWorker instance for pool usage
14 export let threadWorker
: ThreadWorker
;
15 if (ChargingStationUtils
.workerPoolInUse()) {
16 threadWorker
= new ThreadWorker
<ChargingStationWorkerData
>(startChargingStation
, {
17 maxInactiveTime
: WorkerConstants
.POOL_MAX_INACTIVE_TIME
,
21 // Add message listener to start charging station from main thread
23 if (Utils
.isUndefined(workerData
) === false) {
24 startChargingStation(workerData
as ChargingStationWorkerData
);
29 * Listen messages send by the main thread
31 function addMessageListener(): void {
32 parentPort
?.on('message', (message
: WorkerMessage
<ChargingStationWorkerData
>) => {
33 if (message
.id
=== WorkerMessageEvents
.START_WORKER_ELEMENT
) {
34 startChargingStation(message
.data
);
40 * Create and start a charging station instance
42 * @param data - workerData
44 function startChargingStation(data
: ChargingStationWorkerData
): void {
45 const station
= new ChargingStation(data
.index
, data
.templateFile
);