UNPKG

3.7 kBTypeScriptView Raw
1import { Observable } from "observable-fns";
2import { PoolEvent, PoolEventType, QueuedTask, TaskRunFunction } from "./pool-types";
3import { Thread } from "./thread";
4export { PoolEvent, PoolEventType, QueuedTask, Thread };
5export declare namespace Pool {
6 type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
7 type EventType = PoolEventType;
8}
9/**
10 * Thread pool managing a set of worker threads.
11 * Use it to queue tasks that are run on those threads with limited
12 * concurrency.
13 */
14export interface Pool<ThreadType extends Thread> {
15 /**
16 * Returns a promise that resolves once the task queue is emptied.
17 * Promise will be rejected if any task fails.
18 *
19 * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
20 */
21 completed(allowResolvingImmediately?: boolean): Promise<any>;
22 /**
23 * Returns a promise that resolves once the task queue is emptied.
24 * Failing tasks will not cause the promise to be rejected.
25 *
26 * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
27 */
28 settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
29 /**
30 * Returns an observable that yields pool events.
31 */
32 events(): Observable<PoolEvent<ThreadType>>;
33 /**
34 * Queue a task and return a promise that resolves once the task has been dequeued,
35 * started and finished.
36 *
37 * @param task An async function that takes a thread instance and invokes it.
38 */
39 queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
40 /**
41 * Terminate all pool threads.
42 *
43 * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
44 */
45 terminate(force?: boolean): Promise<void>;
46}
47export interface PoolOptions {
48 /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
49 concurrency?: number;
50 /** Maximum no. of jobs to be queued for execution before throwing an error. */
51 maxQueuedJobs?: number;
52 /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
53 name?: string;
54 /** No. of worker threads to spawn and to be managed by the pool. */
55 size?: number;
56}
57declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
58 static EventType: typeof PoolEventType;
59 private readonly debug;
60 private readonly eventObservable;
61 private readonly options;
62 private readonly workers;
63 private readonly eventSubject;
64 private initErrors;
65 private isClosing;
66 private nextTaskID;
67 private taskQueue;
68 constructor(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions);
69 private findIdlingWorker;
70 private runPoolTask;
71 private run;
72 private scheduleWork;
73 private taskCompletion;
74 settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
75 completed(allowResolvingImmediately?: boolean): Promise<void>;
76 events(): Observable<PoolEvent<ThreadType>>;
77 queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
78 terminate(force?: boolean): Promise<void>;
79}
80/**
81 * Thread pool constructor. Creates a new pool and spawns its worker threads.
82 */
83declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
84/**
85 * Thread pool constructor. Creates a new pool and spawns its worker threads.
86 */
87export declare const Pool: typeof PoolConstructor & {
88 EventType: typeof PoolEventType;
89};