refactor: cleanup updateTaskStolenStatisticsWorkerUsage() signature
[poolifier.git] / src / utility-types.ts
... / ...
CommitLineData
1import type { EventLoopUtilization } from 'node:perf_hooks'
2import type { MessagePort, TransferListItem } from 'node:worker_threads'
3import type { KillBehavior } from './worker/worker-options'
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 */
10export interface TaskError<Data = unknown> {
11 /**
12 * Task name triggering the error.
13 */
14 readonly name: string
15 /**
16 * Error message.
17 */
18 readonly message: string
19 /**
20 * Data triggering the error.
21 */
22 readonly data?: Data
23}
24
25/**
26 * Task performance.
27 *
28 * @internal
29 */
30export interface TaskPerformance {
31 /**
32 * Task name.
33 */
34 readonly name: string
35 /**
36 * Task performance timestamp.
37 */
38 readonly timestamp: number
39 /**
40 * Task runtime.
41 */
42 readonly runTime?: number
43 /**
44 * Task event loop utilization.
45 */
46 readonly elu?: EventLoopUtilization
47}
48
49/**
50 * Performance statistics computation.
51 *
52 * @internal
53 */
54export interface WorkerStatistics {
55 runTime: boolean
56 elu: boolean
57}
58
59/**
60 * Message object that is passed as a task between main worker and worker.
61 *
62 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
63 * @internal
64 */
65export interface Task<Data = unknown> {
66 /**
67 * Worker id.
68 */
69 readonly workerId: number
70 /**
71 * Task name.
72 */
73 readonly name?: string
74 /**
75 * Task input data that will be passed to the worker.
76 */
77 readonly data?: Data
78 /**
79 * Array of transferable objects.
80 */
81 readonly transferList?: TransferListItem[]
82 /**
83 * Timestamp.
84 */
85 readonly timestamp?: number
86 /**
87 * Task UUID.
88 */
89 readonly taskId?: string
90}
91
92/**
93 * Message object that is passed between main worker and worker.
94 *
95 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
96 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
97 * @internal
98 */
99export interface MessageValue<Data = unknown, ErrorData = unknown>
100 extends Task<Data> {
101 /**
102 * Kill code.
103 */
104 readonly kill?: KillBehavior | true | 'success' | 'failure'
105 /**
106 * Task error.
107 */
108 readonly taskError?: TaskError<ErrorData>
109 /**
110 * Task performance.
111 */
112 readonly taskPerformance?: TaskPerformance
113 /**
114 * Task function names.
115 */
116 readonly taskFunctions?: string[]
117 /**
118 * Whether the worker computes the given statistics or not.
119 */
120 readonly statistics?: WorkerStatistics
121 /**
122 * Whether the worker is ready or not.
123 */
124 readonly ready?: boolean
125 /**
126 * Whether the worker starts or stops its activity check.
127 */
128 readonly checkActive?: boolean
129 /**
130 * Message port.
131 */
132 readonly port?: MessagePort
133}
134
135/**
136 * An object holding the task execution response promise resolve/reject callbacks.
137 *
138 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
139 * @internal
140 */
141export interface PromiseResponseWrapper<Response = unknown> {
142 /**
143 * Resolve callback to fulfill the promise.
144 */
145 readonly resolve: (value: Response | PromiseLike<Response>) => void
146 /**
147 * Reject callback to reject the promise.
148 */
149 readonly reject: (reason?: unknown) => void
150 /**
151 * The worker node key executing the task.
152 */
153 readonly workerNodeKey: number
154}
155
156export type Writable<T> = { -readonly [P in keyof T]: T[P] }