refactor: switch eslint configuration to strict type checking
[e-mobility-charging-stations-simulator.git] / src / charging-station / broadcast-channel / UIServiceWorkerBroadcastChannel.ts
index b385be7fc47aae52e67eaac792dc4f59256c81a1..3e34782f47a7d3d440bdf5719a899121a412ff4f 100644 (file)
-import { WorkerBroadcastChannel } from './WorkerBroadcastChannel';
+import { WorkerBroadcastChannel } from './WorkerBroadcastChannel.js'
 import {
   type BroadcastChannelResponse,
   type BroadcastChannelResponsePayload,
   type MessageEvent,
   type ResponsePayload,
-  ResponseStatus,
-} from '../../types';
-import { isNullOrUndefined, logger } from '../../utils';
-import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService';
+  ResponseStatus
+} from '../../types/index.js'
+import { logger } from '../../utils/index.js'
+import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService.js'
 
-const moduleName = 'UIServiceWorkerBroadcastChannel';
+const moduleName = 'UIServiceWorkerBroadcastChannel'
 
 interface Responses {
-  responsesExpected: number;
-  responsesReceived: number;
-  responses: BroadcastChannelResponsePayload[];
+  responsesExpected: number
+  responsesReceived: number
+  responses: BroadcastChannelResponsePayload[]
 }
 
 export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
-  private readonly uiService: AbstractUIService;
-  private readonly responses: Map<string, Responses>;
+  private readonly uiService: AbstractUIService
+  private readonly responses: Map<string, Responses>
 
-  constructor(uiService: AbstractUIService) {
-    super();
-    this.uiService = uiService;
-    this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void;
-    this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void;
-    this.responses = new Map<string, Responses>();
+  constructor (uiService: AbstractUIService) {
+    super()
+    this.uiService = uiService
+    this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void
+    this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void
+    this.responses = new Map<string, Responses>()
   }
 
-  private responseHandler(messageEvent: MessageEvent): void {
-    const validatedMessageEvent = this.validateMessageEvent(messageEvent);
+  private responseHandler (messageEvent: MessageEvent): void {
+    const validatedMessageEvent = this.validateMessageEvent(messageEvent)
     if (validatedMessageEvent === false) {
-      return;
+      return
     }
-    if (this.isRequest(validatedMessageEvent.data) === true) {
-      return;
+    if (this.isRequest(validatedMessageEvent.data)) {
+      return
     }
-    const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse;
-    if (this.responses.has(uuid) === false) {
+    const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse
+    if (!this.responses.has(uuid)) {
       this.responses.set(uuid, {
         responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid),
         responsesReceived: 1,
-        responses: [responsePayload],
-      });
+        responses: [responsePayload]
+      })
     } else if (
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       this.responses.get(uuid)!.responsesReceived <= this.responses.get(uuid)!.responsesExpected
     ) {
-      ++this.responses.get(uuid)!.responsesReceived;
-      this.responses.get(uuid)?.responses.push(responsePayload);
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      ++this.responses.get(uuid)!.responsesReceived
+      this.responses.get(uuid)?.responses.push(responsePayload)
     }
     if (
       this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected
     ) {
-      this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid));
-      this.responses.delete(uuid);
-      this.uiService.deleteBroadcastChannelRequest(uuid);
+      this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid))
+      this.responses.delete(uuid)
+      this.uiService.deleteBroadcastChannelRequest(uuid)
     }
   }
 
-  private buildResponsePayload(uuid: string): ResponsePayload {
+  private buildResponsePayload (uuid: string): ResponsePayload {
     const responsesStatus =
       this.responses
         .get(uuid)
         ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true
         ? ResponseStatus.SUCCESS
-        : ResponseStatus.FAILURE;
+        : ResponseStatus.FAILURE
     return {
       status: responsesStatus,
       hashIdsSucceeded: this.responses
         .get(uuid)
-        ?.responses.filter(({ hashId }) => !isNullOrUndefined(hashId))
-        .map(({ status, hashId }) => {
-          if (status === ResponseStatus.SUCCESS) {
-            return hashId;
+        ?.responses.map(({ status, hashId }) => {
+          if (hashId != null && status === ResponseStatus.SUCCESS) {
+            return hashId
           }
-        }) as string[],
+          return undefined
+        })
+        .filter((hashId) => hashId != null) as string[],
       ...(responsesStatus === ResponseStatus.FAILURE && {
         hashIdsFailed: this.responses
           .get(uuid)
-          ?.responses.filter(({ hashId }) => !isNullOrUndefined(hashId))
-          .map(({ status, hashId }) => {
-            if (status === ResponseStatus.FAILURE) {
-              return hashId;
+          ?.responses.map(({ status, hashId }) => {
+            if (hashId != null && status === ResponseStatus.FAILURE) {
+              return hashId
             }
-          }) as string[],
+            return undefined
+          })
+          .filter((hashId) => hashId != null) as string[]
       }),
       ...(responsesStatus === ResponseStatus.FAILURE && {
         responsesFailed: this.responses
           .get(uuid)
-          ?.responses.filter((response) => !isNullOrUndefined(response))
-          .map((response) => {
+          ?.responses.map((response) => {
             if (response.status === ResponseStatus.FAILURE) {
-              return response;
+              return response
             }
-          }) as BroadcastChannelResponsePayload[],
-      }),
-    };
+            return undefined
+          })
+          .filter((response) => response != null) as BroadcastChannelResponsePayload[]
+      })
+    }
   }
 
-  private messageErrorHandler(messageEvent: MessageEvent): void {
+  private messageErrorHandler (messageEvent: MessageEvent): void {
     logger.error(
       `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
-      messageEvent,
-    );
+      messageEvent
+    )
   }
 }