1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 ChargingStationWorkerData
,
5 ChargingStationWorkerMessage
,
6 ChargingStationWorkerMessageEvents
,
7 } from
'../types/ChargingStationWorker';
8 import { parentPort
, workerData
} from
'worker_threads';
10 import ChargingStation from
'./ChargingStation';
11 import { ChargingStationUtils
} from
'./ChargingStationUtils';
12 import { ThreadWorker
} from
'poolifier';
13 import Utils from
'../utils/Utils';
14 import WorkerConstants from
'../worker/WorkerConstants';
16 // Conditionally export ThreadWorker instance for pool usage
17 export let threadWorker
: ThreadWorker
;
18 if (ChargingStationUtils
.workerPoolInUse()) {
19 threadWorker
= new ThreadWorker
<ChargingStationWorkerData
>(startChargingStation
, {
20 maxInactiveTime
: WorkerConstants
.POOL_MAX_INACTIVE_TIME
,
24 // Add message listener to start charging station from main thread
26 if (!Utils
.isUndefined(workerData
)) {
27 startChargingStation(workerData
as ChargingStationWorkerData
);
32 * Listen messages send by the main thread
34 function addMessageListener(): void {
35 parentPort
?.on('message', (message
: ChargingStationWorkerMessage
) => {
36 if (message
.id
=== ChargingStationWorkerMessageEvents
.START_WORKER_ELEMENT
) {
37 startChargingStation(message
.data
);
43 * Create and start a charging station instance
45 * @param data workerData
47 function startChargingStation(data
: ChargingStationWorkerData
): void {
48 const station
= new ChargingStation(data
.index
, data
.templateFile
);