fix: silence sonar code smell
[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,
4c0ada52 22 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
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
70a4f5ea 51
09564a83
JB
52/**
53 * Worker asynchronous function that can be executed.
54 * This function must return a promise.
55 *
56 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
57 * @typeParam Response - Type of execution response. This can only be serializable data.
58 */
48ef9107
JB
59export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
60 data?: Data
61) => Promise<Response>
70a4f5ea 62
09564a83
JB
63/**
64 * Worker function that can be executed.
65 * This function can be synchronous or asynchronous.
66 *
67 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
68 * @typeParam Response - Type of execution response. This can only be serializable data.
69 */
48ef9107
JB
70export type WorkerFunction<Data = unknown, Response = unknown> =
71 | WorkerSyncFunction<Data, Response>
72 | WorkerAsyncFunction<Data, Response>
70a4f5ea 73
a86b6df1 74/**
70a4f5ea 75 * Worker functions that can be executed.
a86b6df1
JB
76 * This object can contain synchronous or asynchronous functions.
77 * The key is the name of the function.
78 * The value is the function itself.
79 *
80 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
81 * @typeParam Response - Type of execution response. This can only be serializable data.
82 */
83export type TaskFunctions<Data = unknown, Response = unknown> = Record<
84string,
85WorkerFunction<Data, Response>
86>
48ef9107 87
be0676b3 88/**
2740a743 89 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 90 *
ae94ec4d 91 * @typeParam Worker - Type of worker.
2740a743 92 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 93 * @internal
be0676b3 94 */
c923ce56 95export interface PromiseResponseWrapper<
f06e48d8 96 Worker extends IWorker,
c923ce56
JB
97 Response = unknown
98> {
be0676b3
APA
99 /**
100 * Resolve callback to fulfill the promise.
101 */
102 readonly resolve: (value: Response) => void
103 /**
104 * Reject callback to reject the promise.
105 */
106 readonly reject: (reason?: string) => void
107 /**
a3445496 108 * The worker handling the execution.
be0676b3 109 */
c923ce56 110 readonly worker: Worker
be0676b3 111}