Worker Handler
defineWorkerHandler() — worker-side. Wires the run/cancel message protocol automatically — you only write the handler function.
import { defineWorkerHandler, type WorkerContext } from 'vue-worker-kit/worker'
export default defineWorkerHandler(async (input: In, ctx: WorkerContext): Promise<Out> => {
// ...
})defineWorkerHandler only starts a message loop when it actually runs inside a dedicated- or shared-worker global scope (checked via self instanceof DedicatedWorkerGlobalScope/SharedWorkerGlobalScope). Importing the file anywhere else — e.g. accidentally from the main bundle — is a no-op. The same file works for both new Worker(...) (via useWorker()/createWorkerPool()) and new SharedWorker(...) (via useSharedWorker()).
You don't need to call reportProgress(1) yourself right before returning — a final, unthrottled progress update of 1 is always sent right before the result, regardless of what your last throttled call was. Without this, a handler that only reports at periodic checkpoints (e.g. every 5%) could leave the main thread's progress stuck below 1 forever, since the checkpoint closest to the end can land inside the previous call's throttle window and get silently dropped.
Context (WorkerContext)
The second argument the handler function receives.
signal
AbortSignal
Aborted when the task is cancelled from the main thread — checking it is optional, cancellation is cooperative.
reportProgress(value)
(value: number) => void — value in 0..1
Sends progress to the main thread, throttled to ~20 messages/sec.
transfer(...transferables)
(...transferables: Transferable[]) => void
Marks objects to send back zero-copy with the result instead of structured-clone copying — see Transferables.
reportChunk(chunk)
(chunk: unknown) => void
Sends an intermediate result, unthrottled — see Streaming / Chunked Results.
throwIfAborted()
() => void
Throws an AbortError if the run was cancelled. A plain check — it does not give the event loop a turn.
yield()
() => Promise<void>
Hands control back to the worker's event loop (so a pending cancel message can be delivered) and then calls throwIfAborted(). Put await ctx.yield() inside long synchronous loops, every few milliseconds of work — see Cooperative cancellation.
defineWorkerOperations()
Several named operations in one worker file:
import { defineWorkerOperations } from 'vue-worker-kit/worker'
export default defineWorkerOperations({
parse: (input: { text: string }) => ({ length: input.text.length }),
format: async (input: { value: number }, ctx) => `v=${input.value}`,
})Each operation receives the same ctx. On the main thread the composable gets call(name, input, options?) with inferred names, inputs and outputs — see Typed operations. A request with an unknown name rejects with code: 'UNKNOWN_OPERATION'; the worker keeps serving.
WorkerHandlerError
Throw it from a handler to send data to the main thread:
import { WorkerHandlerError } from 'vue-worker-kit/worker'
throw new WorkerHandlerError('Bad row', { code: 'E_ROW', details: { row: 7 }, cause })The main thread receives a WorkerError with the same code and details, plus workerCause — the cause chain as { name, message } pairs, up to three levels. If details cannot be structured-cloned, it is replaced by a string description and detailsDropped is true. A plain Error or object with a string code and/or a details value is handled the same way.