Merge dependabot/npm_and_yarn/types/node-20.12.10 into combined-prs-branch
[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 *
6 * @param data - Data sent to the worker.
7 * @returns Execution response.
8 *
9 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
10 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
11 */
12 export type TaskSyncFunction<Data = unknown, Response = unknown> = (
13 data?: Data
14 ) => Response
15
16 /**
17 * Task asynchronous function that can be executed.
18 * This function must return a promise.
19 *
20 * @param data - Data sent to the worker.
21 * @returns Execution response promise.
22 *
23 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
24 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
25 */
26 export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
27 data?: Data
28 ) => Promise<Response>
29
30 /**
31 * Task function that can be executed.
32 * This function can be synchronous or asynchronous.
33 *
34 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
35 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
36 */
37 export type TaskFunction<Data = unknown, Response = unknown> =
38 | TaskSyncFunction<Data, Response>
39 | TaskAsyncFunction<Data, Response>
40
41 /**
42 * Task function object.
43 *
44 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
45 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
46 */
47 export interface TaskFunctionObject<Data = unknown, Response = unknown> {
48 /**
49 * Task function.
50 */
51 taskFunction: TaskFunction<Data, Response>
52 /**
53 * Task function priority. Lower values have higher priority.
54 */
55 priority?: number
56 /**
57 * Task function worker choice strategy.
58 */
59 strategy?: WorkerChoiceStrategy
60 }
61
62 /**
63 * Tasks functions that can be executed.
64 * The key is the name of the task function or task function object.
65 * The value is the task function or task function object.
66 *
67 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
68 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
69 */
70 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
71 string,
72 TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
73 >
74
75 /**
76 * Task function operation result.
77 */
78 export interface TaskFunctionOperationResult {
79 status: boolean
80 error?: Error
81 }