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
] }
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
19 export interface TaskError
<Data
= unknown
> {
25 * Data passed to the worker triggering the error.
33 export interface TaskPerformance
{
35 * Task performance timestamp.
43 * Task event loop utilization.
45 elu
?: EventLoopUtilization
49 * Performance statistics computation.
51 export interface WorkerStatistics
{
57 * Message object that is passed between main worker and worker.
59 * @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
60 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
61 * @typeParam MainWorker - Type of main worker.
64 export interface MessageValue
<
65 MessageData
= unknown
,
67 MainWorker
extends ClusterWorker
| MessagePort
= ClusterWorker
| MessagePort
68 > extends Task
<MessageData
> {
72 readonly kill
?: KillBehavior
| 1
76 readonly taskError
?: TaskError
<Data
>
80 readonly taskPerformance
?: TaskPerformance
82 * Reference to main worker.
84 readonly parent?: MainWorker
86 * Whether to compute the given statistics or not.
88 readonly statistics
?: WorkerStatistics
92 * An object holding the execution response promise resolve/reject callbacks.
94 * @typeParam Worker - Type of worker.
95 * @typeParam Response - Type of execution response. This can only be serializable data.
98 export interface PromiseResponseWrapper
<
99 Worker
extends IWorker
,
103 * Resolve callback to fulfill the promise.
105 readonly resolve
: (value
: Response
) => void
107 * Reject callback to reject the promise.
109 readonly reject
: (reason
?: string) => void
111 * The worker handling the execution.
113 readonly worker
: Worker