Merge pull request #2343 from poolifier/combined-prs-branch
[poolifier.git] / src / worker / task-functions.ts
... / ...
CommitLineData
1import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js'
2
3/**
4 * Task synchronous function that can be executed.
5 *
6 * @param data - Data sent to the worker.
7 * @returns Execution response.
8 *
9 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
10 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
11 */
12export type TaskSyncFunction<Data = unknown, Response = unknown> = (
13 data?: Data
14) => Response
15
16/**
17 * Task asynchronous function that can be executed.
18 * This function must return a promise.
19 *
20 * @param data - Data sent to the worker.
21 * @returns Execution response promise.
22 *
23 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
24 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
25 */
26export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
27 data?: Data
28) => Promise<Response>
29
30/**
31 * Task function that can be executed.
32 * This function can be synchronous or asynchronous.
33 *
34 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
35 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
36 */
37export type TaskFunction<Data = unknown, Response = unknown> =
38 | TaskSyncFunction<Data, Response>
39 | TaskAsyncFunction<Data, Response>
40
41/**
42 * Task function object.
43 *
44 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
45 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
46 */
47export interface TaskFunctionObject<Data = unknown, Response = unknown> {
48 /**
49 * Task function.
50 */
51 taskFunction: TaskFunction<Data, Response>
52 /**
53 * Task function priority. Lower values have higher priority.
54 */
55 priority?: number
56 /**
57 * Task function worker choice strategy.
58 */
59 strategy?: WorkerChoiceStrategy
60}
61
62/**
63 * Tasks functions that can be executed.
64 * The key is the name of the task function or task function object.
65 * The value is the task function or task function object.
66 *
67 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
68 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
69 */
70export type TaskFunctions<Data = unknown, Response = unknown> = Record<
71string,
72TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
73>
74
75/**
76 * Task function operation result.
77 */
78export interface TaskFunctionOperationResult {
79 status: boolean
80 error?: Error
81}