refactor: factor out worker helpers
[poolifier.git] / src / pools / utils.ts
index 4aac5cda47d361fd723198a53ad44f254235b56c..e534ead2363947b40096c2734517a1d7c1ea3550 100644 (file)
@@ -5,6 +5,7 @@ import {
   type WorkerChoiceStrategy
 } from './selection-strategies/selection-strategies-types'
 import type { TasksQueueOptions } from './pool'
+import type { IWorker } from './worker'
 
 export const checkFilePath = (filePath: string): void => {
   if (
@@ -90,3 +91,26 @@ export const checkValidTasksQueueOptions = (
     )
   }
 }
+export const checkWorkerNodeArguments = <Worker extends IWorker>(
+  worker: Worker,
+  tasksQueueBackPressureSize: number
+): void => {
+  if (worker == null) {
+    throw new TypeError('Cannot construct a worker node without a worker')
+  }
+  if (tasksQueueBackPressureSize == null) {
+    throw new TypeError(
+      'Cannot construct a worker node without a tasks queue back pressure size'
+    )
+  }
+  if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
+    throw new TypeError(
+      'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
+    )
+  }
+  if (tasksQueueBackPressureSize <= 0) {
+    throw new RangeError(
+      'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+    )
+  }
+}