| 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 { IPoolWorker } from './pools/pool-worker' |
| 5 | |
| 6 | /** |
| 7 | * Make all properties in T non-readonly. |
| 8 | */ |
| 9 | export type Draft<T> = { -readonly [P in keyof T]?: T[P] } |
| 10 | |
| 11 | /** |
| 12 | * Message object that is passed between worker and main worker. |
| 13 | */ |
| 14 | export interface MessageValue< |
| 15 | Data = unknown, |
| 16 | MainWorker extends ClusterWorker | MessagePort | unknown = unknown |
| 17 | > { |
| 18 | /** |
| 19 | * Input data that will be passed to the worker. |
| 20 | */ |
| 21 | readonly data?: Data |
| 22 | /** |
| 23 | * Id of the message. |
| 24 | */ |
| 25 | readonly id?: string |
| 26 | /** |
| 27 | * Kill code. |
| 28 | */ |
| 29 | readonly kill?: KillBehavior | 1 |
| 30 | /** |
| 31 | * Error. |
| 32 | */ |
| 33 | readonly error?: string |
| 34 | /** |
| 35 | * Runtime. |
| 36 | */ |
| 37 | readonly runTime?: number |
| 38 | /** |
| 39 | * Reference to main worker. |
| 40 | * |
| 41 | * Only for internal use. |
| 42 | */ |
| 43 | readonly parent?: MainWorker |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * An object holding the execution response promise resolve/reject callbacks. |
| 48 | * |
| 49 | * @typeParam Worker - Type of worker. |
| 50 | * @typeParam Response - Type of execution response. This can only be serializable data. |
| 51 | */ |
| 52 | export interface PromiseResponseWrapper< |
| 53 | Worker extends IPoolWorker, |
| 54 | Response = unknown |
| 55 | > { |
| 56 | /** |
| 57 | * Resolve callback to fulfill the promise. |
| 58 | */ |
| 59 | readonly resolve: (value: Response) => void |
| 60 | /** |
| 61 | * Reject callback to reject the promise. |
| 62 | */ |
| 63 | readonly reject: (reason?: string) => void |
| 64 | /** |
| 65 | * The worker handling the promise. |
| 66 | */ |
| 67 | readonly worker: Worker |
| 68 | } |