Skip to content

Worker Pool ​

createWorkerPool() (vue-worker-kit/pool) — for many small, independent tasks (resizing hundreds of images, etc.).

ts
import { createWorkerPool } from 'vue-worker-kit/pool'

const pool = createWorkerPool<typeof import('./resize.worker')>(
  () => new Worker(new URL('./resize.worker.ts', import.meta.url), { type: 'module' }),
)

// Pre-create all workers up to size (optional, avoids cold-start latency on first tasks)
await pool.warmup()

// Process array with per-item transfer and global cancellation signal
const thumbnails = await pool.map(files, {
  concurrency: 4,
  transfer: (file) => [file.buffer], // zero-copy per item
  signal: abortController.signal, // cancel all running tasks
})

const one = await pool.run(files[0])

Workers are created lazily, up to size, as tasks arrive — not all at once. size (option) defaults to navigator.hardwareConcurrency — the browser's own count of logical cores/threads on the machine actually running your app, not a number picked at development time. Pass size explicitly to override it (e.g. to cap it, or if navigator reports something you don't want to trust — some privacy-hardened browsers cap or round it). Falls back to 4 where navigator doesn't exist (SSR).

Options ​

  • size?: number — maximum number of workers (see above)
  • hardCancelOnAbort?: boolean · default false — terminate the slot of a cancelled task; can be overridden per call
  • idleTimeout?: number | false · default false — terminate a slot that stayed idle this many ms; a later task creates a new one
  • affinityMode?: 'prefer' | 'strict' · default 'prefer' — see Affinity
  • unwrap?: 'shallow' | 'deep' · default 'shallow' — see unwrap

Methods ​

pool.run(input, options?) ​

(input: In, options?: RunOptions) => Promise<Out>

Queues the task on the first free worker.

  • transfer?: Transferable[] — zero-copy transfer list for this task
  • signal?: AbortSignal — cancels this task
  • onProgress?: (value: number) => void — receives this task's ctx.reportProgress() calls
  • onChunk?: (chunk: unknown) => void — receives this task's ctx.reportChunk() calls
  • hardCancelOnAbort?: boolean — overrides the pool option for this task
  • affinity?: string | number — key that keeps tasks on the same slot, see Affinity
  • unwrap?: 'shallow' | 'deep' — overrides the pool option for this task

For a module made with defineWorkerOperations() the pool has pool.call(name, input, options?) instead of run()/map().

pool.map(items, options?) ​

(items: In[], options?: MapOptions) => Promise<Out[]>

Processes an array with bounded parallelism, results in input order.

  • concurrency?: number — max parallel tasks (default: pool.size)
  • transfer?: (item: T) => Transferable[] — per-item zero-copy transfer function
  • signal?: AbortSignal — global cancellation signal for all items
  • onProgress?: (value: number) => void — one callback shared across every item in the batch
  • onChunk?: (chunk: unknown) => void — one callback shared across every item in the batch
  • hardCancelOnAbort?: boolean — overrides the pool option for the items of this call
  • unwrap?: 'shallow' | 'deep' — overrides the pool option for the items of this call

Cancellation and crashes ​

  • A task still waiting in the queue is rejected with AbortError the moment its signal aborts, and stats.queued drops at once.
  • A task that is already running is rejected at once too, but without hardCancelOnAbort the pool only cancels cooperatively: the slot stays busy until the handler notices ctx.signal (use await ctx.yield() in long loops) or finishes.
  • With hardCancelOnAbort only the slot of the cancelled task is terminated; the other slots are untouched, and a new slot is created when needed. A map() with one shared signal therefore terminates every busy slot.
  • If a worker crashes, its task rejects with WorkerError('Worker crashed'), the dead slot is removed and terminated, stats stays correct, and the queue goes on with new slots.

See Cancellation, Concurrency & Operations.

Progress and chunk reporting ​

Unlike useWorker()/useSharedWorker(), the pool has no persistent progress/chunks ref — many tasks can be running across different slots at once, so there's no single value to expose. Instead, pass onProgress/onChunk directly to pool.run() or pool.map():

ts
await pool.run(input, {
  onProgress: (value) => console.log('progress', value),
  onChunk: (chunk) => console.log('chunk', chunk),
})

await pool.map(items, {
  onProgress: (value) => console.log('progress', value), // fires for whichever item is reporting
  onChunk: (chunk) => console.log('chunk', chunk),
})

For map(), the same onProgress/onChunk pair is shared across the whole batch — there's no per-item callback, so if you need to know which item a given progress value or chunk belongs to, include that information in the value/chunk itself (e.g. have the worker handler embed the input or an id in what it reports).

A handler written for useWorker()/useSharedWorker() that calls ctx.reportProgress()/ctx.reportChunk() works unchanged under a pool — it doesn't know which composable is driving it — but those calls are only observable if you pass onProgress/onChunk in RunOptions/MapOptions; otherwise they're dropped (see Streaming / Chunked Results for the dev-mode warning that fires in that case).

pool.stats ​

ComputedRef<{ busy: number; idle: number; queued: number }>

Reactive, used by the devtools panel.

pool.terminate() ​

() => void

Closes the pool: every slot is terminated, and waiting and running tasks reject with WorkerError('Worker pool terminated') whose code is 'POOL_TERMINATED' (deliberately not an AbortError). Afterwards run(), call() and map() reject with the same error instead of silently creating new workers, and warmup() does nothing. This differs from a single worker, where terminate() only releases the worker and the next run() creates another.

pool.restart() ​

() => void

Opens a terminated pool again; slots are created lazily by the next tasks.

pool.warmup() ​

() => Promise<void>

Pre-creates all workers up to size without executing tasks.

Affinity ​

run(input, { affinity: key }) sends tasks with the same key to the same slot — useful when a worker keeps a cache for that key.

  • The key is bound to the slot that takes its first task. The binding is never moved: when 'prefer' has to use another slot because the bound one is busy, later tasks still go back to the bound slot once it is free.
  • 'prefer' never blocks. 'strict' makes the task wait in the queue for the bound slot (other queued tasks are not blocked) and never creates extra slots for it.
  • When the bound slot is removed (idleTimeout, a crash, hardCancelOnAbort, terminate()), the key is released and binds again to the slot that takes the next task with that key.
  • Tasks without a key take any free slot.

Vue Composable Variant ​

useWorkerPool()

The same API as createWorkerPool(), with onScopeDispose auto-termination for use directly in setup() — reach for this one instead of createWorkerPool() whenever the pool's lifetime should match the component's.