docs: add documentation to worker function type aliases
[poolifier.git] / src / utility-types.ts
... / ...
CommitLineData
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
3import type { KillBehavior } from './worker/worker-options'
4import type { IWorker, Task } from './pools/worker'
5
6/**
7 * Make all properties in T non-readonly.
8 *
9 * @typeParam T - Type in which properties will be non-readonly.
10 */
11export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
12
13/**
14 * Message object that is passed between main worker and worker.
15 *
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam MainWorker - Type of main worker.
18 * @internal
19 */
20export interface MessageValue<
21 Data = unknown,
22 MainWorker extends ClusterWorker | MessagePort | unknown = unknown
23> extends Task<Data> {
24 /**
25 * Kill code.
26 */
27 readonly kill?: KillBehavior | 1
28 /**
29 * Error.
30 */
31 readonly error?: string
32 /**
33 * Runtime.
34 */
35 readonly runTime?: number
36 /**
37 * Reference to main worker.
38 */
39 readonly parent?: MainWorker
40}
41
42/**
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.
47 */
48export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
49 data?: Data
50) => Response
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 */
58export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
59 data?: Data
60) => Promise<Response>
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 */
68export type WorkerFunction<Data = unknown, Response = unknown> =
69 | WorkerSyncFunction<Data, Response>
70 | WorkerAsyncFunction<Data, Response>
71
72/**
73 * An object holding the execution response promise resolve/reject callbacks.
74 *
75 * @typeParam Worker - Type of worker.
76 * @typeParam Response - Type of execution response. This can only be serializable data.
77 * @internal
78 */
79export interface PromiseResponseWrapper<
80 Worker extends IWorker,
81 Response = unknown
82> {
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 /**
92 * The worker handling the execution.
93 */
94 readonly worker: Worker
95}