-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
75 lines (68 loc) · 2.08 KB
/
types.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
import {WorkerWrapper} from './WorkerWrapper';
/**
* Task executors must return objects of this type.
* @typeParam T - The return type of the task, this is the type of the data that will be sent to the main thread.
*/
export interface TaskResult<T> {
/**
* The result of the task
*/
result: T;
/**
* An array of buffers to transfer back to the main thread
*/
transfer?: ArrayBuffer[];
}
/**
* Object that describes a task to be executed in a worker part of a [[WorkerPool]]
*/
export interface WorkerTask {
/**
* The `id` of the task to execute, this is equivalent to a function name in a [[TaskExecutor]]
*/
id: string;
/**
* An array of arguments for the task, these arguments will be passed to a function called from a [[TaskExecutor]]
*/
args: any[];
/**
* Array of memory buffers to transfer to the worker in which the task will be executed.
*/
transferable?: ArrayBuffer[];
/**
* Function to resolve a promise attached to this task.
* @internal
*/
resolve?: (r: any) => void;
/**
* Function to reject a promise attached to this task.
* @internal
*/
reject?: (e: Error | string) => void;
}
/**
* Any object added to a [[WorkerInterface]] as a task executor must comply with a few rules:
* - Must implement functions as top level properties.
* - Functions that are to be used as tasks must return a [[TaskResult]]
*
* Each function exposed in the object as a top level property will be exposed as a task by a [[WorkerInterface]].
*
* A `TaskExecutor` can be an instance of a class or a plain object with functions as properties.
*/
export type TaskExecutor = any;
/**
* Worker type in NodeJS
*/
export type NodeWorker = import('worker_threads').Worker;
/**
* Worker type in browsers and deno
*/
export type WebWorker = Worker;
/**
* Type for a raw worker on any supported platform
*/
export type PlatformWorker = WebWorker | NodeWorker;
/**
* Type for a raw worker or a wrapped worker on any platform
*/
export type AnyWorker = PlatformWorker | WorkerWrapper;