refactor: cleanup message type namespace
[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
82f36766
JB
14export interface TaskError<Data = unknown> {
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
37 /**
38 * Task wait time.
39 */
40 waitTime?: number
41 /**
42 * Task event loop utilization.
43 */
44 elu?: EventLoopUtilization
45}
46
b6b32453
JB
47/**
48 * Performance statistics computation.
49 */
50export interface WorkerStatistics {
51 runTime: boolean
52 waitTime: boolean
53 elu: boolean
54}
55
729c563d 56/**
02706357 57 * Message object that is passed between main worker and worker.
c319c66b 58 *
34d68e4d 59 * @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
c319c66b
JB
60 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
61 * @typeParam MainWorker - Type of main worker.
71ebe93b 62 * @internal
729c563d 63 */
838898f1 64export interface MessageValue<
34d68e4d 65 MessageData = unknown,
838898f1 66 Data = unknown,
4c0ada52 67 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
34d68e4d 68> extends Task<MessageData> {
729c563d
S
69 /**
70 * Kill code.
71 */
1a81f8af 72 readonly kill?: KillBehavior | 1
729c563d 73 /**
91ee39ed 74 * Task error.
729c563d 75 */
34d68e4d 76 readonly taskError?: TaskError<Data>
bf9549ae 77 /**
d715b7bc 78 * Task performance.
62c15a68 79 */
d715b7bc 80 readonly taskPerformance?: TaskPerformance
729c563d
S
81 /**
82 * Reference to main worker.
729c563d 83 */
838898f1 84 readonly parent?: MainWorker
b6b32453
JB
85 /**
86 * Whether to compute the given statistics or not.
87 */
88 readonly statistics?: WorkerStatistics
325f50bc 89}
be0676b3
APA
90
91/**
2740a743 92 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 93 *
ae94ec4d 94 * @typeParam Worker - Type of worker.
2740a743 95 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 96 * @internal
be0676b3 97 */
c923ce56 98export interface PromiseResponseWrapper<
f06e48d8 99 Worker extends IWorker,
c923ce56
JB
100 Response = unknown
101> {
be0676b3
APA
102 /**
103 * Resolve callback to fulfill the promise.
104 */
105 readonly resolve: (value: Response) => void
106 /**
107 * Reject callback to reject the promise.
108 */
109 readonly reject: (reason?: string) => void
110 /**
a3445496 111 * The worker handling the execution.
be0676b3 112 */
c923ce56 113 readonly worker: Worker
be0676b3 114}