refactor: cleanup and type task error messages
[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
JB
58 *
59 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
60 * @typeParam MainWorker - Type of main worker.
71ebe93b 61 * @internal
729c563d 62 */
838898f1
S
63export interface MessageValue<
64 Data = unknown,
82f36766 65 ErrorData = unknown,
4c0ada52 66 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
02706357 67> extends Task<Data> {
729c563d
S
68 /**
69 * Kill code.
70 */
1a81f8af 71 readonly kill?: KillBehavior | 1
729c563d 72 /**
91ee39ed 73 * Task error.
729c563d 74 */
82f36766 75 readonly taskError?: TaskError<ErrorData>
bf9549ae 76 /**
d715b7bc 77 * Task performance.
62c15a68 78 */
d715b7bc 79 readonly taskPerformance?: TaskPerformance
729c563d
S
80 /**
81 * Reference to main worker.
729c563d 82 */
838898f1 83 readonly parent?: MainWorker
b6b32453
JB
84 /**
85 * Whether to compute the given statistics or not.
86 */
87 readonly statistics?: WorkerStatistics
325f50bc 88}
be0676b3
APA
89
90/**
2740a743 91 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 92 *
ae94ec4d 93 * @typeParam Worker - Type of worker.
2740a743 94 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 95 * @internal
be0676b3 96 */
c923ce56 97export interface PromiseResponseWrapper<
f06e48d8 98 Worker extends IWorker,
c923ce56
JB
99 Response = unknown
100> {
be0676b3
APA
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 /**
a3445496 110 * The worker handling the execution.
be0676b3 111 */
c923ce56 112 readonly worker: Worker
be0676b3 113}