Commit | Line | Data |
---|---|---|
62c15a68 | 1 | import type { EventLoopUtilization } from 'node:perf_hooks' |
7d91a8cd | 2 | import type { MessagePort, TransferListItem } from 'node:worker_threads' |
1a81f8af | 3 | import type { KillBehavior } from './worker/worker-options' |
838898f1 | 4 | |
213e817d JB |
5 | /** |
6 | * Task error. | |
7 | * | |
e102732c | 8 | * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. |
213e817d | 9 | */ |
82f36766 | 10 | export interface TaskError<Data = unknown> { |
ff128cc9 JB |
11 | /** |
12 | * Task name triggering the error. | |
13 | */ | |
14 | readonly name: string | |
82f36766 JB |
15 | /** |
16 | * Error message. | |
17 | */ | |
eb7bf744 | 18 | readonly message: string |
82f36766 | 19 | /** |
ff128cc9 | 20 | * Data triggering the error. |
82f36766 | 21 | */ |
eb7bf744 | 22 | readonly data?: Data |
82f36766 JB |
23 | } |
24 | ||
d715b7bc JB |
25 | /** |
26 | * Task performance. | |
f03062df JB |
27 | * |
28 | * @internal | |
d715b7bc JB |
29 | */ |
30 | export interface TaskPerformance { | |
197b4aa5 JB |
31 | /** |
32 | * Task name. | |
33 | */ | |
34 | readonly name: string | |
d715b7bc JB |
35 | /** |
36 | * Task performance timestamp. | |
37 | */ | |
eb7bf744 | 38 | readonly timestamp: number |
d715b7bc JB |
39 | /** |
40 | * Task runtime. | |
41 | */ | |
eb7bf744 | 42 | readonly runTime?: number |
d715b7bc JB |
43 | /** |
44 | * Task event loop utilization. | |
45 | */ | |
eb7bf744 | 46 | readonly elu?: EventLoopUtilization |
d715b7bc JB |
47 | } |
48 | ||
b6b32453 JB |
49 | /** |
50 | * Performance statistics computation. | |
f03062df JB |
51 | * |
52 | * @internal | |
b6b32453 JB |
53 | */ |
54 | export interface WorkerStatistics { | |
55 | runTime: boolean | |
b6b32453 JB |
56 | elu: boolean |
57 | } | |
58 | ||
5c4d16da JB |
59 | /** |
60 | * Message object that is passed as a task between main worker and worker. | |
61 | * | |
62 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. | |
63 | * @internal | |
64 | */ | |
65 | export interface Task<Data = unknown> { | |
66 | /** | |
67 | * Worker id. | |
68 | */ | |
69 | readonly workerId: number | |
70 | /** | |
71 | * Task name. | |
72 | */ | |
73 | readonly name?: string | |
74 | /** | |
75 | * Task input data that will be passed to the worker. | |
76 | */ | |
77 | readonly data?: Data | |
7d91a8cd JB |
78 | /** |
79 | * Array of transferable objects. | |
80 | */ | |
81 | readonly transferList?: TransferListItem[] | |
5c4d16da JB |
82 | /** |
83 | * Timestamp. | |
84 | */ | |
85 | readonly timestamp?: number | |
86 | /** | |
7629bdf1 | 87 | * Task UUID. |
5c4d16da | 88 | */ |
7629bdf1 | 89 | readonly taskId?: string |
5c4d16da JB |
90 | } |
91 | ||
729c563d | 92 | /** |
02706357 | 93 | * Message object that is passed between main worker and worker. |
c319c66b | 94 | * |
e102732c JB |
95 | * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data. |
96 | * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. | |
71ebe93b | 97 | * @internal |
729c563d | 98 | */ |
6677a3d3 JB |
99 | export interface MessageValue<Data = unknown, ErrorData = unknown> |
100 | extends Task<Data> { | |
729c563d S |
101 | /** |
102 | * Kill code. | |
103 | */ | |
1e3214b6 | 104 | readonly kill?: KillBehavior | true | 'success' | 'failure' |
729c563d | 105 | /** |
91ee39ed | 106 | * Task error. |
729c563d | 107 | */ |
e102732c | 108 | readonly taskError?: TaskError<ErrorData> |
bf9549ae | 109 | /** |
d715b7bc | 110 | * Task performance. |
62c15a68 | 111 | */ |
d715b7bc | 112 | readonly taskPerformance?: TaskPerformance |
90d7d101 JB |
113 | /** |
114 | * Task function names. | |
115 | */ | |
116 | readonly taskFunctions?: string[] | |
b6b32453 | 117 | /** |
f59e1027 | 118 | * Whether the worker computes the given statistics or not. |
b6b32453 JB |
119 | */ |
120 | readonly statistics?: WorkerStatistics | |
f59e1027 | 121 | /** |
7c8381eb | 122 | * Whether the worker is ready or not. |
f59e1027 | 123 | */ |
7c8381eb | 124 | readonly ready?: boolean |
75d3401a | 125 | /** |
b0a4db63 | 126 | * Whether the worker starts or stops its activity check. |
75d3401a | 127 | */ |
b0a4db63 | 128 | readonly checkActive?: boolean |
85aeb3f3 JB |
129 | /** |
130 | * Message port. | |
131 | */ | |
132 | readonly port?: MessagePort | |
325f50bc | 133 | } |
be0676b3 APA |
134 | |
135 | /** | |
e5d3809a | 136 | * An object holding the task execution response promise resolve/reject callbacks. |
be0676b3 | 137 | * |
e102732c | 138 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
c319c66b | 139 | * @internal |
be0676b3 | 140 | */ |
501aea93 | 141 | export interface PromiseResponseWrapper<Response = unknown> { |
be0676b3 APA |
142 | /** |
143 | * Resolve callback to fulfill the promise. | |
144 | */ | |
d2c73f82 | 145 | readonly resolve: (value: Response | PromiseLike<Response>) => void |
be0676b3 APA |
146 | /** |
147 | * Reject callback to reject the promise. | |
148 | */ | |
d2c73f82 | 149 | readonly reject: (reason?: unknown) => void |
be0676b3 | 150 | /** |
e5d3809a | 151 | * The worker node key executing the task. |
be0676b3 | 152 | */ |
501aea93 | 153 | readonly workerNodeKey: number |
be0676b3 | 154 | } |
ff3f866a JB |
155 | |
156 | export type Writable<T> = { -readonly [P in keyof T]: T[P] } |