interface IWorkerPool
Since 0.1.0

A pool of worker threads executing task modules off the event loop.

A task module is an ES module owned by the application that registers its handler via defineWorkerTask (from @setu-ts/runtime/worker). Tasks are addressed by module specifier — never by closure, because functions cannot cross a thread boundary. Inputs and outputs travel by structured clone.

Examples

Example 1

const pool = ctx.services.get<IWorkerPool>(CAPABILITIES.WORKER_POOL);
const thumb = await pool.run<Uint8Array, Uint8Array>(
  new URL('./tasks/resize-image.ts', import.meta.url).href,
  imageBytes,
);

Methods

run<TInput, TOutput>(
taskModule: string,
input: TInput,
): Promise<TOutput>

Runs a task on a pool worker for the given task module, creating the pool lazily on first use.

stats(): readonly TaskPoolStats[]

Returns a snapshot of every pool created so far.

shutdown(): Promise<void>

Terminates every worker in every pool and rejects pending tasks. Called by the plugin's onClose hook; safe to call more than once.

Usage

import { type IWorkerPool } from "common/src/index.ts";