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 S |
28 | /** |
29 | * Error. | |
30 | */ | |
325f50bc | 31 | readonly error?: string |
bf9549ae | 32 | /** |
aee46736 | 33 | * Runtime. |
bf9549ae | 34 | */ |
aee46736 | 35 | readonly runTime?: number |
09a6305f JB |
36 | /** |
37 | * Wait time. | |
38 | */ | |
39 | readonly waitTime?: number | |
729c563d S |
40 | /** |
41 | * Reference to main worker. | |
729c563d | 42 | */ |
838898f1 | 43 | readonly parent?: MainWorker |
325f50bc | 44 | } |
be0676b3 | 45 | |
48ef9107 | 46 | /** |
09564a83 JB |
47 | * Worker synchronous function that can be executed. |
48 | * | |
49 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
50 | * @typeParam Response - Type of execution response. This can only be serializable data. | |
48ef9107 JB |
51 | */ |
52 | export type WorkerSyncFunction<Data = unknown, Response = unknown> = ( | |
53 | data?: Data | |
54 | ) => Response | |
70a4f5ea | 55 | |
09564a83 JB |
56 | /** |
57 | * Worker asynchronous function that can be executed. | |
58 | * This function must return a promise. | |
59 | * | |
60 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
61 | * @typeParam Response - Type of execution response. This can only be serializable data. | |
62 | */ | |
48ef9107 JB |
63 | export type WorkerAsyncFunction<Data = unknown, Response = unknown> = ( |
64 | data?: Data | |
65 | ) => Promise<Response> | |
70a4f5ea | 66 | |
09564a83 JB |
67 | /** |
68 | * Worker function that can be executed. | |
69 | * This function can be synchronous or asynchronous. | |
70 | * | |
71 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
72 | * @typeParam Response - Type of execution response. This can only be serializable data. | |
73 | */ | |
48ef9107 JB |
74 | export type WorkerFunction<Data = unknown, Response = unknown> = |
75 | | WorkerSyncFunction<Data, Response> | |
76 | | WorkerAsyncFunction<Data, Response> | |
70a4f5ea | 77 | |
a86b6df1 | 78 | /** |
70a4f5ea | 79 | * Worker functions that can be executed. |
a86b6df1 JB |
80 | * This object can contain synchronous or asynchronous functions. |
81 | * The key is the name of the function. | |
82 | * The value is the function itself. | |
83 | * | |
84 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
85 | * @typeParam Response - Type of execution response. This can only be serializable data. | |
86 | */ | |
87 | export type TaskFunctions<Data = unknown, Response = unknown> = Record< | |
88 | string, | |
89 | WorkerFunction<Data, Response> | |
90 | > | |
48ef9107 | 91 | |
be0676b3 | 92 | /** |
2740a743 | 93 | * An object holding the execution response promise resolve/reject callbacks. |
be0676b3 | 94 | * |
ae94ec4d | 95 | * @typeParam Worker - Type of worker. |
2740a743 | 96 | * @typeParam Response - Type of execution response. This can only be serializable data. |
c319c66b | 97 | * @internal |
be0676b3 | 98 | */ |
c923ce56 | 99 | export interface PromiseResponseWrapper< |
f06e48d8 | 100 | Worker extends IWorker, |
c923ce56 JB |
101 | Response = unknown |
102 | > { | |
be0676b3 APA |
103 | /** |
104 | * Resolve callback to fulfill the promise. | |
105 | */ | |
106 | readonly resolve: (value: Response) => void | |
107 | /** | |
108 | * Reject callback to reject the promise. | |
109 | */ | |
110 | readonly reject: (reason?: string) => void | |
111 | /** | |
a3445496 | 112 | * The worker handling the execution. |
be0676b3 | 113 | */ |
c923ce56 | 114 | readonly worker: Worker |
be0676b3 | 115 | } |