refactor: improve error messages
[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 10export interface TaskError<Data = unknown> {
7ae6fb74
JB
11 /**
12 * Worker id.
13 */
14 workerId: number
82f36766
JB
15 /**
16 * Error message.
17 */
18 message: string
19 /**
20 * Data passed to the worker triggering the error.
21 */
22 data?: Data
23}
24
d715b7bc
JB
25/**
26 * Task performance.
27 */
28export interface TaskPerformance {
29 /**
30 * Task performance timestamp.
31 */
32 timestamp: number
33 /**
34 * Task runtime.
35 */
36 runTime?: number
d715b7bc
JB
37 /**
38 * Task event loop utilization.
39 */
40 elu?: EventLoopUtilization
41}
42
b6b32453
JB
43/**
44 * Performance statistics computation.
45 */
46export interface WorkerStatistics {
47 runTime: boolean
b6b32453
JB
48 elu: boolean
49}
50
729c563d 51/**
02706357 52 * Message object that is passed between main worker and worker.
c319c66b 53 *
e102732c
JB
54 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
55 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
71ebe93b 56 * @internal
729c563d 57 */
6677a3d3
JB
58export interface MessageValue<Data = unknown, ErrorData = unknown>
59 extends Task<Data> {
f59e1027 60 /**
83fa0a36 61 * Worker id.
f59e1027
JB
62 */
63 readonly workerId?: number
729c563d
S
64 /**
65 * Kill code.
66 */
1a81f8af 67 readonly kill?: KillBehavior | 1
729c563d 68 /**
91ee39ed 69 * Task error.
729c563d 70 */
e102732c 71 readonly taskError?: TaskError<ErrorData>
bf9549ae 72 /**
d715b7bc 73 * Task performance.
62c15a68 74 */
d715b7bc 75 readonly taskPerformance?: TaskPerformance
b6b32453 76 /**
f59e1027 77 * Whether the worker computes the given statistics or not.
b6b32453
JB
78 */
79 readonly statistics?: WorkerStatistics
f59e1027
JB
80 /**
81 * Whether the worker has started or not.
82 */
83 readonly started?: boolean
325f50bc 84}
be0676b3
APA
85
86/**
2740a743 87 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 88 *
ae94ec4d 89 * @typeParam Worker - Type of worker.
e102732c 90 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
c319c66b 91 * @internal
be0676b3 92 */
c923ce56 93export interface PromiseResponseWrapper<
f06e48d8 94 Worker extends IWorker,
c923ce56
JB
95 Response = unknown
96> {
be0676b3
APA
97 /**
98 * Resolve callback to fulfill the promise.
99 */
100 readonly resolve: (value: Response) => void
101 /**
102 * Reject callback to reject the promise.
103 */
104 readonly reject: (reason?: string) => void
105 /**
a3445496 106 * The worker handling the execution.
be0676b3 107 */
c923ce56 108 readonly worker: Worker
be0676b3 109}