build(deps-dev): apply updates
[poolifier.git] / src / worker / task-functions.ts
CommitLineData
31847469
JB
1import type { WorkerChoiceStrategy } from '../pools/selection-strategies/selection-strategies-types.js'
2
b6b32453 3/**
82ea6492 4 * Task synchronous function that can be executed.
b6b32453 5 *
09b75fef 6 * @param data - Data sent to the worker.
77e8da5a 7 * @returns Execution response.
09b75fef 8 *
e102732c
JB
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.
b6b32453 11 */
82ea6492 12export type TaskSyncFunction<Data = unknown, Response = unknown> = (
b6b32453
JB
13 data?: Data
14) => Response
15
16/**
82ea6492 17 * Task asynchronous function that can be executed.
b6b32453
JB
18 * This function must return a promise.
19 *
09b75fef 20 * @param data - Data sent to the worker.
77e8da5a 21 * @returns Execution response promise.
09b75fef 22 *
e102732c
JB
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.
b6b32453 25 */
82ea6492 26export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
b6b32453
JB
27 data?: Data
28) => Promise<Response>
29
30/**
82ea6492 31 * Task function that can be executed.
b6b32453
JB
32 * This function can be synchronous or asynchronous.
33 *
e102732c
JB
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.
b6b32453 36 */
82ea6492
JB
37export type TaskFunction<Data = unknown, Response = unknown> =
38 | TaskSyncFunction<Data, Response>
39 | TaskAsyncFunction<Data, Response>
b6b32453 40
31847469
JB
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
b6b32453 62/**
82ea6492 63 * Tasks functions that can be executed.
31847469 64 * The key is the name of the task function or task function object.
181af286 65 * The value is the task function or task function object.
b6b32453 66 *
e102732c
JB
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.
b6b32453
JB
69 */
70export type TaskFunctions<Data = unknown, Response = unknown> = Record<
71string,
31847469 72TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
b6b32453 73>
e81c38f2
JB
74
75/**
4e38fd21 76 * Task function operation result.
e81c38f2 77 */
4e38fd21 78export interface TaskFunctionOperationResult {
e81c38f2
JB
79 status: boolean
80 error?: Error
81}