1 import type { Worker
as ClusterWorker
} from
'node:cluster'
2 import type { MessagePort
} from
'node:worker_threads'
3 import type { KillBehavior
} from
'./worker/worker-options'
4 import type { IWorker
, Task
} from
'./pools/worker'
7 * Make all properties in T non-readonly.
9 * @typeParam T - Type in which properties will be non-readonly.
11 export type Draft
<T
> = { -readonly [P
in keyof T
]?: T
[P
] }
14 * Message object that is passed between main worker and worker.
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam MainWorker - Type of main worker.
20 export interface MessageValue
<
22 MainWorker
extends ClusterWorker
| MessagePort
= ClusterWorker
| MessagePort
23 > extends Task
<Data
> {
27 readonly kill
?: KillBehavior
| 1
31 readonly error
?: string
33 * Task data triggering task error.
35 readonly errorData
?: unknown
39 readonly runTime
?: number
43 readonly waitTime
?: number
45 * Reference to main worker.
47 readonly parent?: MainWorker
51 * Worker synchronous function that can be executed.
53 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
54 * @typeParam Response - Type of execution response. This can only be serializable data.
56 export type WorkerSyncFunction
<Data
= unknown
, Response
= unknown
> = (
61 * Worker asynchronous function that can be executed.
62 * This function must return a promise.
64 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
65 * @typeParam Response - Type of execution response. This can only be serializable data.
67 export type WorkerAsyncFunction
<Data
= unknown
, Response
= unknown
> = (
69 ) => Promise
<Response
>
72 * Worker function that can be executed.
73 * This function can be synchronous or asynchronous.
75 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
76 * @typeParam Response - Type of execution response. This can only be serializable data.
78 export type WorkerFunction
<Data
= unknown
, Response
= unknown
> =
79 | WorkerSyncFunction
<Data
, Response
>
80 | WorkerAsyncFunction
<Data
, Response
>
83 * Worker functions that can be executed.
84 * This object can contain synchronous or asynchronous functions.
85 * The key is the name of the function.
86 * The value is the function itself.
88 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
89 * @typeParam Response - Type of execution response. This can only be serializable data.
91 export type TaskFunctions
<Data
= unknown
, Response
= unknown
> = Record
<
93 WorkerFunction
<Data
, Response
>
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