1 | import { Thread } from "./thread";
|
2 |
|
3 | export declare enum PoolEventType {
|
4 | initialized = "initialized",
|
5 | taskCanceled = "taskCanceled",
|
6 | taskCompleted = "taskCompleted",
|
7 | taskFailed = "taskFailed",
|
8 | taskQueued = "taskQueued",
|
9 | taskQueueDrained = "taskQueueDrained",
|
10 | taskStart = "taskStart",
|
11 | terminated = "terminated"
|
12 | }
|
13 | export declare type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>;
|
14 |
|
15 | export declare type PoolEvent<ThreadType extends Thread> = {
|
16 | type: PoolEventType.initialized;
|
17 | size: number;
|
18 | } | {
|
19 | type: PoolEventType.taskQueued;
|
20 | taskID: number;
|
21 | } | {
|
22 | type: PoolEventType.taskQueueDrained;
|
23 | } | {
|
24 | type: PoolEventType.taskStart;
|
25 | taskID: number;
|
26 | workerID: number;
|
27 | } | {
|
28 | type: PoolEventType.taskCompleted;
|
29 | returnValue: any;
|
30 | taskID: number;
|
31 | workerID: number;
|
32 | } | {
|
33 | type: PoolEventType.taskFailed;
|
34 | error: Error;
|
35 | taskID: number;
|
36 | workerID: number;
|
37 | } | {
|
38 | type: PoolEventType.taskCanceled;
|
39 | taskID: number;
|
40 | } | {
|
41 | type: PoolEventType.terminated;
|
42 | remainingQueue: Array<QueuedTask<ThreadType, any>>;
|
43 | };
|
44 | export interface WorkerDescriptor<ThreadType extends Thread> {
|
45 | init: Promise<ThreadType>;
|
46 | runningTasks: Array<Promise<any>>;
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 | export interface QueuedTask<ThreadType extends Thread, Return> {
|
52 |
|
53 | id: number;
|
54 |
|
55 | run: TaskRunFunction<ThreadType, Return>;
|
56 | |
57 |
|
58 |
|
59 | cancel(): void;
|
60 | |
61 |
|
62 |
|
63 |
|
64 | then: Promise<Return>["then"];
|
65 | }
|