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