feat: support multiple functions per worker
[poolifier.git] / src / utility-types.ts
CommitLineData
fc3e6586
JB
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
1a81f8af 3import type { KillBehavior } from './worker/worker-options'
02706357 4import 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
11export 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
20export interface MessageValue<
21 Data = unknown,
cc9b8d30 22 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
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
729c563d
S
36 /**
37 * Reference to main worker.
729c563d 38 */
838898f1 39 readonly parent?: MainWorker
325f50bc 40}
be0676b3 41
48ef9107 42/**
09564a83
JB
43 * Worker synchronous function that can be executed.
44 *
45 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
46 * @typeParam Response - Type of execution response. This can only be serializable data.
48ef9107
JB
47 */
48export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
49 data?: Data
50) => Response
09564a83
JB
51/**
52 * Worker asynchronous function that can be executed.
53 * This function must return a promise.
54 *
55 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
56 * @typeParam Response - Type of execution response. This can only be serializable data.
57 */
48ef9107
JB
58export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
59 data?: Data
60) => Promise<Response>
09564a83
JB
61/**
62 * Worker function that can be executed.
63 * This function can be synchronous or asynchronous.
64 *
65 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
66 * @typeParam Response - Type of execution response. This can only be serializable data.
67 */
48ef9107
JB
68export type WorkerFunction<Data = unknown, Response = unknown> =
69 | WorkerSyncFunction<Data, Response>
70 | WorkerAsyncFunction<Data, Response>
a86b6df1
JB
71/**
72 * Worker functions object that can be executed.
73 * This object can contain synchronous or asynchronous functions.
74 * The key is the name of the function.
75 * The value is the function itself.
76 *
77 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
78 * @typeParam Response - Type of execution response. This can only be serializable data.
79 */
80export type TaskFunctions<Data = unknown, Response = unknown> = Record<
81string,
82WorkerFunction<Data, Response>
83>
48ef9107 84
be0676b3 85/**
2740a743 86 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 87 *
ae94ec4d 88 * @typeParam Worker - Type of worker.
2740a743 89 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 90 * @internal
be0676b3 91 */
c923ce56 92export interface PromiseResponseWrapper<
f06e48d8 93 Worker extends IWorker,
c923ce56
JB
94 Response = unknown
95> {
be0676b3
APA
96 /**
97 * Resolve callback to fulfill the promise.
98 */
99 readonly resolve: (value: Response) => void
100 /**
101 * Reject callback to reject the promise.
102 */
103 readonly reject: (reason?: string) => void
104 /**
a3445496 105 * The worker handling the execution.
be0676b3 106 */
c923ce56 107 readonly worker: Worker
be0676b3 108}