Initial comment conversion to TSDoc
[poolifier.git] / src / utility-types.ts
CommitLineData
cc9b8d30 1import type { Worker as ClusterWorker } from 'cluster'
838898f1 2import type { MessagePort } from 'worker_threads'
ea7a90d3 3import type { IPoolWorker } from './pools/pool-worker'
1a81f8af 4import type { KillBehavior } from './worker/worker-options'
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 */
325f50bc 25 readonly id?: number
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 /**
23135a89 35 * Task runtime.
bf9549ae
JB
36 */
37 readonly taskRunTime?: 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/**
47 * An object holding the worker that will be used to resolve/rejects the promise later on.
48 *
38e795c1
JB
49 * @typeParam Worker - Type of worker.
50 * @typeParam Response - Type of response of execution. This can only be serializable data.
be0676b3
APA
51 */
52export interface PromiseWorkerResponseWrapper<
ea7a90d3 53 Worker extends IPoolWorker,
be0676b3
APA
54 Response = unknown
55> {
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 /**
65 * The worker that has the assigned task.
66 */
67 readonly worker: Worker
68}