refactor: remove unneeded null checks
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 12:34:56 +0000 (14:34 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 12:34:56 +0000 (14:34 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/least-busy-worker-choice-strategy.ts
src/pools/selection-strategies/least-elu-worker-choice-strategy.ts

index 534cb49080a7127f10a366cf9b99d6187f18fb61..0b56cbe42dcfab43ce802cbcaecc92992292c8f1 100644 (file)
@@ -297,7 +297,7 @@ export abstract class AbstractPool<
       throw new TypeError('Invalid tasks queue options: must be a plain object')
     }
     if (
-      tasksQueueOptions?.concurrency != null &&
+      tasksQueueOptions.concurrency != null &&
       !Number.isSafeInteger(tasksQueueOptions.concurrency)
     ) {
       throw new TypeError(
@@ -305,27 +305,27 @@ export abstract class AbstractPool<
       )
     }
     if (
-      tasksQueueOptions?.concurrency != null &&
+      tasksQueueOptions.concurrency != null &&
       tasksQueueOptions.concurrency <= 0
     ) {
       throw new RangeError(
         `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
       )
     }
-    if (tasksQueueOptions?.queueMaxSize != null) {
+    if (tasksQueueOptions.queueMaxSize != null) {
       throw new Error(
         'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead'
       )
     }
     if (
-      tasksQueueOptions?.size != null &&
+      tasksQueueOptions.size != null &&
       !Number.isSafeInteger(tasksQueueOptions.size)
     ) {
       throw new TypeError(
         'Invalid worker node tasks queue size: must be an integer'
       )
     }
-    if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
+    if (tasksQueueOptions.size != null && tasksQueueOptions.size <= 0) {
       throw new RangeError(
         `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
       )
@@ -770,7 +770,7 @@ export abstract class AbstractPool<
       }
       const timestamp = performance.now()
       const workerNodeKey = this.chooseWorkerNode()
-      const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
+      const workerInfo = this.getWorkerInfo(workerNodeKey)
       const task: Task<Data> = {
         name: name ?? DEFAULT_TASK_NAME,
         // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -1064,7 +1064,7 @@ export abstract class AbstractPool<
     worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
     worker.on('error', (error) => {
       const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
-      const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
+      const workerInfo = this.getWorkerInfo(workerNodeKey)
       workerInfo.ready = false
       this.workerNodes[workerNodeKey].closeChannel()
       this.emitter?.emit(PoolEvents.error, error)
@@ -1122,7 +1122,7 @@ export abstract class AbstractPool<
         })
       }
     })
-    const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
+    const workerInfo = this.getWorkerInfo(workerNodeKey)
     this.sendToWorker(workerNodeKey, {
       checkActive: true,
       workerId: workerInfo.id as number
@@ -1193,7 +1193,7 @@ export abstract class AbstractPool<
         elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
           .elu.aggregate
       },
-      workerId: (this.getWorkerInfo(workerNodeKey) as WorkerInfo).id as number
+      workerId: this.getWorkerInfo(workerNodeKey).id as number
     })
   }
 
@@ -1337,10 +1337,8 @@ export abstract class AbstractPool<
         this.handleTaskExecutionResponse(message)
       } else if (message.taskFunctions != null) {
         // Task functions message received from worker
-        (
-          this.getWorkerInfo(
-            this.getWorkerNodeKeyByWorkerId(message.workerId)
-          ) as WorkerInfo
+        this.getWorkerInfo(
+          this.getWorkerNodeKeyByWorkerId(message.workerId)
         ).taskFunctions = message.taskFunctions
       }
     }
@@ -1352,7 +1350,7 @@ export abstract class AbstractPool<
     }
     const workerInfo = this.getWorkerInfo(
       this.getWorkerNodeKeyByWorkerId(message.workerId)
-    ) as WorkerInfo
+    )
     workerInfo.ready = message.ready as boolean
     workerInfo.taskFunctions = message.taskFunctions
     if (this.emitter != null && this.ready) {
@@ -1414,8 +1412,8 @@ export abstract class AbstractPool<
    * @param workerNodeKey - The worker node key.
    * @returns The worker information.
    */
-  protected getWorkerInfo (workerNodeKey: number): WorkerInfo | undefined {
-    return this.workerNodes[workerNodeKey]?.info
+  protected getWorkerInfo (workerNodeKey: number): WorkerInfo {
+    return this.workerNodes[workerNodeKey].info
   }
 
   /**
index 999d9c866cd46aebc168cadf9408dc86ec8c0c0d..5ec7cedc0f9dc0f2f4a1079302a94d515c5d4281 100644 (file)
@@ -162,8 +162,8 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
     return this.taskStatisticsRequirements.runTime.median
-      ? this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.median ?? 0
-      : this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.average ?? 0
+      ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
+      : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
   }
 
   /**
@@ -176,8 +176,8 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
     return this.taskStatisticsRequirements.waitTime.median
-      ? this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.median ?? 0
-      : this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.average ?? 0
+      ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
+      : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
   }
 
   /**
@@ -190,8 +190,8 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   protected getWorkerNodeTaskElu (workerNodeKey: number): number {
     return this.taskStatisticsRequirements.elu.median
-      ? this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.median ?? 0
-      : this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.average ?? 0
+      ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
+      : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
   }
 
   /**
index 91fbfc782579f83950482ba71019c9197e5e6348..984340b552f458c1f2d9704f2767dc39e329d649 100644 (file)
@@ -74,10 +74,10 @@ export class LeastBusyWorkerChoiceStrategy<
   private leastBusyNextWorkerNodeKey (): number | undefined {
     return this.pool.workerNodes.reduce(
       (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
-        return (workerNode.usage.runTime?.aggregate ?? 0) +
-          (workerNode.usage.waitTime?.aggregate ?? 0) <
-          (workerNodes[minWorkerNodeKey].usage.runTime?.aggregate ?? 0) +
-            (workerNodes[minWorkerNodeKey].usage.waitTime?.aggregate ?? 0)
+        return (workerNode.usage.runTime.aggregate ?? 0) +
+          (workerNode.usage.waitTime.aggregate ?? 0) <
+          (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0) +
+            (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0)
           ? workerNodeKey
           : minWorkerNodeKey
       },
index 67a1fdf387a5433999e0b1edcdd70988c6f20d5f..0fe9cbec0275513ee8d6feed443915cf77536f07 100644 (file)
@@ -70,8 +70,8 @@ export class LeastEluWorkerChoiceStrategy<
   private leastEluNextWorkerNodeKey (): number | undefined {
     return this.pool.workerNodes.reduce(
       (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
-        return (workerNode.usage.elu?.active?.aggregate ?? 0) <
-          (workerNodes[minWorkerNodeKey].usage.elu?.active?.aggregate ?? 0)
+        return (workerNode.usage.elu.active.aggregate ?? 0) <
+          (workerNodes[minWorkerNodeKey].usage.elu.active.aggregate ?? 0)
           ? workerNodeKey
           : minWorkerNodeKey
       },