| 1 | import type { EventLoopUtilization } from 'node:perf_hooks' |
| 2 | import type { KillBehavior } from './worker/worker-options' |
| 3 | import type { IWorker, Task } from './pools/worker' |
| 4 | |
| 5 | /** |
| 6 | * Task error. |
| 7 | * |
| 8 | * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. |
| 9 | */ |
| 10 | export interface TaskError<Data = unknown> { |
| 11 | /** |
| 12 | * Error message. |
| 13 | */ |
| 14 | message: string |
| 15 | /** |
| 16 | * Data passed to the worker triggering the error. |
| 17 | */ |
| 18 | data?: Data |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Task performance. |
| 23 | */ |
| 24 | export interface TaskPerformance { |
| 25 | /** |
| 26 | * Task performance timestamp. |
| 27 | */ |
| 28 | timestamp: number |
| 29 | /** |
| 30 | * Task runtime. |
| 31 | */ |
| 32 | runTime?: number |
| 33 | /** |
| 34 | * Task event loop utilization. |
| 35 | */ |
| 36 | elu?: EventLoopUtilization |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Performance statistics computation. |
| 41 | */ |
| 42 | export interface WorkerStatistics { |
| 43 | runTime: boolean |
| 44 | elu: boolean |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Message object that is passed between main worker and worker. |
| 49 | * |
| 50 | * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data. |
| 51 | * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. |
| 52 | * @internal |
| 53 | */ |
| 54 | export interface MessageValue<Data = unknown, ErrorData = unknown> |
| 55 | extends Task<Data> { |
| 56 | /** |
| 57 | * Kill code. |
| 58 | */ |
| 59 | readonly kill?: KillBehavior | 1 |
| 60 | /** |
| 61 | * Task error. |
| 62 | */ |
| 63 | readonly taskError?: TaskError<ErrorData> |
| 64 | /** |
| 65 | * Task performance. |
| 66 | */ |
| 67 | readonly taskPerformance?: TaskPerformance |
| 68 | /** |
| 69 | * Whether the worker computes the given statistics or not. |
| 70 | */ |
| 71 | readonly statistics?: WorkerStatistics |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * An object holding the execution response promise resolve/reject callbacks. |
| 76 | * |
| 77 | * @typeParam Worker - Type of worker. |
| 78 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 79 | * @internal |
| 80 | */ |
| 81 | export interface PromiseResponseWrapper< |
| 82 | Worker extends IWorker, |
| 83 | Response = unknown |
| 84 | > { |
| 85 | /** |
| 86 | * Resolve callback to fulfill the promise. |
| 87 | */ |
| 88 | readonly resolve: (value: Response) => void |
| 89 | /** |
| 90 | * Reject callback to reject the promise. |
| 91 | */ |
| 92 | readonly reject: (reason?: string) => void |
| 93 | /** |
| 94 | * The worker handling the execution. |
| 95 | */ |
| 96 | readonly worker: Worker |
| 97 | } |