refactor: add PoolEvents/PoolEvent types
[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'
c923ce56 4import type { IPoolWorker } from './pools/pool-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.
729c563d 42 */
838898f1 43 readonly parent?: MainWorker
325f50bc 44}
be0676b3
APA
45
46/**
2740a743 47 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 48 *
ae94ec4d 49 * @typeParam Worker - Type of worker.
2740a743 50 * @typeParam Response - Type of execution response. This can only be serializable data.
be0676b3 51 */
c923ce56
JB
52export interface PromiseResponseWrapper<
53 Worker extends IPoolWorker,
54 Response = unknown
55> {
be0676b3
APA
56 /**
57 * Resolve callback to fulfill the promise.
58 */
59 readonly resolve: (value: Response) => void
60 /**
61 * Reject callback to reject the promise.
62 */
63 readonly reject: (reason?: string) => void
64 /**
c923ce56 65 * The worker handling the promise.
be0676b3 66 */
c923ce56 67 readonly worker: Worker
be0676b3 68}