Skip to content
On this page

useWebWorker

Category
Export Size
927 B
Last Changed
2 months ago
Related

Simple Web Workers registration and communication.

Usage

js
import { useWebWorker } from '@vueuse/core'

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

const { data, post, terminate, worker } = 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
workerShallowRef<Worker | undefined>Reference to the instance of the WebWorker
MethodSignatureDescription
post(data: any) => voidSends data to the worker thread.
terminate() => voidStops and terminates the worker.

Type Declarations

typescript
export interface UseWebWorkerReturn<Data = any> {
  data: Ref<Data>
  post: typeof Worker.prototype["postMessage"]
  terminate: () => void
  worker: ShallowRef<Worker | undefined>
}
declare 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>
export interface UseWebWorkerReturn<Data = any> {
  data: Ref<Data>
  post: typeof Worker.prototype["postMessage"]
  terminate: () => void
  worker: ShallowRef<Worker | undefined>
}
declare 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

SourceDocs

Contributors

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

Changelog

v9.4.0 on 10/25/2022
c8849 - feat: support overloads (#2259)
v6.9.2 on 11/19/2021
d19a6 - fix: explicit define return interface (#935)

Released under the MIT License.