build(deps-dev): apply updates
[poolifier.git] / src / worker / task-functions.ts
index 353b8b0c1f8b329d71c89e6a89bbbdd921d3d548..2fd951fb2ac03d71b820a1673561630f0c9d7736 100644 (file)
@@ -1,6 +1,9 @@
+import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js'
+
 /**
  * Task synchronous function that can be executed.
- *
+ * @param data - Data sent to the worker.
+ * @returns Execution response.
  * @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.
  */
@@ -11,7 +14,8 @@ export type TaskSyncFunction<Data = unknown, Response = unknown> = (
 /**
  * Task asynchronous function that can be executed.
  * This function must return a promise.
- *
+ * @param data - Data sent to the worker.
+ * @returns Execution response promise.
  * @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.
  */
@@ -22,7 +26,6 @@ export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
 /**
  * Task function that can be executed.
  * This function can be synchronous or asynchronous.
- *
  * @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.
  */
@@ -30,24 +33,42 @@ 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 task 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>
+  string,
+  TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
 >
 
 /**
- * Task function operation return type.
+ * Task function operation result.
  */
-export interface TaskFunctionOperationReturnType {
+export interface TaskFunctionOperationResult {
   status: boolean
   error?: Error
 }