1 import type { Worker
as ClusterWorker
} from
'node:cluster'
2 import type { MessagePort
} from
'node:worker_threads'
3 import type { EventLoopUtilization
} from
'node:perf_hooks'
4 import type { KillBehavior
} from
'./worker/worker-options'
5 import type { IWorker
, Task
} from
'./pools/worker'
8 * Make all properties in T non-readonly.
10 * @typeParam T - Type in which properties will be non-readonly.
12 export type Draft
<T
> = { -readonly [P
in keyof T
]?: T
[P
] }
15 * Message object that is passed between main worker and worker.
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam MainWorker - Type of main worker.
21 export interface MessageValue
<
23 MainWorker
extends ClusterWorker
| MessagePort
= ClusterWorker
| MessagePort
24 > extends Task
<Data
> {
28 readonly kill
?: KillBehavior
| 1
32 readonly error
?: string
34 * Task data triggering task error.
36 readonly errorData
?: unknown
40 readonly runTime
?: number
44 readonly waitTime
?: number
46 * Event loop utilization.
48 readonly elu
?: EventLoopUtilization
50 * Reference to main worker.
52 readonly parent?: MainWorker
56 * Worker synchronous function that can be executed.
58 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
59 * @typeParam Response - Type of execution response. This can only be serializable data.
61 export type WorkerSyncFunction
<Data
= unknown
, Response
= unknown
> = (
66 * Worker asynchronous function that can be executed.
67 * This function must return a promise.
69 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
70 * @typeParam Response - Type of execution response. This can only be serializable data.
72 export type WorkerAsyncFunction
<Data
= unknown
, Response
= unknown
> = (
74 ) => Promise
<Response
>
77 * Worker function that can be executed.
78 * This function can be synchronous or asynchronous.
80 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
81 * @typeParam Response - Type of execution response. This can only be serializable data.
83 export type WorkerFunction
<Data
= unknown
, Response
= unknown
> =
84 | WorkerSyncFunction
<Data
, Response
>
85 | WorkerAsyncFunction
<Data
, Response
>
88 * Worker functions that can be executed.
89 * This object can contain synchronous or asynchronous functions.
90 * The key is the name of the function.
91 * The value is the function itself.
93 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
94 * @typeParam Response - Type of execution response. This can only be serializable data.
96 export type TaskFunctions
<Data
= unknown
, Response
= unknown
> = Record
<
98 WorkerFunction
<Data
, Response
>
102 * An object holding the execution response promise resolve/reject callbacks.
104 * @typeParam Worker - Type of worker.
105 * @typeParam Response - Type of execution response. This can only be serializable data.
108 export interface PromiseResponseWrapper
<
109 Worker
extends IWorker
,
113 * Resolve callback to fulfill the promise.
115 readonly resolve
: (value
: Response
) => void
117 * Reject callback to reject the promise.
119 readonly reject
: (reason
?: string) => void
121 * The worker handling the execution.
123 readonly worker
: Worker