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