perf: optimize task(s) stealing
[poolifier.git] / src / utils.ts
index c568a29a049eda2de26cf9bd99d63e613294bc3f..dd839bc491b622aa871b876424474d24abdfb44e 100644 (file)
@@ -1,6 +1,8 @@
 import { getRandomValues } from 'node:crypto'
 import * as os from 'node:os'
 
+import type { TaskFunctionProperties } from './utility-types.js'
+import type { TaskFunctionObject } from './worker/task-functions.js'
 import type { KillBehavior } from './worker/worker-options.js'
 
 /**
@@ -205,7 +207,7 @@ export const max = (...args: number[]): number =>
  * @internal
  */
 // eslint-disable-next-line @typescript-eslint/no-explicit-any
-export const once = <A extends any[], R, C>(
+export const once = <A extends any[], R, C extends ThisType<any>>(
   fn: (...args: A) => R,
   context: C
 ): ((...args: A) => R) => {
@@ -220,3 +222,18 @@ export const once = <A extends any[], R, C>(
     return result
   }
 }
+
+export const buildTaskFunctionProperties = <Data, Response>(
+  name: string,
+  taskFunctionObject: TaskFunctionObject<Data, Response> | undefined
+): TaskFunctionProperties => {
+  return {
+    name,
+    ...(taskFunctionObject?.priority != null && {
+      priority: taskFunctionObject.priority
+    }),
+    ...(taskFunctionObject?.strategy != null && {
+      strategy: taskFunctionObject.strategy
+    })
+  }
+}