build: make eslint configuration use strict type checking
[poolifier.git] / src / worker / abstract-worker.ts
index 2359758dc72c2a362b60e513890664731fdc7fea..890f340e69a63ac1bce217ecafa0ae5191febae1 100644 (file)
@@ -71,7 +71,7 @@ export abstract class AbstractWorker<
   /**
    * Performance statistics computation requirements.
    */
-  protected statistics!: WorkerStatistics
+  protected statistics?: WorkerStatistics
   /**
    * Handler id of the `activeInterval` worker activity check.
    */
@@ -86,8 +86,8 @@ export abstract class AbstractWorker<
    * @param opts - Options for the worker.
    */
   public constructor (
-    protected readonly isMain: boolean,
-    private readonly mainWorker: MainWorker,
+    protected readonly isMain: boolean | undefined,
+    private readonly mainWorker: MainWorker | undefined | null,
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
   ) {
@@ -113,7 +113,10 @@ export abstract class AbstractWorker<
    * @param taskFunctions - The task function(s) parameter that should be checked.
    */
   private checkTaskFunctions (
-    taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>
+    taskFunctions:
+    | TaskFunction<Data, Response>
+    | TaskFunctions<Data, Response>
+    | undefined
   ): void {
     if (taskFunctions == null) {
       throw new Error('taskFunctions parameter is mandatory')
@@ -352,7 +355,7 @@ export abstract class AbstractWorker<
       taskFunctionOperationStatus: response.status,
       taskFunctionName,
       ...(!response.status &&
-        response?.error != null && {
+        response.error != null && {
         workerError: {
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           name: taskFunctionName!,
@@ -370,7 +373,7 @@ export abstract class AbstractWorker<
   protected handleKillMessage (_message: MessageValue<Data>): void {
     this.stopCheckActive()
     if (isAsyncFunction(this.opts.killHandler)) {
-      (this.opts.killHandler?.() as Promise<void>)
+      (this.opts.killHandler() as Promise<void>)
         .then(() => {
           this.sendToMainWorker({ kill: 'success' })
           return undefined
@@ -492,8 +495,7 @@ export abstract class AbstractWorker<
         workerError: {
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           name: name!,
-          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          message: `Task function '${name!}' not found`,
+          message: `Task function '${name}' not found`,
           data
         },
         taskId
@@ -583,18 +585,24 @@ export abstract class AbstractWorker<
   }
 
   private beginTaskPerformance (name?: string): TaskPerformance {
-    this.checkStatistics()
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
     return {
       name: name ?? DEFAULT_TASK_NAME,
       timestamp: performance.now(),
-      ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
+      ...(this.statistics.elu && {
+        elu: performance.eventLoopUtilization()
+      })
     }
   }
 
   private endTaskPerformance (
     taskPerformance: TaskPerformance
   ): TaskPerformance {
-    this.checkStatistics()
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
     return {
       ...taskPerformance,
       ...(this.statistics.runTime && {
@@ -606,12 +614,6 @@ export abstract class AbstractWorker<
     }
   }
 
-  private checkStatistics (): void {
-    if (this.statistics == null) {
-      throw new Error('Performance statistics computation requirements not set')
-    }
-  }
-
   private updateLastTaskTimestamp (): void {
     if (this.activeInterval != null) {
       this.lastTaskTimestamp = performance.now()