Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useWebWorker.md
1---2category: Browser3related: useWebWorkerFn4---56# useWebWorker78Simple [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) registration and communication.910## Usage1112```ts13import { useWebWorker } from '@vueuse/core'1415const { data, post, terminate, worker } = useWebWorker('/path/to/worker.js')16```1718| State | Type | Description |19| ------ | --------------------------------- | ---------------------------------------------------------------------------------------------------- |20| data | `Ref<any>` | Reference to the latest data received via the worker, can be watched to respond to incoming messages |21| worker | `ShallowRef<Worker \| undefined>` | Reference to the instance of the WebWorker |2223| Method | Signature | Description |24| --------- | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |25| post | `(message: any, transfer: Transferable[]): void`<br>`(message: any, options?: StructuredSerializeOptions \| undefined): void` | Sends data to the worker thread. |26| terminate | `() => void` | Stops and terminates the worker. |2728## Type Declarations2930```ts31type PostMessage = (typeof Worker.prototype)["postMessage"]32export interface UseWebWorkerReturn<Data = any> {33data: Ref<Data>34post: PostMessage35terminate: () => void36worker: ShallowRef<Worker | undefined>37}38type WorkerFn = (...args: unknown[]) => Worker39/**40* Simple Web Workers registration and communication.41*42* @see https://vueuse.org/useWebWorker43* @param url44* @param workerOptions45* @param options46*/47export declare function useWebWorker<T = any>(48url: string,49workerOptions?: WorkerOptions,50options?: ConfigurableWindow,51): UseWebWorkerReturn<T>52/**53* Simple Web Workers registration and communication.54*55* @see https://vueuse.org/useWebWorker56*/57export declare function useWebWorker<T = any>(58worker: Worker | WorkerFn,59): UseWebWorkerReturn<T>60```61