Add dynamic worker choice strategy change at runtime
[poolifier.git] / src / utility-types.ts
1 import type { Worker as ClusterWorker } from 'cluster'
2 import type { MessagePort } from 'worker_threads'
3 import type { AbstractPoolWorker } from './pools/abstract-pool-worker'
4 import type { KillBehavior } from './worker/worker-options'
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?: number
26 /**
27 * Kill code.
28 */
29 readonly kill?: KillBehavior | 1
30 /**
31 * Error.
32 */
33 readonly error?: string
34 /**
35 * Reference to main worker.
36 *
37 * Only for internal use.
38 */
39 readonly parent?: MainWorker
40 }
41
42 /**
43 * An object holding the worker that will be used to resolve/rejects the promise later on.
44 *
45 * @template Worker Type of worker.
46 * @template Response Type of response of execution. This can only be serializable data.
47 */
48 export interface PromiseWorkerResponseWrapper<
49 Worker extends AbstractPoolWorker,
50 Response = unknown
51 > {
52 /**
53 * Resolve callback to fulfill the promise.
54 */
55 readonly resolve: (value: Response) => void
56 /**
57 * Reject callback to reject the promise.
58 */
59 readonly reject: (reason?: string) => void
60 /**
61 * The worker that has the assigned task.
62 */
63 readonly worker: Worker
64 }