perf: properly bound methods in hot paths
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 21:16:31 +0000 (23:16 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 21:16:31 +0000 (23:16 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/OCPPIncomingRequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/charging-station/ocpp/OCPPResponseService.ts

index 999e1ceafaf9a703d81bcf2890e69eec728e3c0a..a3aced78aec9289e929e3d71abae9823c116fdae 100644 (file)
@@ -258,7 +258,11 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         ),
       ],
     ]);
-    this.validatePayload.bind(this);
+    this.validatePayload = this.validatePayload.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: OCPP16IncomingRequestCommand,
+      commandPayload: JsonType
+    ) => boolean;
   }
 
   public async incomingRequestHandler(
index 05ab1eb3d1bc6badd37888ef91b77796c815af3d..7158e5348251218e3325b4028dcd94fa88762d2e 100644 (file)
@@ -122,7 +122,11 @@ export class OCPP16RequestService extends OCPPRequestService {
         ),
       ],
     ]);
-    this.buildRequestPayload.bind(this);
+    this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
+      chargingStation: ChargingStation,
+      commandName: OCPP16RequestCommand,
+      commandParams?: JsonType
+    ) => Request;
   }
 
   public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
index 30ed58b00c1a41137aa20472962bccfadef2eee6..2a86135dd78923f335d25fd850274ab25aedeed0 100644 (file)
@@ -280,7 +280,11 @@ export class OCPP16ResponseService extends OCPPResponseService {
         ),
       ],
     ]);
-    this.validatePayload.bind(this);
+    this.validatePayload = this.validatePayload.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: OCPP16RequestCommand,
+      payload: JsonType
+    ) => boolean;
   }
 
   public async responseHandler(
index 1b5cf72c2bbd6a76243fa6f8fc7bc68b21648098..2d1ebd4dba73c1aa5367d4a6754b9b0e87833f9e 100644 (file)
@@ -40,7 +40,11 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
         ),
       ],
     ]);
-    this.validatePayload.bind(this);
+    this.validatePayload = this.validatePayload.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: OCPP20IncomingRequestCommand,
+      commandPayload: JsonType
+    ) => boolean;
   }
 
   public async incomingRequestHandler(
index 49cd8008f74f09b5f273bc9ffcdb5c6de98d980b..4cc9109cfc0e34ad46a7f94ecc85e9dbbdbc06b6 100644 (file)
@@ -59,7 +59,11 @@ export class OCPP20RequestService extends OCPPRequestService {
         ),
       ],
     ]);
-    this.buildRequestPayload.bind(this);
+    this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
+      chargingStation: ChargingStation,
+      commandName: OCPP20RequestCommand,
+      commandParams?: JsonType
+    ) => Request;
   }
 
   public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
index f11af29ee6cded9270f5255b559df30d44e36575..a0af4ab1280f03d751060d3195d3c9127dd7db31 100644 (file)
@@ -79,7 +79,11 @@ export class OCPP20ResponseService extends OCPPResponseService {
         ),
       ],
     ]);
-    this.validatePayload.bind(this);
+    this.validatePayload = this.validatePayload.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: OCPP20RequestCommand,
+      payload: JsonType
+    ) => boolean;
   }
 
   public async responseHandler(
index 2d15ce5d7908435f1d84e9d114df67d02d9bedca..80857cc795b7941bb5a2b8e571c28dc2e3a73661 100644 (file)
@@ -32,8 +32,20 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.incomingRequestHandler.bind(this);
-    this.validateIncomingRequestPayload.bind(this);
+    this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      commandName: IncomingRequestCommand,
+      commandPayload: JsonType
+    ) => Promise<void>;
+    this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
+      T extends JsonType
+    >(
+      chargingStation: ChargingStation,
+      commandName: IncomingRequestCommand,
+      schema: JSONSchemaType<T>,
+      payload: T
+    ) => boolean;
   }
 
   public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
index e00e954c9bfebe5be9e6dd7c6200182559248d90..cc89ab80e75123906b5a54e1a9205bafe59bbc59 100644 (file)
@@ -42,14 +42,63 @@ export abstract class OCPPRequestService {
     });
     ajvFormats(this.ajv);
     this.ocppResponseService = ocppResponseService;
-    this.requestHandler.bind(this);
-    this.sendMessage.bind(this);
-    this.sendResponse.bind(this);
-    this.sendError.bind(this);
-    this.internalSendMessage.bind(this);
-    this.buildMessageToSend.bind(this);
-    this.validateRequestPayload.bind(this);
-    this.validateIncomingRequestResponsePayload.bind(this);
+    this.requestHandler = this.requestHandler.bind(this) as <
+      ReqType extends JsonType,
+      ResType extends JsonType
+    >(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      commandParams?: JsonType,
+      params?: RequestParams
+    ) => Promise<ResType>;
+    this.sendMessage = this.sendMessage.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType,
+      commandName: RequestCommand,
+      params?: RequestParams
+    ) => Promise<ResponseType>;
+    this.sendResponse = this.sendResponse.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType,
+      commandName: IncomingRequestCommand
+    ) => Promise<ResponseType>;
+    this.sendError = this.sendError.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      ocppError: OCPPError,
+      commandName: RequestCommand | IncomingRequestCommand
+    ) => Promise<ResponseType>;
+    this.internalSendMessage = this.internalSendMessage.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType | OCPPError,
+      messageType: MessageType,
+      commandName: RequestCommand | IncomingRequestCommand,
+      params?: RequestParams
+    ) => Promise<ResponseType>;
+    this.buildMessageToSend = this.buildMessageToSend.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType | OCPPError,
+      messageType: MessageType,
+      commandName: RequestCommand | IncomingRequestCommand,
+      responseCallback: ResponseCallback,
+      errorCallback: ErrorCallback
+    ) => string;
+    this.validateRequestPayload = this.validateRequestPayload.bind(this) as <T extends JsonObject>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand | IncomingRequestCommand,
+      payload: T
+    ) => boolean;
+    this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind(
+      this
+    ) as <T extends JsonObject>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand | IncomingRequestCommand,
+      payload: T
+    ) => boolean;
   }
 
   public static getInstance<T extends OCPPRequestService>(
index 5adcf6fb9007ae2ac52e80770f17b10d9d4dba00..c77308907c6dac130338a5eb03a3ce1008b2ca0b 100644 (file)
@@ -31,8 +31,18 @@ export abstract class OCPPResponseService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.responseHandler.bind(this);
-    this.validateResponsePayload.bind(this);
+    this.responseHandler = this.responseHandler.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      payload: JsonType,
+      requestPayload: JsonType
+    ) => Promise<void>;
+    this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      schema: JSONSchemaType<T>,
+      payload: T
+    ) => boolean;
   }
 
   public static getInstance<T extends OCPPResponseService>(this: new () => T): T {