chore: v2.4.8
[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'
02706357 4import type { IWorker, Task } 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 11/**
02706357 12 * Message object that is passed between main worker and worker.
c319c66b
JB
13 *
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam MainWorker - Type of main worker.
71ebe93b 16 * @internal
729c563d 17 */
838898f1
S
18export interface MessageValue<
19 Data = unknown,
cc9b8d30 20 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
02706357 21> extends Task<Data> {
729c563d
S
22 /**
23 * Kill code.
24 */
1a81f8af 25 readonly kill?: KillBehavior | 1
729c563d
S
26 /**
27 * Error.
28 */
325f50bc 29 readonly error?: string
bf9549ae 30 /**
aee46736 31 * Runtime.
bf9549ae 32 */
aee46736 33 readonly runTime?: number
729c563d
S
34 /**
35 * Reference to main worker.
36 *
90a9a10f 37 * Only for internal use.
50e66724 38 * @internal
729c563d 39 */
838898f1 40 readonly parent?: MainWorker
325f50bc 41}
be0676b3
APA
42
43/**
2740a743 44 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 45 *
ae94ec4d 46 * @typeParam Worker - Type of worker.
2740a743 47 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 48 * @internal
be0676b3 49 */
c923ce56 50export interface PromiseResponseWrapper<
f06e48d8 51 Worker extends IWorker,
c923ce56
JB
52 Response = unknown
53> {
be0676b3
APA
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 /**
a3445496 63 * The worker handling the execution.
be0676b3 64 */
c923ce56 65 readonly worker: Worker
be0676b3 66}