Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / utility-types.ts
CommitLineData
62c15a68 1import type { EventLoopUtilization } from 'node:perf_hooks'
1a81f8af 2import type { KillBehavior } from './worker/worker-options'
02706357 3import type { IWorker, Task } from './pools/worker'
838898f1 4
213e817d
JB
5/**
6 * Task error.
7 *
e102732c 8 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
213e817d 9 */
82f36766 10export interface TaskError<Data = unknown> {
7ae6fb74
JB
11 /**
12 * Worker id.
13 */
eb7bf744 14 readonly workerId: number
82f36766
JB
15 /**
16 * Error message.
17 */
eb7bf744 18 readonly message: string
82f36766
JB
19 /**
20 * Data passed to the worker triggering the error.
21 */
eb7bf744 22 readonly data?: Data
82f36766
JB
23}
24
d715b7bc
JB
25/**
26 * Task performance.
f03062df
JB
27 *
28 * @internal
d715b7bc
JB
29 */
30export interface TaskPerformance {
31 /**
32 * Task performance timestamp.
33 */
eb7bf744 34 readonly timestamp: number
d715b7bc
JB
35 /**
36 * Task runtime.
37 */
eb7bf744 38 readonly runTime?: number
d715b7bc
JB
39 /**
40 * Task event loop utilization.
41 */
eb7bf744 42 readonly elu?: EventLoopUtilization
d715b7bc
JB
43}
44
b6b32453
JB
45/**
46 * Performance statistics computation.
f03062df
JB
47 *
48 * @internal
b6b32453
JB
49 */
50export interface WorkerStatistics {
51 runTime: boolean
b6b32453
JB
52 elu: boolean
53}
54
729c563d 55/**
02706357 56 * Message object that is passed between main worker and worker.
c319c66b 57 *
e102732c
JB
58 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
59 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
71ebe93b 60 * @internal
729c563d 61 */
6677a3d3
JB
62export interface MessageValue<Data = unknown, ErrorData = unknown>
63 extends Task<Data> {
f59e1027 64 /**
83fa0a36 65 * Worker id.
f59e1027
JB
66 */
67 readonly workerId?: number
729c563d
S
68 /**
69 * Kill code.
70 */
1a81f8af 71 readonly kill?: KillBehavior | 1
729c563d 72 /**
91ee39ed 73 * Task error.
729c563d 74 */
e102732c 75 readonly taskError?: TaskError<ErrorData>
bf9549ae 76 /**
d715b7bc 77 * Task performance.
62c15a68 78 */
d715b7bc 79 readonly taskPerformance?: TaskPerformance
b6b32453 80 /**
f59e1027 81 * Whether the worker computes the given statistics or not.
b6b32453
JB
82 */
83 readonly statistics?: WorkerStatistics
f59e1027
JB
84 /**
85 * Whether the worker has started or not.
86 */
87 readonly started?: boolean
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.
e102732c 94 * @typeParam Response - Type of execution response. This can only be structured-cloneable 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}