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.