docs: enhance documentation
[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 } from './pools/worker'
5
6 /**
7 * Make all properties in T non-readonly.
8 */
9 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
10
11 /**
12 * Message object that is passed between worker and main worker.
13 *
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam MainWorker - Type of main worker.
16 */
17 export interface MessageValue<
18 Data = unknown,
19 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
20 > {
21 /**
22 * Input data that will be passed to the worker.
23 */
24 readonly data?: Data
25 /**
26 * Id of the message.
27 */
28 readonly id?: string
29 /**
30 * Kill code.
31 */
32 readonly kill?: KillBehavior | 1
33 /**
34 * Error.
35 */
36 readonly error?: string
37 /**
38 * Runtime.
39 */
40 readonly runTime?: number
41 /**
42 * Reference to main worker.
43 *
44 * Only for internal use.
45 * @internal
46 */
47 readonly parent?: MainWorker
48 }
49
50 /**
51 * An object holding the execution response promise resolve/reject callbacks.
52 *
53 * @typeParam Worker - Type of worker.
54 * @typeParam Response - Type of execution response. This can only be serializable data.
55 * @internal
56 */
57 export interface PromiseResponseWrapper<
58 Worker extends IWorker,
59 Response = unknown
60 > {
61 /**
62 * Resolve callback to fulfill the promise.
63 */
64 readonly resolve: (value: Response) => void
65 /**
66 * Reject callback to reject the promise.
67 */
68 readonly reject: (reason?: string) => void
69 /**
70 * The worker handling the execution.
71 */
72 readonly worker: Worker
73 }