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