refactor: use Array.from to build Array() from Map()
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 10 May 2024 14:08:21 +0000 (16:08 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 10 May 2024 14:08:21 +0000 (16:08 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/selection-strategies-utils.ts
src/pools/selection-strategies/worker-choice-strategies-context.ts

index 6e63f781dd0da7b1d0185dda8aa2b2243c451dc7..3c7f5f4df327afcce3aaf7ee7f7ee88c397e6513 100644 (file)
@@ -118,10 +118,10 @@ export const toggleMedianMeasurementStatisticsRequirements = (
 export const buildWorkerChoiceStrategiesPolicy = (
   workerChoiceStrategies: Map<WorkerChoiceStrategy, IWorkerChoiceStrategy>
 ): StrategyPolicy => {
-  const policies: StrategyPolicy[] = []
-  for (const workerChoiceStrategy of workerChoiceStrategies.values()) {
-    policies.push(workerChoiceStrategy.strategyPolicy)
-  }
+  const policies: StrategyPolicy[] = Array.from(
+    workerChoiceStrategies,
+    ([_, workerChoiceStrategy]) => workerChoiceStrategy.strategyPolicy
+  )
   return {
     dynamicWorkerUsage: policies.some(p => p.dynamicWorkerUsage),
     dynamicWorkerReady: policies.some(p => p.dynamicWorkerReady)
@@ -131,12 +131,11 @@ export const buildWorkerChoiceStrategiesPolicy = (
 export const buildWorkerChoiceStrategiesTaskStatisticsRequirements = (
   workerChoiceStrategies: Map<WorkerChoiceStrategy, IWorkerChoiceStrategy>
 ): TaskStatisticsRequirements => {
-  const taskStatisticsRequirements: TaskStatisticsRequirements[] = []
-  for (const workerChoiceStrategy of workerChoiceStrategies.values()) {
-    taskStatisticsRequirements.push(
+  const taskStatisticsRequirements: TaskStatisticsRequirements[] = Array.from(
+    workerChoiceStrategies,
+    ([_, workerChoiceStrategy]) =>
       workerChoiceStrategy.taskStatisticsRequirements
-    )
-  }
+  )
   return {
     runTime: {
       aggregate: taskStatisticsRequirements.some(r => r.runTime.aggregate),
index d088015acc1ad9b62fd57a1221871e42210418bb..185fed71287e668179e201d7ee79848463fd1d76 100644 (file)
@@ -137,11 +137,10 @@ export class WorkerChoiceStrategiesContext<
    * @returns `true` if the update is successful, `false` otherwise.
    */
   public update (workerNodeKey: number): boolean {
-    const res: boolean[] = []
-    for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
-      res.push(workerChoiceStrategy.update(workerNodeKey))
-    }
-    return res.every(r => r)
+    return Array.from(
+      this.workerChoiceStrategies,
+      ([_, workerChoiceStrategy]) => workerChoiceStrategy.update(workerNodeKey)
+    ).every(r => r)
   }
 
   /**
@@ -195,11 +194,10 @@ export class WorkerChoiceStrategiesContext<
    * @returns `true` if the removal is successful, `false` otherwise.
    */
   public remove (workerNodeKey: number): boolean {
-    const res: boolean[] = []
-    for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
-      res.push(workerChoiceStrategy.remove(workerNodeKey))
-    }
-    return res.every(r => r)
+    return Array.from(
+      this.workerChoiceStrategies,
+      ([_, workerChoiceStrategy]) => workerChoiceStrategy.remove(workerNodeKey)
+    ).every(r => r)
   }
 
   /**