docs: update benchmarks vs. external pools
[poolifier.git] / src / utility-types.ts
... / ...
CommitLineData
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
3import type { EventLoopUtilization } from 'node:perf_hooks'
4import type { KillBehavior } from './worker/worker-options'
5import type { IWorker, Task } from './pools/worker'
6
7/**
8 * Make all properties in T non-readonly.
9 *
10 * @typeParam T - Type in which properties will be non-readonly.
11 */
12export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
13
14/**
15 * Task error.
16 *
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 */
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
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 event loop utilization.
44 */
45 elu?: EventLoopUtilization
46}
47
48/**
49 * Performance statistics computation.
50 */
51export interface WorkerStatistics {
52 runTime: boolean
53 elu: boolean
54}
55
56/**
57 * Message object that is passed between main worker and worker.
58 *
59 * @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
60 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
61 * @typeParam MainWorker - Type of main worker.
62 * @internal
63 */
64export interface MessageValue<
65 MessageData = unknown,
66 Data = unknown,
67 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
68> extends Task<MessageData> {
69 /**
70 * Kill code.
71 */
72 readonly kill?: KillBehavior | 1
73 /**
74 * Task error.
75 */
76 readonly taskError?: TaskError<Data>
77 /**
78 * Task performance.
79 */
80 readonly taskPerformance?: TaskPerformance
81 /**
82 * Reference to main worker.
83 */
84 readonly parent?: MainWorker
85 /**
86 * Whether to compute the given statistics or not.
87 */
88 readonly statistics?: WorkerStatistics
89}
90
91/**
92 * An object holding the execution response promise resolve/reject callbacks.
93 *
94 * @typeParam Worker - Type of worker.
95 * @typeParam Response - Type of execution response. This can only be serializable data.
96 * @internal
97 */
98export interface PromiseResponseWrapper<
99 Worker extends IWorker,
100 Response = unknown
101> {
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 /**
111 * The worker handling the execution.
112 */
113 readonly worker: Worker
114}