docs: refine task statistics description
[poolifier.git] / src / utility-types.ts
CommitLineData
fc3e6586
JB
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
62c15a68 3import type { EventLoopUtilization } from 'node:perf_hooks'
1a81f8af 4import type { KillBehavior } from './worker/worker-options'
02706357 5import type { IWorker, Task } from './pools/worker'
838898f1 6
729c563d 7/**
3832ad95 8 * Make all properties in T non-readonly.
b40ed511
JB
9 *
10 * @typeParam T - Type in which properties will be non-readonly.
729c563d 11 */
325f50bc
S
12export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
13
213e817d
JB
14/**
15 * Task error.
16 *
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 */
82f36766
JB
19export interface TaskError<Data = unknown> {
20 /**
21 * Error message.
22 */
23 message: string
24 /**
25 * Data passed to the worker triggering the error.
26 */
27 data?: Data
28}
29
d715b7bc
JB
30/**
31 * Task performance.
32 */
33export interface TaskPerformance {
34 /**
35 * Task performance timestamp.
36 */
37 timestamp: number
38 /**
39 * Task runtime.
40 */
41 runTime?: number
42 /**
43 * Task wait time.
44 */
45 waitTime?: number
46 /**
47 * Task event loop utilization.
48 */
49 elu?: EventLoopUtilization
50}
51
b6b32453
JB
52/**
53 * Performance statistics computation.
54 */
55export interface WorkerStatistics {
56 runTime: boolean
57 waitTime: boolean
58 elu: boolean
59}
60
729c563d 61/**
02706357 62 * Message object that is passed between main worker and worker.
c319c66b 63 *
34d68e4d 64 * @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
c319c66b
JB
65 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
66 * @typeParam MainWorker - Type of main worker.
71ebe93b 67 * @internal
729c563d 68 */
838898f1 69export interface MessageValue<
34d68e4d 70 MessageData = unknown,
838898f1 71 Data = unknown,
4c0ada52 72 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
34d68e4d 73> extends Task<MessageData> {
729c563d
S
74 /**
75 * Kill code.
76 */
1a81f8af 77 readonly kill?: KillBehavior | 1
729c563d 78 /**
91ee39ed 79 * Task error.
729c563d 80 */
34d68e4d 81 readonly taskError?: TaskError<Data>
bf9549ae 82 /**
d715b7bc 83 * Task performance.
62c15a68 84 */
d715b7bc 85 readonly taskPerformance?: TaskPerformance
729c563d
S
86 /**
87 * Reference to main worker.
729c563d 88 */
838898f1 89 readonly parent?: MainWorker
b6b32453
JB
90 /**
91 * Whether to compute the given statistics or not.
92 */
93 readonly statistics?: WorkerStatistics
325f50bc 94}
be0676b3
APA
95
96/**
2740a743 97 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 98 *
ae94ec4d 99 * @typeParam Worker - Type of worker.
2740a743 100 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 101 * @internal
be0676b3 102 */
c923ce56 103export interface PromiseResponseWrapper<
f06e48d8 104 Worker extends IWorker,
c923ce56
JB
105 Response = unknown
106> {
be0676b3
APA
107 /**
108 * Resolve callback to fulfill the promise.
109 */
110 readonly resolve: (value: Response) => void
111 /**
112 * Reject callback to reject the promise.
113 */
114 readonly reject: (reason?: string) => void
115 /**
a3445496 116 * The worker handling the execution.
be0676b3 117 */
c923ce56 118 readonly worker: Worker
be0676b3 119}