fn
useWebWorker
v0.0.14testeddemoSimple 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', import.meta.url)));Demo
Loading demo…
Signature
ts
export function useWebWorker<Data = unknown>(
source: WebWorkerSource,
options: UseWebWorkerOptions ={ ... }Type Parameters
Data= unknownParameters
| Parameter | Type | Description |
|---|---|---|
source | WebWorkerSource | A Worker instance, a script URL/string, or a factory returning a Worker |
options? | UseWebWorkerOptions | Worker constructor options and message/error hooks |
Returns
UseWebWorkerReturn<Data>{ data, post, terminate, worker, isSupported }| Property | Type | Description |
|---|---|---|
data | ShallowRef<Data | null> | The latest data received from the worker via postMessage |
post | WorkerPostMessage | Send a message to the worker. No-op when the worker is unavailable. |
terminate | VoidFunction | Terminate the worker immediately and release the reference. |
worker | ShallowRef<Worker | undefined> | The underlying Worker instance, or undefined when unsupported / terminated. |
isSupported | ShallowRef<boolean> | Whether Worker is available in the current environment (SSR-safe). |