544b4cd0a07e85f407a192466601167392167a41
[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 export interface MessageValue<
15 Data = unknown,
16 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
17 > {
18 /**
19 * Input data that will be passed to the worker.
20 */
21 readonly data?: Data
22 /**
23 * Id of the message.
24 */
25 readonly id?: string
26 /**
27 * Kill code.
28 */
29 readonly kill?: KillBehavior | 1
30 /**
31 * Error.
32 */
33 readonly error?: string
34 /**
35 * Runtime.
36 */
37 readonly runTime?: number
38 /**
39 * Reference to main worker.
40 *
41 * Only for internal use.
42 * @internal
43 */
44 readonly parent?: MainWorker
45 }
46
47 /**
48 * An object holding the execution response promise resolve/reject callbacks.
49 *
50 * @typeParam Worker - Type of worker.
51 * @typeParam Response - Type of execution response. This can only be serializable data.
52 */
53 export interface PromiseResponseWrapper<
54 Worker extends IWorker,
55 Response = unknown
56 > {
57 /**
58 * Resolve callback to fulfill the promise.
59 */
60 readonly resolve: (value: Response) => void
61 /**
62 * Reject callback to reject the promise.
63 */
64 readonly reject: (reason?: string) => void
65 /**
66 * The worker handling the execution.
67 */
68 readonly worker: Worker
69 }