rewrk
v1.2.11
Published
Dead simple React web workers.
Downloads
8
Readme
Rewrk
Dead simple React web workers.
API
useWorker<T extends Record<string, unknown>>(dynamicImport: Promise<T>, options?: WorkerOptions): ProxiedWorkerMethods<T>
Creates a worker from a dynamic import and returns a proxy object that can be used to call the worker's exported functions.
Arguments
dynamicImport
- A function that returns dynamic import of the worker module. The module must export at least one function. Non-function exports will be stripped away.options?
- Options to pass to the worker constructor. By default thetype
option is set to"module"
.
Returns
A proxy object that can be used to call the worker's exported functions. The proxy object will be available immidiately despite the dynamic import returning a Promise.
Usage
import { useWorker } from "rewrk";
const WorkerComponent = () => {
const worker = useWorker(import("./worker"));
return <button onClick={() => worker.doSomething()}>Do something</button>;
};
License
MIT © Juan de Urtubey