Commit | Line | Data |
---|---|---|
31847469 JB |
1 | import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js' |
2 | ||
b6b32453 | 3 | /** |
82ea6492 | 4 | * Task synchronous function that can be executed. |
09b75fef | 5 | * @param data - Data sent to the worker. |
77e8da5a | 6 | * @returns Execution response. |
e102732c JB |
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. | |
b6b32453 | 9 | */ |
82ea6492 | 10 | export type TaskSyncFunction<Data = unknown, Response = unknown> = ( |
b6b32453 JB |
11 | data?: Data |
12 | ) => Response | |
13 | ||
14 | /** | |
82ea6492 | 15 | * Task asynchronous function that can be executed. |
b6b32453 | 16 | * This function must return a promise. |
09b75fef | 17 | * @param data - Data sent to the worker. |
77e8da5a | 18 | * @returns Execution response promise. |
e102732c JB |
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. | |
b6b32453 | 21 | */ |
82ea6492 | 22 | export type TaskAsyncFunction<Data = unknown, Response = unknown> = ( |
b6b32453 JB |
23 | data?: Data |
24 | ) => Promise<Response> | |
25 | ||
26 | /** | |
82ea6492 | 27 | * Task function that can be executed. |
b6b32453 | 28 | * This function can be synchronous or asynchronous. |
e102732c JB |
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. | |
b6b32453 | 31 | */ |
82ea6492 | 32 | export type TaskFunction<Data = unknown, Response = unknown> = |
82ea6492 | 33 | | TaskAsyncFunction<Data, Response> |
97231086 | 34 | | TaskSyncFunction<Data, Response> |
b6b32453 | 35 | |
31847469 JB |
36 | /** |
37 | * Task function object. | |
31847469 JB |
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> { | |
31847469 JB |
42 | /** |
43 | * Task function priority. Lower values have higher priority. | |
44 | */ | |
45 | priority?: number | |
46 | /** | |
47 | * Task function worker choice strategy. | |
48 | */ | |
49 | strategy?: WorkerChoiceStrategy | |
97231086 JB |
50 | /** |
51 | * Task function. | |
52 | */ | |
53 | taskFunction: TaskFunction<Data, Response> | |
31847469 JB |
54 | } |
55 | ||
b6b32453 | 56 | /** |
82ea6492 | 57 | * Tasks functions that can be executed. |
31847469 | 58 | * The key is the name of the task function or task function object. |
181af286 | 59 | * The value is the task function or task function object. |
e102732c JB |
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. | |
b6b32453 JB |
62 | */ |
63 | export type TaskFunctions<Data = unknown, Response = unknown> = Record< | |
3a502712 JB |
64 | string, |
65 | TaskFunction<Data, Response> | TaskFunctionObject<Data, Response> | |
b6b32453 | 66 | > |
e81c38f2 JB |
67 | |
68 | /** | |
4e38fd21 | 69 | * Task function operation result. |
e81c38f2 | 70 | */ |
4e38fd21 | 71 | export interface TaskFunctionOperationResult { |
e81c38f2 | 72 | error?: Error |
97231086 | 73 | status: boolean |
e81c38f2 | 74 | } |