From: Jérôme Benoit Date: Sun, 3 Sep 2023 12:34:56 +0000 (+0200) Subject: refactor: remove unneeded null checks X-Git-Tag: v2.6.41~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=46b0bb099fa5f4d822c5f271d24b4e9858c5b807;p=poolifier.git refactor: remove unneeded null checks Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 534cb490..0b56cbe4 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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 = { 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 } /** diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 999d9c86..5ec7cedc 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -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 } /** diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index 91fbfc78..984340b5 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -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 }, diff --git a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts index 67a1fdf3..0fe9cbec 100644 --- a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts @@ -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 },