build: reenable eslint type checking
[poolifier.git] / src / pools / utils.ts
index c3ca698cac24da378dddf9451031a304b1a5c5a3..24f2cfb2a6d075b2133473bef04aa9a650607c5b 100644 (file)
@@ -4,7 +4,7 @@ import { env } from 'node:process'
 import {
   SHARE_ENV,
   Worker as ThreadWorker,
-  type WorkerOptions
+  type WorkerOptions,
 } from 'node:worker_threads'
 
 import type { MessageValue, Task } from '../utility-types.js'
@@ -13,7 +13,7 @@ import type { TasksQueueOptions } from './pool.js'
 import {
   type MeasurementStatisticsRequirements,
   WorkerChoiceStrategies,
-  type WorkerChoiceStrategy
+  type WorkerChoiceStrategy,
 } from './selection-strategies/selection-strategies-types.js'
 import type { WorkerChoiceStrategiesContext } from './selection-strategies/worker-choice-strategies-context.js'
 import {
@@ -23,7 +23,7 @@ import {
   type WorkerNodeOptions,
   type WorkerType,
   WorkerTypes,
-  type WorkerUsage
+  type WorkerUsage,
 } from './worker.js'
 
 /**
@@ -33,7 +33,7 @@ export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsR
   {
     aggregate: false,
     average: false,
-    median: false
+    median: false,
   }
 
 export const getDefaultTasksQueueOptions = (
@@ -44,7 +44,7 @@ export const getDefaultTasksQueueOptions = (
     concurrency: 1,
     taskStealing: true,
     tasksStealingOnBackPressure: false,
-    tasksFinishedTimeout: 2000
+    tasksFinishedTimeout: 2000,
   }
 }
 
@@ -89,7 +89,7 @@ export const checkDynamicPoolSize = (
 
 export const checkValidPriority = (priority: number | undefined): void => {
   if (priority != null && !Number.isSafeInteger(priority)) {
-    throw new TypeError(`Invalid property 'priority': '${priority}'`)
+    throw new TypeError(`Invalid property 'priority': '${priority.toString()}'`)
   }
   if (
     priority != null &&
@@ -130,7 +130,7 @@ export const checkValidTasksQueueOptions = (
     tasksQueueOptions.concurrency <= 0
   ) {
     throw new RangeError(
-      `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
+      `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency.toString()} is a negative integer or zero`
     )
   }
   if (
@@ -143,7 +143,7 @@ export const checkValidTasksQueueOptions = (
   }
   if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
     throw new RangeError(
-      `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
+      `Invalid worker node tasks queue size: ${tasksQueueOptions.size.toString()} is a negative integer or zero`
     )
   }
 }
@@ -169,7 +169,7 @@ export const checkWorkerNodeArguments = (
   }
   if (!isPlainObject(opts)) {
     throw new TypeError(
-      'Cannot construct a worker node with invalid options: must be a plain object'
+      'Cannot construct a worker node with invalid worker node options: must be a plain object'
     )
   }
   if (opts.tasksQueueBackPressureSize == null) {
@@ -202,11 +202,20 @@ export const checkWorkerNodeArguments = (
       'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
     )
   }
+  if (opts.tasksQueuePriority == null) {
+    throw new TypeError(
+      'Cannot construct a worker node without a tasks queue priority option'
+    )
+  }
+  if (typeof opts.tasksQueuePriority !== 'boolean') {
+    throw new TypeError(
+      'Cannot construct a worker node with a tasks queue priority option that is not a boolean'
+    )
+  }
 }
 
 /**
  * Updates the given measurement statistics.
- *
  * @param measurementStatistics - The measurement statistics to update.
  * @param measurementRequirements - The measurement statistics requirements.
  * @param measurementValue - The measurement value.
@@ -226,21 +235,25 @@ const updateMeasurementStatistics = (
       (measurementStatistics.aggregate ?? 0) + measurementValue
     measurementStatistics.minimum = min(
       measurementValue,
-      measurementStatistics.minimum ?? Infinity
+      measurementStatistics.minimum ?? Number.POSITIVE_INFINITY
     )
     measurementStatistics.maximum = max(
       measurementValue,
-      measurementStatistics.maximum ?? -Infinity
+      measurementStatistics.maximum ?? Number.NEGATIVE_INFINITY
     )
     if (measurementRequirements.average || measurementRequirements.median) {
-      measurementStatistics.history.push(measurementValue)
+      measurementStatistics.history.put(measurementValue)
       if (measurementRequirements.average) {
-        measurementStatistics.average = average(measurementStatistics.history)
+        measurementStatistics.average = average(
+          measurementStatistics.history.toArray()
+        )
       } else if (measurementStatistics.average != null) {
         delete measurementStatistics.average
       }
       if (measurementRequirements.median) {
-        measurementStatistics.median = median(measurementStatistics.history)
+        measurementStatistics.median = median(
+          measurementStatistics.history.toArray()
+        )
       } else if (measurementStatistics.median != null) {
         delete measurementStatistics.median
       }
@@ -355,13 +368,13 @@ export const updateEluWorkerUsage = <
 export const createWorker = <Worker extends IWorker>(
   type: WorkerType,
   filePath: string,
-  opts: { env?: Record<string, unknown>, workerOptions?: WorkerOptions }
+  opts: { env?: Record<string, unknown>; workerOptions?: WorkerOptions }
 ): Worker => {
   switch (type) {
     case WorkerTypes.thread:
       return new ThreadWorker(filePath, {
         env: SHARE_ENV,
-        ...opts.workerOptions
+        ...opts.workerOptions,
       }) as unknown as Worker
     case WorkerTypes.cluster:
       return cluster.fork(opts.env) as unknown as Worker
@@ -373,7 +386,6 @@ export const createWorker = <Worker extends IWorker>(
 
 /**
  * Returns the worker type of the given worker.
- *
  * @param worker - The worker to get the type of.
  * @returns The worker type of the given worker.
  * @internal
@@ -388,7 +400,6 @@ export const getWorkerType = (worker: IWorker): WorkerType | undefined => {
 
 /**
  * Returns the worker id of the given worker.
- *
  * @param worker - The worker to get the id of.
  * @returns The worker id of the given worker.
  * @internal