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
, ChargingStationUtils
} from
'./internal';
8 import type { ChargingStationWorkerData
} from
'../types';
9 import { Utils
} from
'../utils';
10 import { WorkerConstants
, type WorkerMessage
, WorkerMessageEvents
} from
'../worker';
12 // Conditionally export ThreadWorker instance for pool usage
13 export let threadWorker
: ThreadWorker
;
14 if (ChargingStationUtils
.workerPoolInUse()) {
15 threadWorker
= new ThreadWorker
<ChargingStationWorkerData
>(startChargingStation
, {
16 maxInactiveTime
: WorkerConstants
.POOL_MAX_INACTIVE_TIME
,
20 // Add message listener to start charging station from main thread
22 if (Utils
.isUndefined(workerData
) === false) {
23 startChargingStation(workerData
as ChargingStationWorkerData
);
28 * Listen messages send by the main thread
30 function addMessageListener(): void {
31 parentPort
?.on('message', (message
: WorkerMessage
<ChargingStationWorkerData
>) => {
32 if (message
.id
=== WorkerMessageEvents
.START_WORKER_ELEMENT
) {
33 startChargingStation(message
.data
);
39 * Create and start a charging station instance
41 * @param data - workerData
43 function startChargingStation(data
: ChargingStationWorkerData
): void {
44 const station
= new ChargingStation(data
.index
, data
.templateFile
);