Commit | Line | Data |
---|---|---|
fc3e6586 JB |
1 | import type { Worker as ClusterWorker } from 'node:cluster' |
2 | import type { MessagePort } from 'node:worker_threads' | |
1a81f8af | 3 | import type { KillBehavior } from './worker/worker-options' |
02706357 | 4 | import type { IWorker, Task } from './pools/worker' |
838898f1 | 5 | |
729c563d | 6 | /** |
3832ad95 | 7 | * Make all properties in T non-readonly. |
b40ed511 JB |
8 | * |
9 | * @typeParam T - Type in which properties will be non-readonly. | |
729c563d | 10 | */ |
325f50bc S |
11 | export type Draft<T> = { -readonly [P in keyof T]?: T[P] } |
12 | ||
729c563d | 13 | /** |
02706357 | 14 | * Message object that is passed between main worker and worker. |
c319c66b JB |
15 | * |
16 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
17 | * @typeParam MainWorker - Type of main worker. | |
71ebe93b | 18 | * @internal |
729c563d | 19 | */ |
838898f1 S |
20 | export interface MessageValue< |
21 | Data = unknown, | |
4c0ada52 | 22 | MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort |
02706357 | 23 | > extends Task<Data> { |
729c563d S |
24 | /** |
25 | * Kill code. | |
26 | */ | |
1a81f8af | 27 | readonly kill?: KillBehavior | 1 |
729c563d | 28 | /** |
91ee39ed | 29 | * Task error. |
729c563d | 30 | */ |
325f50bc | 31 | readonly error?: string |
91ee39ed JB |
32 | /** |
33 | * Task data triggering task error. | |
34 | */ | |
35 | readonly errorData?: unknown | |
bf9549ae | 36 | /** |
aee46736 | 37 | * Runtime. |
bf9549ae | 38 | */ |
aee46736 | 39 | readonly runTime?: number |
09a6305f JB |
40 | /** |
41 | * Wait time. | |
42 | */ | |
43 | readonly waitTime?: number | |
729c563d S |
44 | /** |
45 | * Reference to main worker. | |
729c563d | 46 | */ |
838898f1 | 47 | readonly parent?: MainWorker |
325f50bc | 48 | } |
be0676b3 | 49 | |
48ef9107 | 50 | /** |
09564a83 JB |
51 | * Worker synchronous function that can be executed. |
52 | * | |
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. | |
48ef9107 JB |
55 | */ |
56 | export type WorkerSyncFunction<Data = unknown, Response = unknown> = ( | |
57 | data?: Data | |
58 | ) => Response | |
70a4f5ea | 59 | |
09564a83 JB |
60 | /** |
61 | * Worker asynchronous function that can be executed. | |
62 | * This function must return a promise. | |
63 | * | |
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. | |
66 | */ | |
48ef9107 JB |
67 | export type WorkerAsyncFunction<Data = unknown, Response = unknown> = ( |
68 | data?: Data | |
69 | ) => Promise<Response> | |
70a4f5ea | 70 | |
09564a83 JB |
71 | /** |
72 | * Worker function that can be executed. | |
73 | * This function can be synchronous or asynchronous. | |
74 | * | |
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. | |
77 | */ | |
48ef9107 JB |
78 | export type WorkerFunction<Data = unknown, Response = unknown> = |
79 | | WorkerSyncFunction<Data, Response> | |
80 | | WorkerAsyncFunction<Data, Response> | |
70a4f5ea | 81 | |
a86b6df1 | 82 | /** |
70a4f5ea | 83 | * Worker functions that can be executed. |
a86b6df1 JB |
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. | |
87 | * | |
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. | |
90 | */ | |
91 | export type TaskFunctions<Data = unknown, Response = unknown> = Record< | |
92 | string, | |
93 | WorkerFunction<Data, Response> | |
94 | > | |
48ef9107 | 95 | |
be0676b3 | 96 | /** |
2740a743 | 97 | * An object holding the execution response promise resolve/reject callbacks. |
be0676b3 | 98 | * |
ae94ec4d | 99 | * @typeParam Worker - Type of worker. |
2740a743 | 100 | * @typeParam Response - Type of execution response. This can only be serializable data. |
c319c66b | 101 | * @internal |
be0676b3 | 102 | */ |
c923ce56 | 103 | export interface PromiseResponseWrapper< |
f06e48d8 | 104 | Worker extends IWorker, |
c923ce56 JB |
105 | Response = unknown |
106 | > { | |
be0676b3 APA |
107 | /** |
108 | * Resolve callback to fulfill the promise. | |
109 | */ | |
110 | readonly resolve: (value: Response) => void | |
111 | /** | |
112 | * Reject callback to reject the promise. | |
113 | */ | |
114 | readonly reject: (reason?: string) => void | |
115 | /** | |
a3445496 | 116 | * The worker handling the execution. |
be0676b3 | 117 | */ |
c923ce56 | 118 | readonly worker: Worker |
be0676b3 | 119 | } |