refactor: cleanup interface scope
[poolifier.git] / src / utility-types.ts
1 import type { EventLoopUtilization } from 'node:perf_hooks'
2 import type { MessagePort, TransferListItem } from 'node:worker_threads'
3 import type { KillBehavior } from './worker/worker-options'
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 * Task name triggering the error.
13 */
14 readonly name: string
15 /**
16 * Error message.
17 */
18 readonly message: string
19 /**
20 * Data triggering the error.
21 */
22 readonly data?: Data
23 }
24
25 /**
26 * Task performance.
27 *
28 * @internal
29 */
30 export interface TaskPerformance {
31 /**
32 * Task name.
33 */
34 readonly name: string
35 /**
36 * Task performance timestamp.
37 */
38 readonly timestamp: number
39 /**
40 * Task runtime.
41 */
42 readonly runTime?: number
43 /**
44 * Task event loop utilization.
45 */
46 readonly elu?: EventLoopUtilization
47 }
48
49 /**
50 * Worker task performance statistics computation settings.
51 *
52 * @internal
53 */
54 export interface WorkerStatistics {
55 /**
56 * Whether the worker computes the task runtime or not.
57 */
58 readonly runTime: boolean
59 /**
60 * Whether the worker computes the task event loop utilization (ELU) or not.
61 */
62 readonly elu: boolean
63 }
64
65 /**
66 * Message object that is passed as a task between main worker and worker.
67 *
68 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
69 * @internal
70 */
71 export interface Task<Data = unknown> {
72 /**
73 * Worker id.
74 */
75 readonly workerId: number
76 /**
77 * Task name.
78 */
79 readonly name?: string
80 /**
81 * Task input data that will be passed to the worker.
82 */
83 readonly data?: Data
84 /**
85 * Array of transferable objects.
86 */
87 readonly transferList?: TransferListItem[]
88 /**
89 * Timestamp.
90 */
91 readonly timestamp?: number
92 /**
93 * Task UUID.
94 */
95 readonly taskId?: string
96 }
97
98 /**
99 * Message object that is passed between main worker and worker.
100 *
101 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
102 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
103 * @internal
104 */
105 export interface MessageValue<Data = unknown, ErrorData = unknown>
106 extends Task<Data> {
107 /**
108 * Kill code.
109 */
110 readonly kill?: KillBehavior | true | 'success' | 'failure'
111 /**
112 * Task error.
113 */
114 readonly taskError?: TaskError<ErrorData>
115 /**
116 * Task performance.
117 */
118 readonly taskPerformance?: TaskPerformance
119 /**
120 * Task function names.
121 */
122 readonly taskFunctions?: string[]
123 /**
124 * Whether the worker computes the given statistics or not.
125 */
126 readonly statistics?: WorkerStatistics
127 /**
128 * Whether the worker is ready or not.
129 */
130 readonly ready?: boolean
131 /**
132 * Whether the worker starts or stops its activity check.
133 */
134 readonly checkActive?: boolean
135 /**
136 * Message port.
137 */
138 readonly port?: MessagePort
139 }
140
141 /**
142 * An object holding the task execution response promise resolve/reject callbacks.
143 *
144 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
145 * @internal
146 */
147 export interface PromiseResponseWrapper<Response = unknown> {
148 /**
149 * Resolve callback to fulfill the promise.
150 */
151 readonly resolve: (value: Response | PromiseLike<Response>) => void
152 /**
153 * Reject callback to reject the promise.
154 */
155 readonly reject: (reason?: unknown) => void
156 /**
157 * The worker node key executing the task.
158 */
159 readonly workerNodeKey: number
160 }
161
162 export type Writable<T> = { -readonly [P in keyof T]: T[P] }