UNPKG

1.97 kBTypeScriptView Raw
1import { Thread } from "./thread";
2/** Pool event type. Specifies the type of each `PoolEvent`. */
3export 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}
13export declare type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>;
14/** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */
15export 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};
44export interface WorkerDescriptor<ThreadType extends Thread> {
45 init: Promise<ThreadType>;
46 runningTasks: Array<Promise<any>>;
47}
48/**
49 * Task that has been `pool.queued()`-ed.
50 */
51export interface QueuedTask<ThreadType extends Thread, Return> {
52 /** @private */
53 id: number;
54 /** @private */
55 run: TaskRunFunction<ThreadType, Return>;
56 /**
57 * Queued tasks can be cancelled until the pool starts running them on a worker thread.
58 */
59 cancel(): void;
60 /**
61 * `QueuedTask` is thenable, so you can `await` it.
62 * Resolves when the task has successfully been executed. Rejects if the task fails.
63 */
64 then: Promise<Return>["then"];
65}