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.
47 * Task event loop utilization.
49 elu
?: EventLoopUtilization
53 * Performance statistics computation.
55 export interface WorkerStatistics
{
62 * Message object that is passed between main worker and worker.
64 * @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
65 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
66 * @typeParam MainWorker - Type of main worker.
69 export interface MessageValue
<
70 MessageData
= unknown
,
72 MainWorker
extends ClusterWorker
| MessagePort
= ClusterWorker
| MessagePort
73 > extends Task
<MessageData
> {
77 readonly kill
?: KillBehavior
| 1
81 readonly taskError
?: TaskError
<Data
>
85 readonly taskPerformance
?: TaskPerformance
87 * Reference to main worker.
89 readonly parent?: MainWorker
91 * Whether to compute the given statistics or not.
93 readonly statistics
?: WorkerStatistics
97 * An object holding the execution response promise resolve/reject callbacks.
99 * @typeParam Worker - Type of worker.
100 * @typeParam Response - Type of execution response. This can only be serializable data.
103 export interface PromiseResponseWrapper
<
104 Worker
extends IWorker
,
108 * Resolve callback to fulfill the promise.
110 readonly resolve
: (value
: Response
) => void
112 * Reject callback to reject the promise.
114 readonly reject
: (reason
?: string) => void
116 * The worker handling the execution.
118 readonly worker
: Worker