feat: add task function properties support
[poolifier.git] / src / worker / utils.ts
index d8c4c3e905318b3c485991155c65cfdc03f269a7..486d92e77a73e8f3ac003b655b34e1a4b4615574 100644 (file)
@@ -1,5 +1,6 @@
+import { checkValidWorkerChoiceStrategy } from '../pools/utils.js'
 import { isPlainObject } from '../utils.js'
-import type { TaskFunction } from './task-functions.js'
+import type { TaskFunctionObject } from './task-functions.js'
 import { KillBehaviors, type WorkerOptions } from './worker-options.js'
 
 export const checkValidWorkerOptions = (
@@ -34,7 +35,7 @@ export const checkValidWorkerOptions = (
 
 export const checkValidTaskFunctionEntry = <Data = unknown, Response = unknown>(
   name: string,
-  fn: TaskFunction<Data, Response>
+  fnObj: TaskFunctionObject<Data, Response>
 ): void => {
   if (typeof name !== 'string') {
     throw new TypeError('A taskFunctions parameter object key is not a string')
@@ -44,11 +45,18 @@ export const checkValidTaskFunctionEntry = <Data = unknown, Response = unknown>(
       'A taskFunctions parameter object key is an empty string'
     )
   }
-  if (typeof fn !== 'function') {
+  if (typeof fnObj.taskFunction !== 'function') {
     throw new TypeError(
-      'A taskFunctions parameter object value is not a function'
+      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
+      `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function`
     )
   }
+  if (fnObj.priority != null && !Number.isSafeInteger(fnObj.priority)) {
+    throw new TypeError(
+      `taskFunction object 'priority' property '${fnObj.priority}' is not an integer`
+    )
+  }
+  checkValidWorkerChoiceStrategy(fnObj.strategy)
 }
 
 export const checkTaskFunctionName = (name: string): void => {