parallel-universe
Version:
The set of async flow control structures and promise utils.
55 lines (54 loc) • 2.15 kB
TypeScript
import { AbortablePromise } from './AbortablePromise';
import { AbortableCallback } from './types';
/**
* The callback execution pool that can execute limited number of callbacks in parallel while other submitted callbacks
* wait in the queue.
*/
export declare class WorkPool {
/**
* The queue that holds submitted jobs.
*/
private _jobQueue;
/**
* Active workers and workers with pending termination.
*/
private _workers;
/**
* Creates a new {@link WorkPool} instance that uses given number of workers.
*
* @param size The number of workers in the pool.
*/
constructor(size?: number);
/**
* The number of active workers in the pool.
*/
get size(): number;
/**
* Changes the size of the pool by spawning or terminating workers. If the size of the pool is reduced, then
* corresponding workers are terminated and if they were processing jobs, those jobs are instantly aborted.
*
* @param size The new size of the pool.
* @param reason The reason that is used to reject pending job promises that are processed by terminated workers. Only
* applicable if the pool is downsized.
* @return The promise that is fulfilled when the number of workers matches the requested size: excessive workers were
* deleted or additional workers were spawned.
*/
setSize(size: number, reason?: any): Promise<void>;
/**
* Submits a new callback that should be executed by the worker in the pool.
*
* @param cb The callback to invoke.
* @template T The callback result.
* @returns The promise that is fulfilled with the callback result.
*/
submit<T>(cb: AbortableCallback<T>): AbortablePromise<T>;
/**
* Aborts all pending jobs and returns promise that is fulfilled when all workers are terminated.
*
* This operation preserves the size of the pool intact.
*
* @param reason The reason that is used to reject all pending job promises.
* @return The promise that is fulfilled when all workers are terminated.
*/
abort(reason?: any): Promise<void>;
}