| 1 | /** |
| 2 | * Task synchronous function that can be executed. |
| 3 | * |
| 4 | * @param data - Data sent to the worker. |
| 5 | * |
| 6 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 7 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 8 | */ |
| 9 | export type TaskSyncFunction<Data = unknown, Response = unknown> = ( |
| 10 | data?: Data |
| 11 | ) => Response |
| 12 | |
| 13 | /** |
| 14 | * Task asynchronous function that can be executed. |
| 15 | * This function must return a promise. |
| 16 | * |
| 17 | * @param data - Data sent to the worker. |
| 18 | * |
| 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 | * |
| 30 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 31 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 32 | */ |
| 33 | export type TaskFunction<Data = unknown, Response = unknown> = |
| 34 | | TaskSyncFunction<Data, Response> |
| 35 | | TaskAsyncFunction<Data, Response> |
| 36 | |
| 37 | /** |
| 38 | * Tasks functions that can be executed. |
| 39 | * This object can contain synchronous or asynchronous functions. |
| 40 | * The key is the name of the function. |
| 41 | * The value is the function itself. |
| 42 | * |
| 43 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 44 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 45 | */ |
| 46 | export type TaskFunctions<Data = unknown, Response = unknown> = Record< |
| 47 | string, |
| 48 | TaskFunction<Data, Response> |
| 49 | > |
| 50 | |
| 51 | /** |
| 52 | * Task function operation result. |
| 53 | */ |
| 54 | export interface TaskFunctionOperationResult { |
| 55 | status: boolean |
| 56 | error?: Error |
| 57 | } |