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