Single Worker
useWorker() — main-thread composable, wraps a single lazily-created worker.
const { run, isRunning, progress, error, cancel, warmup } = useWorker<typeof import('./x.worker')>(
() => new Worker(new URL('./x.worker.ts', import.meta.url), { type: 'module' }),
{ idleTimeout: 30_000, retries: 0 },
)
// Optional: pre-create the worker without running a task (avoids cold-start latency on first run)
await warmup()
const output = await run(input, { transfer: [input.buffer], signal: controller.signal })By default (unwrap: 'shallow') run()'s input is passed through toRaw() before being posted — a ref/reactive value read straight off a component (() => list.value) is not structured-cloneable as a live Proxy, so the raw snapshot is what actually gets sent. Only the top-level value is unwrapped; see unwrap for reactive objects nested inside the input.
If the worker file uses defineWorkerOperations(), the returned object has call(name, input, options?) instead of run() — see Cancellation, Concurrency & Operations.
Options
idleTimeout
number | false · default: 30000
Worker self-terminates after this many ms idle (frees memory); the next run() transparently recreates it.
retries
number · default: 0
Automatic retries on rejection — never applied to cancellations (AbortError always rejects immediately). See Retry Strategy with Backoff.
retryDelay
(attempt: number) => number
Delay before each retry — see Retry Strategy with Backoff.
hardCancelOnAbort
boolean · default: false
On abort(), terminate the worker immediately instead of waiting for cooperative ctx.signal handling; the next run() creates a fresh one. This is an option of the instance — RunOptions.hardCancelOnAbort is honoured by the pool, not by useWorker(). Without it, a handler stuck in a synchronous loop keeps the worker busy after run() has already rejected — see Cooperative cancellation.
concurrency
'parallel' | 'latest' | 'serial' · default: 'parallel'
What a new run() does while others are active — start at once, supersede the older ones, or wait in line. See Concurrency modes.
unwrap
'shallow' | 'deep' · default: 'shallow'
'shallow' unwraps only the top-level reactive input. 'deep' first copies plain objects and arrays recursively, so reactive proxies nested anywhere inside the input are removed too (cycles are kept; Date, typed arrays and other non-plain values are passed as they are). Can be overridden per call with RunOptions.unwrap. When a nested proxy makes postMessage fail, the DataCloneError in development mode names the path to the first one (for example input.items[2].meta) and suggests unwrap: 'deep'.
cache
{ cache: 'lru', maxCacheSize?: number }
Memoizes results by input — see Memoization / Result Cache.
streaming
boolean · default: false
Enables ctx.reportChunk()/chunks — see Streaming / Chunked Results.
Return value
run(input, options?)
(input: In, options?: RunOptions) => Promise<Out>
RunOptions for a single worker:
transfer?: Transferable[]— zero-copy transfer list for this callsignal?: AbortSignal— cancels this callonProgress?: (value: number) => void— receives this call's progress, in addition to the sharedprogressrefonChunk?: (chunk: unknown) => void— receives this call's chunks, in addition tochunkswhenstreaming: true; withoutstreamingit still receives them and no warning is loggedunwrap?: 'shallow' | 'deep'— overrides the instance option for this call
RunOptions also has hardCancelOnAbort and affinity; both apply to pools only.
call(name, input, options?)
Present instead of run() for a module made with defineWorkerOperations(). Name, input and output types are inferred; options is the same RunOptions.
isRunning
ComputedRef<boolean>
progress
ShallowRef<number>
error
ShallowRef<WorkerError | null>
cancel()
() => void
Aborts the current run() call(s) that didn't receive their own signal.
warmup()
() => Promise<void>
Pre-creates the worker without executing a task (useful for avoiding cold-start latency).
terminate()
() => void
Terminates the underlying worker immediately, if one exists — the next run()/warmup() transparently creates a fresh one. For a single worker terminate() only releases resources; it is not a permanent close. (A pool is different: after pool.terminate() it rejects new tasks until restart().) Call it directly when you need to tear a worker down on demand instead of waiting for idleTimeout or an abort — this matters in particular for a useWorker() created outside an active effect scope (for example inside a Pinia store or a plain factory function), where onScopeDispose never fires and the worker would otherwise only ever stop via the idle timer or hardCancelOnAbort.
chunks
ShallowRef<unknown[]> | undefined
Present only when streaming: true, and reactive — see Streaming / Chunked Results for how updates are batched.
Automatic terminate() on onScopeDispose when useWorker() is called inside an active effect scope — call terminate() yourself when there is no such scope.