docs: refine comments
[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 */
eb7bf744 14 readonly message: string
82f36766
JB
15 /**
16 * Data passed to the worker triggering the error.
17 */
eb7bf744 18 readonly data?: Data
82f36766
JB
19}
20
d715b7bc
JB
21/**
22 * Task performance.
f03062df
JB
23 *
24 * @internal
d715b7bc
JB
25 */
26export interface TaskPerformance {
27 /**
28 * Task performance timestamp.
29 */
eb7bf744 30 readonly timestamp: number
d715b7bc
JB
31 /**
32 * Task runtime.
33 */
eb7bf744 34 readonly runTime?: number
d715b7bc
JB
35 /**
36 * Task event loop utilization.
37 */
eb7bf744 38 readonly elu?: EventLoopUtilization
d715b7bc
JB
39}
40
b6b32453
JB
41/**
42 * Performance statistics computation.
f03062df
JB
43 *
44 * @internal
b6b32453
JB
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> {
729c563d
S
60 /**
61 * Kill code.
62 */
48487131 63 readonly kill?: KillBehavior | true
729c563d 64 /**
91ee39ed 65 * Task error.
729c563d 66 */
e102732c 67 readonly taskError?: TaskError<ErrorData>
bf9549ae 68 /**
d715b7bc 69 * Task performance.
62c15a68 70 */
d715b7bc 71 readonly taskPerformance?: TaskPerformance
b6b32453 72 /**
f59e1027 73 * Whether the worker computes the given statistics or not.
b6b32453
JB
74 */
75 readonly statistics?: WorkerStatistics
f59e1027 76 /**
7c8381eb 77 * Whether the worker is ready or not.
f59e1027 78 */
7c8381eb 79 readonly ready?: boolean
75d3401a 80 /**
48487131 81 * Whether the worker starts or stops its aliveness check.
75d3401a 82 */
48487131 83 readonly checkAlive?: 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}