build(deps-dev): apply updates
[poolifier.git] / src / worker / task-functions.ts
1 import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js'
2
3 /**
4 * Task synchronous function that can be executed.
5 * @param data - Data sent to the worker.
6 * @returns Execution response.
7 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
8 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
9 */
10 export type TaskSyncFunction<Data = unknown, Response = unknown> = (
11 data?: Data
12 ) => Response
13
14 /**
15 * Task asynchronous function that can be executed.
16 * This function must return a promise.
17 * @param data - Data sent to the worker.
18 * @returns Execution response promise.
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
21 */
22 export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
23 data?: Data
24 ) => Promise<Response>
25
26 /**
27 * Task function that can be executed.
28 * This function can be synchronous or asynchronous.
29 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
30 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
31 */
32 export type TaskFunction<Data = unknown, Response = unknown> =
33 | TaskSyncFunction<Data, Response>
34 | TaskAsyncFunction<Data, Response>
35
36 /**
37 * Task function object.
38 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
39 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
40 */
41 export interface TaskFunctionObject<Data = unknown, Response = unknown> {
42 /**
43 * Task function.
44 */
45 taskFunction: TaskFunction<Data, Response>
46 /**
47 * Task function priority. Lower values have higher priority.
48 */
49 priority?: number
50 /**
51 * Task function worker choice strategy.
52 */
53 strategy?: WorkerChoiceStrategy
54 }
55
56 /**
57 * Tasks functions that can be executed.
58 * The key is the name of the task function or task function object.
59 * The value is the task function or task function object.
60 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
61 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
62 */
63 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
64 string,
65 TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
66 >
67
68 /**
69 * Task function operation result.
70 */
71 export interface TaskFunctionOperationResult {
72 status: boolean
73 error?: Error
74 }