| 1 | /** |
| 2 | * Task synchronous function that can be executed. |
| 3 | * |
| 4 | * @param data - Data sent to the worker. |
| 5 | * @returns Execution response. |
| 6 | * |
| 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 | * |
| 18 | * @param data - Data sent to the worker. |
| 19 | * @returns Execution response promise. |
| 20 | * |
| 21 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 22 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 23 | */ |
| 24 | export type TaskAsyncFunction<Data = unknown, Response = unknown> = ( |
| 25 | data?: Data |
| 26 | ) => Promise<Response> |
| 27 | |
| 28 | /** |
| 29 | * Task function that can be executed. |
| 30 | * This function can be synchronous or asynchronous. |
| 31 | * |
| 32 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 33 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 34 | */ |
| 35 | export type TaskFunction<Data = unknown, Response = unknown> = |
| 36 | | TaskSyncFunction<Data, Response> |
| 37 | | TaskAsyncFunction<Data, Response> |
| 38 | |
| 39 | /** |
| 40 | * Tasks functions that can be executed. |
| 41 | * This object can contain synchronous or asynchronous functions. |
| 42 | * The key is the name of the function. |
| 43 | * The value is the function itself. |
| 44 | * |
| 45 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 46 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 47 | */ |
| 48 | export type TaskFunctions<Data = unknown, Response = unknown> = Record< |
| 49 | string, |
| 50 | TaskFunction<Data, Response> |
| 51 | > |
| 52 | |
| 53 | /** |
| 54 | * Task function operation result. |
| 55 | */ |
| 56 | export interface TaskFunctionOperationResult { |
| 57 | status: boolean |
| 58 | error?: Error |
| 59 | } |