Skip to content
On this page

useWebWorker

Category
Last Changed
4 months ago

Simple Web Workers registration and communication.

Try out the high-level useWebWorkerFn

Usage

import { useWebWorker } from '@vueuse/core'

const { data, post, terminate } = useWebWorker('/path/to/worker.js')
import { useWebWorker } from '@vueuse/core'

const { data, post, terminate } = useWebWorker('/path/to/worker.js')
StateTypeDescription
dataRef<any>Reference to the latest data received via the worker, can be watched to respond to incoming messages
MethodSignatureDescription
post(data: any) => voidSends data to the worker thread.
terminate() => voidStops and terminates the worker.

Type Declarations

export interface UseWebWorkerReturn<Data = any> {
  data: Ref<Data>
  post: typeof Worker.prototype["postMessage"]
  terminate: () => void
  worker: Ref<Worker | undefined>
}
/**
 * Simple Web Workers registration and communication.
 *
 * @see https://vueuse.org/useWebWorker
 * @param url
 * @param workerOptions
 * @param options
 */
export declare function useWebWorker<Data = any>(
  url: string,
  workerOptions?: WorkerOptions,
  options?: ConfigurableWindow
): UseWebWorkerReturn<Data>
export interface UseWebWorkerReturn<Data = any> {
  data: Ref<Data>
  post: typeof Worker.prototype["postMessage"]
  terminate: () => void
  worker: Ref<Worker | undefined>
}
/**
 * Simple Web Workers registration and communication.
 *
 * @see https://vueuse.org/useWebWorker
 * @param url
 * @param workerOptions
 * @param options
 */
export declare function useWebWorker<Data = any>(
  url: string,
  workerOptions?: WorkerOptions,
  options?: ConfigurableWindow
): UseWebWorkerReturn<Data>

Source

SourceDocs

Contributors

Anthony Fu
Antério Vieira
karma
Shinigami
Mario Kolli
Shinigami
wheat
Alex Kozack
userquin

Changelog

v6.9.2 on 11/19/2021
d19a6 - fix: explicit define return interface (#935)
useWebWorker has loaded