Merge branch 'master' into elu-strategy
[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
729c563d 14/**
02706357 15 * Message object that is passed between main worker and worker.
c319c66b
JB
16 *
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam MainWorker - Type of main worker.
71ebe93b 19 * @internal
729c563d 20 */
838898f1
S
21export interface MessageValue<
22 Data = unknown,
4c0ada52 23 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
02706357 24> extends Task<Data> {
729c563d
S
25 /**
26 * Kill code.
27 */
1a81f8af 28 readonly kill?: KillBehavior | 1
729c563d 29 /**
91ee39ed 30 * Task error.
729c563d 31 */
325f50bc 32 readonly error?: string
91ee39ed
JB
33 /**
34 * Task data triggering task error.
35 */
36 readonly errorData?: unknown
bf9549ae 37 /**
aee46736 38 * Runtime.
bf9549ae 39 */
aee46736 40 readonly runTime?: number
09a6305f
JB
41 /**
42 * Wait time.
43 */
44 readonly waitTime?: number
62c15a68
JB
45 /**
46 * Event loop utilization.
47 */
48 readonly elu?: EventLoopUtilization
729c563d
S
49 /**
50 * Reference to main worker.
729c563d 51 */
838898f1 52 readonly parent?: MainWorker
325f50bc 53}
be0676b3 54
48ef9107 55/**
09564a83
JB
56 * Worker synchronous function that can be executed.
57 *
58 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
59 * @typeParam Response - Type of execution response. This can only be serializable data.
48ef9107
JB
60 */
61export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
62 data?: Data
63) => Response
70a4f5ea 64
09564a83
JB
65/**
66 * Worker asynchronous function that can be executed.
67 * This function must return a promise.
68 *
69 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
70 * @typeParam Response - Type of execution response. This can only be serializable data.
71 */
48ef9107
JB
72export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
73 data?: Data
74) => Promise<Response>
70a4f5ea 75
09564a83
JB
76/**
77 * Worker function that can be executed.
78 * This function can be synchronous or asynchronous.
79 *
80 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
81 * @typeParam Response - Type of execution response. This can only be serializable data.
82 */
48ef9107
JB
83export type WorkerFunction<Data = unknown, Response = unknown> =
84 | WorkerSyncFunction<Data, Response>
85 | WorkerAsyncFunction<Data, Response>
70a4f5ea 86
a86b6df1 87/**
70a4f5ea 88 * Worker functions that can be executed.
a86b6df1
JB
89 * This object can contain synchronous or asynchronous functions.
90 * The key is the name of the function.
91 * The value is the function itself.
92 *
93 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
94 * @typeParam Response - Type of execution response. This can only be serializable data.
95 */
96export type TaskFunctions<Data = unknown, Response = unknown> = Record<
97string,
98WorkerFunction<Data, Response>
99>
48ef9107 100
be0676b3 101/**
2740a743 102 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 103 *
ae94ec4d 104 * @typeParam Worker - Type of worker.
2740a743 105 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 106 * @internal
be0676b3 107 */
c923ce56 108export interface PromiseResponseWrapper<
f06e48d8 109 Worker extends IWorker,
c923ce56
JB
110 Response = unknown
111> {
be0676b3
APA
112 /**
113 * Resolve callback to fulfill the promise.
114 */
115 readonly resolve: (value: Response) => void
116 /**
117 * Reject callback to reject the promise.
118 */
119 readonly reject: (reason?: string) => void
120 /**
a3445496 121 * The worker handling the execution.
be0676b3 122 */
c923ce56 123 readonly worker: Worker
be0676b3 124}