Merge branch 'master' into worker-info
[poolifier.git] / src / utility-types.ts
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 * Worker id.
58 */
59 readonly workerId?: number
60 /**
61 * Kill code.
62 */
63 readonly kill?: KillBehavior | 1
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 has started or not.
78 */
79 readonly started?: boolean
80 }
81
82 /**
83 * An object holding the execution response promise resolve/reject callbacks.
84 *
85 * @typeParam Worker - Type of worker.
86 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
87 * @internal
88 */
89 export interface PromiseResponseWrapper<
90 Worker extends IWorker,
91 Response = unknown
92 > {
93 /**
94 * Resolve callback to fulfill the promise.
95 */
96 readonly resolve: (value: Response) => void
97 /**
98 * Reject callback to reject the promise.
99 */
100 readonly reject: (reason?: string) => void
101 /**
102 * The worker handling the execution.
103 */
104 readonly worker: Worker
105 }