import type Disposable from '../core/Disposable'; import type Progress from '../core/Progress'; /** * A message to send to the worker. */ export interface Message { /** * The unique id of this message. Used to match the response to the original message. */ id: number; type: string; payload: T; } export interface BaseResponse { requestId: number; } export interface SuccessResponse extends BaseResponse { payload: T; } export interface ErrorResponse extends BaseResponse { error: string; } export type Response = SuccessResponse | ErrorResponse; export declare class WorkerError extends Error { readonly messageId: number; constructor(messageId: number, message: string); } export interface PoolWorker { terminate(): void; postMessage(message: Message, transfer: Transferable[]): void; addEventListener(type: 'message', listener: (ev: MessageEvent) => void): void; removeEventListener(type: 'message', listener: (ev: MessageEvent) => void): void; } export declare function createErrorResponse(requestId: number, error: unknown): Response; export type BaseMessageMap = Record; /** * A simple Web Worker pool that can select idle workers to perform work. * * Additionally, idle workers are terminated after a delay to free resources. * * @typeParam TMessageType - The type of the messages supported by the workers. * @typeParam TMessageMap - The map between request and response messages. */ export default class WorkerPool> implements Disposable, Progress { private readonly _concurrency; private readonly _workers; private readonly _createWorker; private _disposed; private _terminationDelay; private _messageId; get loading(): boolean; get progress(): number; constructor(options: { /** * The function to create a worker. */ createWorker: () => PoolWorker; /** * Optional concurrency (i.e max number of simultaneous workers) * @defaultValue `navigator.hardwareConcurrency` */ concurrency?: number; /** * The delay, in milliseconds, after which an idle worker is terminated. * @defaultValue 10000 */ terminationDelay?: number; }); static get defaultConcurrency(): number; /** * Sends a message to the first available worker, then waits for a response matching this * message's id, then returns this response, or throw an error if an error response is received. */ queue(type: K, payload: TMessageMap[K]['payload'], transfer?: Transferable[]): Promise; /** * Terminates all workers. */ dispose(): void; private startWorkerTerminationTimeout; private createWorker; private getWorker; } //# sourceMappingURL=WorkerPool.d.ts.map