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