refactor: factor out measurement statistics requirements default
[poolifier.git] / src / utility-types.ts
CommitLineData
62c15a68 1import type { EventLoopUtilization } from 'node:perf_hooks'
1a81f8af 2import type { KillBehavior } from './worker/worker-options'
02706357 3import type { IWorker, Task } from './pools/worker'
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
JB
10export 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
d715b7bc
JB
21/**
22 * Task performance.
23 */
24export interface TaskPerformance {
25 /**
26 * Task performance timestamp.
27 */
28 timestamp: number
29 /**
30 * Task runtime.
31 */
32 runTime?: number
d715b7bc
JB
33 /**
34 * Task event loop utilization.
35 */
36 elu?: EventLoopUtilization
37}
38
b6b32453
JB
39/**
40 * Performance statistics computation.
41 */
42export interface WorkerStatistics {
43 runTime: boolean
b6b32453
JB
44 elu: boolean
45}
46
729c563d 47/**
02706357 48 * Message object that is passed between main worker and worker.
c319c66b 49 *
e102732c
JB
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.
71ebe93b 52 * @internal
729c563d 53 */
6677a3d3
JB
54export interface MessageValue<Data = unknown, ErrorData = unknown>
55 extends Task<Data> {
729c563d
S
56 /**
57 * Kill code.
58 */
1a81f8af 59 readonly kill?: KillBehavior | 1
729c563d 60 /**
91ee39ed 61 * Task error.
729c563d 62 */
e102732c 63 readonly taskError?: TaskError<ErrorData>
bf9549ae 64 /**
d715b7bc 65 * Task performance.
62c15a68 66 */
d715b7bc 67 readonly taskPerformance?: TaskPerformance
b6b32453
JB
68 /**
69 * Whether to compute the given statistics or not.
70 */
71 readonly statistics?: WorkerStatistics
325f50bc 72}
be0676b3
APA
73
74/**
2740a743 75 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 76 *
ae94ec4d 77 * @typeParam Worker - Type of worker.
e102732c 78 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
c319c66b 79 * @internal
be0676b3 80 */
c923ce56 81export interface PromiseResponseWrapper<
f06e48d8 82 Worker extends IWorker,
c923ce56
JB
83 Response = unknown
84> {
be0676b3
APA
85 /**
86 * Resolve callback to fulfill the promise.
87 */
88 readonly resolve: (value: Response) => void
89 /**
90 * Reject callback to reject the promise.
91 */
92 readonly reject: (reason?: string) => void
93 /**
a3445496 94 * The worker handling the execution.
be0676b3 95 */
c923ce56 96 readonly worker: Worker
be0676b3 97}