feat: add task function properties support
[poolifier.git] / src / worker / task-functions.ts
index 5518043eb022857209401a32ecd0a0a9d18bcf30..8999b69decbe215d279ce09cce457e6b5bca633f 100644 (file)
@@ -1,3 +1,5 @@
+import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js'
+
 /**
  * Task synchronous function that can be executed.
  *
@@ -36,18 +38,38 @@ export type TaskFunction<Data = unknown, Response = unknown> =
   | TaskSyncFunction<Data, Response>
   | TaskAsyncFunction<Data, Response>
 
+/**
+ * Task function object.
+ *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
+ */
+export interface TaskFunctionObject<Data = unknown, Response = unknown> {
+  /**
+   * Task function.
+   */
+  taskFunction: TaskFunction<Data, Response>
+  /**
+   * Task function priority. Lower values have higher priority.
+   */
+  priority?: number
+  /**
+   * Task function worker choice strategy.
+   */
+  strategy?: WorkerChoiceStrategy
+}
+
 /**
  * Tasks functions that can be executed.
- * This object can contain synchronous or asynchronous functions.
- * The key is the name of the function.
- * The value is the function itself.
+ * The key is the name of the task function or task function object.
+ * The value is the function or task function object.
  *
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
  * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  */
 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
 string,
-TaskFunction<Data, Response>
+TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
 >
 
 /**