/** * Construction options for {@link WorkerPool}. * * `concurrency` sets the maximum number of Worker instances created; once this * limit is reached, `acquire()` calls queue until a worker is released. * `workerUrl` is the script URL passed to `new Worker()`. Set `smol: true` on * Bun to request a reduced-heap worker. * * @example * ```ts * import { WorkerPool, type WorkerPoolOptions } from '@lostgradient/weft'; * * const options: WorkerPoolOptions = { * concurrency: 4, * workerUrl: new URL('./activity-worker.ts', import.meta.url), * smol: false, * }; * using pool = new WorkerPool(options); * void pool; * ``` */ export interface WorkerPoolOptions { concurrency: number; workerUrl: string | URL; smol?: boolean; } /** * Bounded pool of Web Workers with acquire/release lifecycle management. * * Workers are created lazily up to `concurrency` and reused across tasks. * `acquire()` returns a `Worker` immediately if one is available, creates a new * one if under the limit, or queues the request until a worker is released. * Use `[Symbol.asyncDispose]()` for a graceful shutdown that waits for * in-flight workers to finish, or `[Symbol.dispose]()` for immediate * termination. * * @example * ```ts * import { WorkerPool } from '@lostgradient/weft'; * * await using pool = new WorkerPool({ * concurrency: 2, * workerUrl: new URL('./worker.ts', import.meta.url), * }); * * const worker = await pool.acquire(); * worker.postMessage({ task: 'hello' }); * // ... wait for message event ... * pool.release(worker); * ``` */ export declare class WorkerPool implements Disposable, AsyncDisposable { #private; constructor(options: WorkerPoolOptions); /** Acquire a worker from the pool. Blocks if at capacity. */ acquire(): Promise; /** * Acquire a specific worker once it is released back to the pool. * * This is intentionally narrower than `acquire()`: it preserves worker-local * generator state for parked workflow execution without reserving unrelated * idle workers while the target worker is still busy. */ acquireSpecificWorker(worker: Worker): Promise; /** * Remove a failed worker from the pool without returning it to the available * set. Pending requests for that exact worker fail; generic waiters may get * a replacement worker if the pool still has capacity. */ discard(worker: Worker): void; /** Release a worker back to the pool. */ release(worker: Worker): void; /** Get the number of available workers. */ get availableCount(): number; /** Get the total number of workers. */ get totalCount(): number; /** Get the number of pending acquire requests. */ get pendingCount(): number; /** Immediate termination. */ [Symbol.dispose](): void; /** Graceful: wait for in-flight, then terminate. */ [Symbol.asyncDispose](): Promise; }