chore: v2.4.12
[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 * Reference to main worker.
38 */
39 readonly parent?: MainWorker
40 }
41
42 /**
43 * Worker synchronous function that can be executed.
44 *
45 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
46 * @typeParam Response - Type of execution response. This can only be serializable data.
47 */
48 export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
49 data?: Data
50 ) => Response
51
52 /**
53 * Worker asynchronous function that can be executed.
54 * This function must return a promise.
55 *
56 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
57 * @typeParam Response - Type of execution response. This can only be serializable data.
58 */
59 export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
60 data?: Data
61 ) => Promise<Response>
62
63 /**
64 * Worker function that can be executed.
65 * This function can be synchronous or asynchronous.
66 *
67 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
68 * @typeParam Response - Type of execution response. This can only be serializable data.
69 */
70 export type WorkerFunction<Data = unknown, Response = unknown> =
71 | WorkerSyncFunction<Data, Response>
72 | WorkerAsyncFunction<Data, Response>
73
74 /**
75 * Worker functions that can be executed.
76 * This object can contain synchronous or asynchronous functions.
77 * The key is the name of the function.
78 * The value is the function itself.
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 */
83 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
84 string,
85 WorkerFunction<Data, Response>
86 >
87
88 /**
89 * An object holding the execution response promise resolve/reject callbacks.
90 *
91 * @typeParam Worker - Type of worker.
92 * @typeParam Response - Type of execution response. This can only be serializable data.
93 * @internal
94 */
95 export interface PromiseResponseWrapper<
96 Worker extends IWorker,
97 Response = unknown
98 > {
99 /**
100 * Resolve callback to fulfill the promise.
101 */
102 readonly resolve: (value: Response) => void
103 /**
104 * Reject callback to reject the promise.
105 */
106 readonly reject: (reason?: string) => void
107 /**
108 * The worker handling the execution.
109 */
110 readonly worker: Worker
111 }