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.
09b75fef 5 * @param data - Data sent to the worker.
77e8da5a 6 * @returns Execution response.
e102732c
JB
7 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
8 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b6b32453 9 */
82ea6492 10export type TaskSyncFunction<Data = unknown, Response = unknown> = (
b6b32453
JB
11 data?: Data
12) => Response
13
14/**
82ea6492 15 * Task asynchronous function that can be executed.
b6b32453 16 * This function must return a promise.
09b75fef 17 * @param data - Data sent to the worker.
77e8da5a 18 * @returns Execution response promise.
e102732c
JB
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b6b32453 21 */
82ea6492 22export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
b6b32453
JB
23 data?: Data
24) => Promise<Response>
25
26/**
82ea6492 27 * Task function that can be executed.
b6b32453 28 * This function can be synchronous or asynchronous.
e102732c
JB
29 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
30 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b6b32453 31 */
82ea6492
JB
32export type TaskFunction<Data = unknown, Response = unknown> =
33 | TaskSyncFunction<Data, Response>
34 | TaskAsyncFunction<Data, Response>
b6b32453 35
31847469
JB
36/**
37 * Task function object.
31847469
JB
38 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
39 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
40 */
41export interface TaskFunctionObject<Data = unknown, Response = unknown> {
42 /**
43 * Task function.
44 */
45 taskFunction: TaskFunction<Data, Response>
46 /**
47 * Task function priority. Lower values have higher priority.
48 */
49 priority?: number
50 /**
51 * Task function worker choice strategy.
52 */
53 strategy?: WorkerChoiceStrategy
54}
55
b6b32453 56/**
82ea6492 57 * Tasks functions that can be executed.
31847469 58 * The key is the name of the task function or task function object.
181af286 59 * The value is the task function or task function object.
e102732c
JB
60 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
61 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b6b32453
JB
62 */
63export type TaskFunctions<Data = unknown, Response = unknown> = Record<
3a502712
JB
64 string,
65 TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
b6b32453 66>
e81c38f2
JB
67
68/**
4e38fd21 69 * Task function operation result.
e81c38f2 70 */
4e38fd21 71export interface TaskFunctionOperationResult {
e81c38f2
JB
72 status: boolean
73 error?: Error
74}