1 | import { Observable } from "observable-fns";
|
2 | import { PoolEvent, PoolEventType, QueuedTask, TaskRunFunction } from "./pool-types";
|
3 | import { Thread } from "./thread";
|
4 | export { PoolEvent, PoolEventType, QueuedTask, Thread };
|
5 | export declare namespace Pool {
|
6 | type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
|
7 | type EventType = PoolEventType;
|
8 | }
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export interface Pool<ThreadType extends Thread> {
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | completed(allowResolvingImmediately?: boolean): Promise<any>;
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
|
29 | |
30 |
|
31 |
|
32 | events(): Observable<PoolEvent<ThreadType>>;
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | terminate(force?: boolean): Promise<void>;
|
46 | }
|
47 | export interface PoolOptions {
|
48 |
|
49 | concurrency?: number;
|
50 |
|
51 | maxQueuedJobs?: number;
|
52 |
|
53 | name?: string;
|
54 |
|
55 | size?: number;
|
56 | }
|
57 | declare 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 | */
|
83 | declare 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 | */
|
87 | export declare const Pool: typeof PoolConstructor & {
|
88 | EventType: typeof PoolEventType;
|
89 | };
|