Skip to content

useWebWorker

Category
Export Size
322 B
Last Changed
15 minutes ago
Related

简单的Web Workers注册和通信。

Usage

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

const { data, post, terminate, worker } = useWebWorker('/path/to/worker.js')
状态类型描述
dataRef<any>对通过 worker 接收的最新数据的引用,可以监视以响应传入消息
workerShallowRef<Worker | undefined>对 WebWorker 实例的引用
方法签名描述
post(message: any, transfer: Transferable[]): void
(message: any, options?: StructuredSerializeOptions | undefined): void
发送数据到工作线程。
terminate() => void停止并终止工作线程。

类型声明

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
/**
 * 简单的 Web Workers 注册和通信。
 *
 * @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>
/**
 * 简单的 Web Workers 注册和通信。
 *
 * @see https://vueuse.org/useWebWorker
 * @param worker
 */
export declare function useWebWorker<T = any>(
  worker: Worker | WorkerFn,
): UseWebWorkerReturn<T>

Source

SourceDocs

贡献者

Anthony Fu
丶远方
Antério Vieira
Anthony Fu
René Hellenes
Yasser Lahbibi
DarknessChaser
Young
karma
Shinigami
Mario Kolli
Shinigami
wheat
Alex Kozack
userquin

变更日志

v10.2.0 on 6/16/2023
5988f - fix: add web worker transferable option (#3123)
v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v9.4.0 on 10/25/2022
c8849 - feat: support overloads (#2259)

Released under the MIT License.