Skip to content
On this page

useWebWorker

Category
Last Changed
3 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
Shinigami
Mario Kolli
Shinigami
wheat
Alex Kozack
userquin

Changelog

v6.9.2 on 11/19/2021
d19a6 - fix: explicit define return interface (#935)
v6.0.0-beta.2 on 8/9/2021
ff21b - feat: use tryOnScopeDispose instead of tryOnUnmounted
v4.11.2 on 5/30/2021
68c7d - feat(typedef): add return typedefs (#543) (#544)
v4.11.0 on 5/14/2021
cdd6d - feat(useWebWoker): return worker (#507)
v4.11.0 on 5/14/2021
c5cf0 - feat(useWebWoker): return worker (#507)
v4.4.1 on 3/14/2021
1a31f - fix: SSR compatibility
v4.3.6 on 3/12/2021
0c04f - feat: allow provide WorkerOptions for the Worker, closes #372 (#373)
useWebWorker has loaded