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