refactor: use named export in benchmarking code
[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
d715b7bc
JB
42 /**
43 * Task event loop utilization.
44 */
45 elu?: EventLoopUtilization
46}
47
b6b32453
JB
48/**
49 * Performance statistics computation.
50 */
51export interface WorkerStatistics {
52 runTime: boolean
b6b32453
JB
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}