fba39012e995b24cf135d5050b7d86348b719fb3
[poolifier.git] / src / utility-types.ts
1 import type { Worker as ClusterWorker } from 'cluster'
2 import type { MessagePort } from 'worker_threads'
3 import type { IPoolWorker } from './pools/pool-worker'
4 import type { KillBehavior } from './worker/worker-options'
5
6 /**
7 * Make all properties in T non-readonly.
8 */
9 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
10
11 /**
12 * Message object that is passed between worker and main worker.
13 */
14 export interface MessageValue<
15 Data = unknown,
16 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
17 > {
18 /**
19 * Input data that will be passed to the worker.
20 */
21 readonly data?: Data
22 /**
23 * Id of the message.
24 */
25 readonly id?: string
26 /**
27 * Kill code.
28 */
29 readonly kill?: KillBehavior | 1
30 /**
31 * Error.
32 */
33 readonly error?: string
34 /**
35 * Task runtime.
36 */
37 readonly taskRunTime?: number
38 /**
39 * Reference to main worker.
40 *
41 * Only for internal use.
42 */
43 readonly parent?: MainWorker
44 }
45
46 /**
47 * An object holding the worker that will be used to resolve/rejects the promise later on.
48 *
49 * @typeParam Worker - Type of worker.
50 * @typeParam Response - Type of response of execution. This can only be serializable data.
51 */
52 export interface PromiseWorkerResponseWrapper<
53 Worker extends IPoolWorker,
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 }