Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / utility-types.ts
CommitLineData
fc3e6586
JB
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
1a81f8af 3import type { KillBehavior } from './worker/worker-options'
f06e48d8 4import type { IWorker } from './pools/worker'
838898f1 5
729c563d 6/**
3832ad95 7 * Make all properties in T non-readonly.
729c563d 8 */
325f50bc
S
9export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
10
729c563d
S
11/**
12 * Message object that is passed between worker and main worker.
13 */
838898f1
S
14export interface MessageValue<
15 Data = unknown,
cc9b8d30 16 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
838898f1 17> {
729c563d
S
18 /**
19 * Input data that will be passed to the worker.
20 */
325f50bc 21 readonly data?: Data
729c563d 22 /**
e088a00c 23 * Id of the message.
729c563d 24 */
b4e75778 25 readonly id?: string
729c563d
S
26 /**
27 * Kill code.
28 */
1a81f8af 29 readonly kill?: KillBehavior | 1
729c563d
S
30 /**
31 * Error.
32 */
325f50bc 33 readonly error?: string
bf9549ae 34 /**
aee46736 35 * Runtime.
bf9549ae 36 */
aee46736 37 readonly runTime?: number
729c563d
S
38 /**
39 * Reference to main worker.
40 *
90a9a10f 41 * Only for internal use.
50e66724 42 * @internal
729c563d 43 */
838898f1 44 readonly parent?: MainWorker
325f50bc 45}
be0676b3
APA
46
47/**
2740a743 48 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 49 *
ae94ec4d 50 * @typeParam Worker - Type of worker.
2740a743 51 * @typeParam Response - Type of execution response. This can only be serializable data.
be0676b3 52 */
c923ce56 53export interface PromiseResponseWrapper<
f06e48d8 54 Worker extends IWorker,
c923ce56
JB
55 Response = unknown
56> {
be0676b3
APA
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 /**
a3445496 66 * The worker handling the execution.
be0676b3 67 */
c923ce56 68 readonly worker: Worker
be0676b3 69}