docs: add documentation to worker function type aliases
[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>
71
be0676b3 72/**
2740a743 73 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 74 *
ae94ec4d 75 * @typeParam Worker - Type of worker.
2740a743 76 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 77 * @internal
be0676b3 78 */
c923ce56 79export interface PromiseResponseWrapper<
f06e48d8 80 Worker extends IWorker,
c923ce56
JB
81 Response = unknown
82> {
be0676b3
APA
83 /**
84 * Resolve callback to fulfill the promise.
85 */
86 readonly resolve: (value: Response) => void
87 /**
88 * Reject callback to reject the promise.
89 */
90 readonly reject: (reason?: string) => void
91 /**
a3445496 92 * The worker handling the execution.
be0676b3 93 */
c923ce56 94 readonly worker: Worker
be0676b3 95}