Refine eslint configuration
[poolifier.git] / src / utility-types.ts
CommitLineData
cc9b8d30 1import type { Worker as ClusterWorker } from 'cluster'
838898f1 2import type { MessagePort } from 'worker_threads'
bdaf31cd 3import type { AbstractPoolWorker } from './pools/abstract-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
729c563d
S
34 /**
35 * Reference to main worker.
36 *
90a9a10f 37 * Only for internal use.
729c563d 38 */
838898f1 39 readonly parent?: MainWorker
325f50bc 40}
be0676b3
APA
41
42/**
43 * An object holding the worker that will be used to resolve/rejects the promise later on.
44 *
45 * @template Worker Type of worker.
46 * @template Response Type of response of execution. This can only be serializable data.
47 */
48export interface PromiseWorkerResponseWrapper<
bdaf31cd 49 Worker extends AbstractPoolWorker,
be0676b3
APA
50 Response = unknown
51> {
52 /**
53 * Resolve callback to fulfill the promise.
54 */
55 readonly resolve: (value: Response) => void
56 /**
57 * Reject callback to reject the promise.
58 */
59 readonly reject: (reason?: string) => void
60 /**
61 * The worker that has the assigned task.
62 */
63 readonly worker: Worker
64}