Merge branch 'master' into waittime
[poolifier.git] / src / utility-types.ts
1 import type { Worker as ClusterWorker } from 'node:cluster'
2 import type { MessagePort } from 'node:worker_threads'
3 import type { KillBehavior } from './worker/worker-options'
4 import type { IWorker, Task } from './pools/worker'
5
6 /**
7 * Make all properties in T non-readonly.
8 *
9 * @typeParam T - Type in which properties will be non-readonly.
10 */
11 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
12
13 /**
14 * Message object that is passed between main worker and worker.
15 *
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam MainWorker - Type of main worker.
18 * @internal
19 */
20 export interface MessageValue<
21 Data = unknown,
22 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
23 > extends Task<Data> {
24 /**
25 * Kill code.
26 */
27 readonly kill?: KillBehavior | 1
28 /**
29 * Error.
30 */
31 readonly error?: string
32 /**
33 * Runtime.
34 */
35 readonly runTime?: number
36 /**
37 * Wait time.
38 */
39 readonly waitTime?: number
40 /**
41 * Reference to main worker.
42 */
43 readonly parent?: MainWorker
44 }
45
46 /**
47 * Worker synchronous function that can be executed.
48 *
49 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
50 * @typeParam Response - Type of execution response. This can only be serializable data.
51 */
52 export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
53 data?: Data
54 ) => Response
55
56 /**
57 * Worker asynchronous function that can be executed.
58 * This function must return a promise.
59 *
60 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
61 * @typeParam Response - Type of execution response. This can only be serializable data.
62 */
63 export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
64 data?: Data
65 ) => Promise<Response>
66
67 /**
68 * Worker function that can be executed.
69 * This function can be synchronous or asynchronous.
70 *
71 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
72 * @typeParam Response - Type of execution response. This can only be serializable data.
73 */
74 export type WorkerFunction<Data = unknown, Response = unknown> =
75 | WorkerSyncFunction<Data, Response>
76 | WorkerAsyncFunction<Data, Response>
77
78 /**
79 * Worker functions that can be executed.
80 * This object can contain synchronous or asynchronous functions.
81 * The key is the name of the function.
82 * The value is the function itself.
83 *
84 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
85 * @typeParam Response - Type of execution response. This can only be serializable data.
86 */
87 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
88 string,
89 WorkerFunction<Data, Response>
90 >
91
92 /**
93 * An object holding the execution response promise resolve/reject callbacks.
94 *
95 * @typeParam Worker - Type of worker.
96 * @typeParam Response - Type of execution response. This can only be serializable data.
97 * @internal
98 */
99 export interface PromiseResponseWrapper<
100 Worker extends IWorker,
101 Response = unknown
102 > {
103 /**
104 * Resolve callback to fulfill the promise.
105 */
106 readonly resolve: (value: Response) => void
107 /**
108 * Reject callback to reject the promise.
109 */
110 readonly reject: (reason?: string) => void
111 /**
112 * The worker handling the execution.
113 */
114 readonly worker: Worker
115 }