0fdaa36e19c34e1ace772866e174c4d5b6905381
[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 { IPoolWorker } from './pools/pool-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 * Task runtime.
36 */
37 readonly taskRunTime?: number
38 /**
39 * Reference to main worker.
40 *
41 * Only for internal use.
42 */
43 readonly parent?: MainWorker
44 }
45
46 /**
47 * An object holding the execution response promise resolve/reject callbacks.
48 *
49 * @typeParam Response - Type of execution response. This can only be serializable data.
50 */
51 export interface PromiseResponseWrapper<
52 Worker extends IPoolWorker,
53 Response = unknown
54 > {
55 /**
56 * Resolve callback to fulfill the promise.
57 */
58 readonly resolve: (value: Response) => void
59 /**
60 * Reject callback to reject the promise.
61 */
62 readonly reject: (reason?: string) => void
63 /**
64 * The worker handling the promise.
65 */
66 readonly worker: Worker
67 }