useWebWorker ​
Simple Web Workers registration and communication.
Usage ​
js
import { useWebWorker } from '@vueuse/core'
const { data, post, terminate, worker } = useWebWorker('/path/to/worker.js')
State | Type | Description |
---|---|---|
data | Ref<any> | Reference to the latest data received via the worker, can be watched to respond to incoming messages |
worker | ShallowRef<Worker | undefined> | Reference to the instance of the WebWorker |
Method | Signature | Description |
---|---|---|
post | (message: any, transfer: Transferable[]): void (message: any, options?: StructuredSerializeOptions | undefined): void | Sends data to the worker thread. |
terminate | () => void | Stops and terminates the worker. |
Type Declarations ​
typescript
type PostMessage = (typeof Worker.prototype)["postMessage"]
export interface UseWebWorkerReturn<Data = any> {
data: Ref<Data>
post: PostMessage
terminate: () => void
worker: ShallowRef<Worker | undefined>
}
type WorkerFn = (...args: unknown[]) => Worker
/**
* Simple Web Workers registration and communication.
*
* @see https://vueuse.org/useWebWorker
* @param url
* @param workerOptions
* @param options
*/
export declare function useWebWorker<T = any>(
url: string,
workerOptions?: WorkerOptions,
options?: ConfigurableWindow,
): UseWebWorkerReturn<T>
/**
* Simple Web Workers registration and communication.
*
* @see https://vueuse.org/useWebWorker
* @param worker
*/
export declare function useWebWorker<T = any>(
worker: Worker | WorkerFn,
): UseWebWorkerReturn<T>
Source ​
Contributors ​
Anthony Fu
Antério Vieira
Anthony Fu
René Hellenes
Yasser Lahbibi
DarknessChaser
Young
karma
Shinigami
Mario Kolli
Shinigami
wheat
Alex Kozack
userquin
Changelog ​
v12.0.0-beta.1
on 11/21/2024v10.2.0
on 6/16/2023v10.0.0-beta.5
on 4/13/2023cb644
- refactor!: remove isFunction
and isString
utilsv9.4.0
on 10/25/2022