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