R
fn

useWebWorker

v0.0.15testeddemo

Simple Web Worker communication with reactive incoming data and automatic teardown

Examples

ts
const { data, post, terminate } = useWebWorker('/worker.js');
post({ type: 'start' });
watch(data, value => console.log(value));
ts
const { data } = useWebWorker(() => new Worker(new URL('./worker.ts', globalThis._importMeta_.url)));

Demo

Loading demo…

Signature

ts
export function useWebWorker<Data = any>(
  source: WebWorkerSource,
  options: UseWebWorkerOptions ={ ... }

Type Parameters

Data= any

Parameters

ParameterTypeDescription
sourceWebWorkerSourceA `Worker` instance, a script URL/string, or a factory returning a `Worker`
options?UseWebWorkerOptionsWorker constructor options and message/error hooks

Returns

UseWebWorkerReturn<Data>`{ data, post, terminate, worker, isSupported }`