Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / utility-types.ts
CommitLineData
62c15a68 1import type { EventLoopUtilization } from 'node:perf_hooks'
85aeb3f3 2import type { MessagePort } from 'node:worker_threads'
1a81f8af 3import type { KillBehavior } from './worker/worker-options'
5c4d16da 4import type { IWorker } from './pools/worker'
838898f1 5
213e817d
JB
6/**
7 * Task error.
8 *
e102732c 9 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
213e817d 10 */
82f36766 11export interface TaskError<Data = unknown> {
ff128cc9
JB
12 /**
13 * Task name triggering the error.
14 */
15 readonly name: string
82f36766
JB
16 /**
17 * Error message.
18 */
eb7bf744 19 readonly message: string
82f36766 20 /**
ff128cc9 21 * Data triggering the error.
82f36766 22 */
eb7bf744 23 readonly data?: Data
82f36766
JB
24}
25
d715b7bc
JB
26/**
27 * Task performance.
f03062df
JB
28 *
29 * @internal
d715b7bc
JB
30 */
31export interface TaskPerformance {
197b4aa5
JB
32 /**
33 * Task name.
34 */
35 readonly name: string
d715b7bc
JB
36 /**
37 * Task performance timestamp.
38 */
eb7bf744 39 readonly timestamp: number
d715b7bc
JB
40 /**
41 * Task runtime.
42 */
eb7bf744 43 readonly runTime?: number
d715b7bc
JB
44 /**
45 * Task event loop utilization.
46 */
eb7bf744 47 readonly elu?: EventLoopUtilization
d715b7bc
JB
48}
49
b6b32453
JB
50/**
51 * Performance statistics computation.
f03062df
JB
52 *
53 * @internal
b6b32453
JB
54 */
55export interface WorkerStatistics {
56 runTime: boolean
b6b32453
JB
57 elu: boolean
58}
59
5c4d16da
JB
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 */
66export 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
729c563d 89/**
02706357 90 * Message object that is passed between main worker and worker.
c319c66b 91 *
e102732c
JB
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.
71ebe93b 94 * @internal
729c563d 95 */
6677a3d3
JB
96export interface MessageValue<Data = unknown, ErrorData = unknown>
97 extends Task<Data> {
729c563d
S
98 /**
99 * Kill code.
100 */
48487131 101 readonly kill?: KillBehavior | true
729c563d 102 /**
91ee39ed 103 * Task error.
729c563d 104 */
e102732c 105 readonly taskError?: TaskError<ErrorData>
bf9549ae 106 /**
d715b7bc 107 * Task performance.
62c15a68 108 */
d715b7bc 109 readonly taskPerformance?: TaskPerformance
b6b32453 110 /**
f59e1027 111 * Whether the worker computes the given statistics or not.
b6b32453
JB
112 */
113 readonly statistics?: WorkerStatistics
f59e1027 114 /**
7c8381eb 115 * Whether the worker is ready or not.
f59e1027 116 */
7c8381eb 117 readonly ready?: boolean
75d3401a 118 /**
b0a4db63 119 * Whether the worker starts or stops its activity check.
75d3401a 120 */
b0a4db63 121 readonly checkActive?: boolean
85aeb3f3
JB
122 /**
123 * Message port.
124 */
125 readonly port?: MessagePort
325f50bc 126}
be0676b3
APA
127
128/**
2740a743 129 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 130 *
ae94ec4d 131 * @typeParam Worker - Type of worker.
e102732c 132 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
c319c66b 133 * @internal
be0676b3 134 */
c923ce56 135export interface PromiseResponseWrapper<
f06e48d8 136 Worker extends IWorker,
c923ce56
JB
137 Response = unknown
138> {
be0676b3
APA
139 /**
140 * Resolve callback to fulfill the promise.
141 */
d2c73f82 142 readonly resolve: (value: Response | PromiseLike<Response>) => void
be0676b3
APA
143 /**
144 * Reject callback to reject the promise.
145 */
d2c73f82 146 readonly reject: (reason?: unknown) => void
be0676b3 147 /**
a3445496 148 * The worker handling the execution.
be0676b3 149 */
c923ce56 150 readonly worker: Worker
be0676b3 151}