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