refactor: flag some object literals as read only
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Nov 2024 14:29:56 +0000 (15:29 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Nov 2024 14:29:56 +0000 (15:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/least-busy-worker-choice-strategy.ts
src/pools/selection-strategies/least-elu-worker-choice-strategy.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
src/pools/utils.ts
src/worker/abstract-worker.ts

index 69cb0bbcb2277b092d54de8deae34610b2dd0da3..9d1a62e04fe6d7d6f1458022f4e635b095fb3010 100644 (file)
@@ -41,11 +41,12 @@ export abstract class AbstractWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
-    elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-    runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-    waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  }
+  public readonly taskStatisticsRequirements: TaskStatisticsRequirements =
+    Object.freeze({
+      elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+      runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+      waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+    })
 
   /**
    * Constructs a worker choice strategy bound to the pool.
index 23f751df54850f0251aa9a11ddfdf1e11d0fbf63..fc0dcc3baebdd1b373fafc4c8da457c8d50d5b3b 100644 (file)
@@ -25,7 +25,7 @@ export class FairShareWorkerChoiceStrategy<
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
   public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
-    {
+    Object.freeze({
       elu: {
         aggregate: true,
         average: true,
@@ -41,7 +41,7 @@ export class FairShareWorkerChoiceStrategy<
         average: true,
         median: false,
       },
-    }
+    })
 
   /** @inheritDoc */
   public constructor (
index 0db54c4fd7b6e7279da25512fa7f18c140402da8..fbc79718471d77748f3337ab2511e27d41bec974 100644 (file)
@@ -41,7 +41,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
   private workerNodeVirtualTaskExecutionTime = 0
   /** @inheritDoc */
   public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
-    {
+    Object.freeze({
       elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
       runTime: {
         aggregate: true,
@@ -53,7 +53,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
         average: true,
         median: false,
       },
-    }
+    })
 
   /** @inheritDoc */
   public constructor (
index 5392cdd2301471ed462d08f81bd4c34ce9448846..b6ad7075b10cc085f54e51fd8f7baa374cd834f5 100644 (file)
@@ -24,7 +24,7 @@ export class LeastBusyWorkerChoiceStrategy<
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
   public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
-    {
+    Object.freeze({
       elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
       runTime: {
         aggregate: true,
@@ -36,7 +36,7 @@ export class LeastBusyWorkerChoiceStrategy<
         average: false,
         median: false,
       },
-    }
+    })
 
   /** @inheritDoc */
   public constructor (
index 8b0548c404c35ad450c8196136402125cb3d31c2..37193a1cba4a1ae9eb23d1b439ac710a3b5d2b92 100644 (file)
@@ -24,7 +24,7 @@ export class LeastEluWorkerChoiceStrategy<
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
   public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
-    {
+    Object.freeze({
       elu: {
         aggregate: true,
         average: false,
@@ -32,7 +32,7 @@ export class LeastEluWorkerChoiceStrategy<
       },
       runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
       waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-    }
+    })
 
   /** @inheritDoc */
   public constructor (
index b3a4f1d855b628e7ead303baf85fc296b5d46d96..a62103a942b974d228a486995186a03e2db483f9 100644 (file)
@@ -30,7 +30,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
-    {
+    Object.freeze({
       elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
       runTime: {
         aggregate: true,
@@ -42,7 +42,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
         average: true,
         median: false,
       },
-    }
+    })
 
   /** @inheritDoc */
   public constructor (
index 3c88dd4588c2cdfcffc8f44eaa5aa31cfb4937e5..c8257eaf95e582f6dae6baec574b3093a5570b27 100644 (file)
@@ -30,24 +30,24 @@ import {
 /**
  * Default measurement statistics requirements.
  */
-export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsRequirements =
-  {
+export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: Readonly<MeasurementStatisticsRequirements> =
+  Object.freeze({
     aggregate: false,
     average: false,
     median: false,
-  }
+  })
 
 export const getDefaultTasksQueueOptions = (
   poolMaxSize: number
-): Required<TasksQueueOptions> => {
-  return {
+): Required<Readonly<TasksQueueOptions>> => {
+  return Object.freeze({
     concurrency: 1,
     size: Math.pow(poolMaxSize, 2),
     tasksFinishedTimeout: 2000,
     tasksStealingOnBackPressure: true,
     tasksStealingRatio: 0.6,
     taskStealing: true,
-  }
+  })
 }
 
 export const checkFilePath = (filePath: string | undefined): void => {
index c8aa5a05626be66c3ddb68363fd4bfe0e4b27739..1ea46a23f7b87d945792d838d4216a9fca8f58ed 100644 (file)
@@ -34,7 +34,7 @@ import {
 import { KillBehaviors, type WorkerOptions } from './worker-options.js'
 
 const DEFAULT_MAX_INACTIVE_TIME = 60000
-const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
+const DEFAULT_WORKER_OPTIONS: Readonly<WorkerOptions> = Object.freeze({
   /**
    * The kill behavior option on this worker or its default value.
    */
@@ -48,7 +48,7 @@ const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
    * The pool automatically checks and terminates this worker when the time expires.
    */
   maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME,
-}
+})
 
 /**
  * Base class that implements some shared logic for all poolifier workers.
@@ -68,7 +68,7 @@ export abstract class AbstractWorker<
   /**
    * Worker id.
    */
-  protected abstract id: number
+  protected abstract readonly id: number
   /**
    * Timestamp of the last task processed by this worker.
    */
@@ -194,7 +194,7 @@ export abstract class AbstractWorker<
     protected readonly isMain: boolean | undefined,
     private readonly mainWorker: MainWorker | null | undefined,
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
-    protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
+    protected readonly opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
   ) {
     if (this.isMain == null) {
       throw new Error('isMain parameter is mandatory')