Skip to content

v1.0.11

Latest

Choose a tag to compare

@debabin debabin released this 17 Jul 15:33

Add web worker hooks

This release introduces useWebWorker, a reactive wrapper around a web worker that surfaces its messages and errors as state, and useWebWorkerCallback, which runs a self-contained function in a worker without a separate worker file and returns a promise.

Features

  • useWebWorker — added a hook that wraps a web worker, exposing data, error, terminated, post, restart, and terminate, and accepting the native name, type, and credentials options alongside onMessage and onError callbacks.
  • useWebWorker — the hook accepts either a script URL or an existing Worker instance, taking ownership of the instance so it is terminated on unmount, and recreates the worker whenever the source or the native options change.
  • useWebWorker — added a restart method to recreate a worker after it has been terminated, since post is silently ignored once the worker is gone and there is otherwise no way back.
  • useWebWorker — errors from error and messageerror events are captured in error, which is the only channel for failures that happen inside the worker thread and can never be caught with try/catch around post.
  • useWebWorkerCallback — added a hook that runs a callback in a worker built from its source at call time, exposing pending, run, and terminate, with run returning a promise that resolves with the callback result or rejects with its error.
  • useWebWorkerCallback — the worker and its object URL are created per call and released once the promise settles, and only one call may be in flight at a time, so a second run rejects while the first is still running.