Skip to content

useWebWorker

简单的 网络工作者 注册和通讯。

¥Simple Web Workers registration and communication.

用法

¥Usage

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

const { data, post, terminate, worker } = useWebWorker('/path/to/worker.js')
状态类型描述
dataRef<any>参考通过工作线程接收到的最新数据,可以监视以响应传入的消息
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
/**
 * 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>

源代码

源代码文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号