/** * Status of the web worker */ type WorkerStatus = "idle" | "running" | "success" | "error" | "terminated"; /** * Return value for the useWebWorker hook */ interface UseWebWorkerReturnValue { /** * Post a message to the web worker * @param message - The message to send to the worker */ postMessage: (message: any) => void; /** * Terminate the web worker */ terminate: () => void; /** * The current status of the web worker */ status: WorkerStatus; /** * The last data received from the worker */ data: T | null; /** * Any error that occurred */ error: Error | null; /** * Whether Web Workers are supported */ isSupported: boolean; } /** * useWebWorker hook * * Simplified Web Worker management with message passing. * Provides an easy way to offload heavy computations to a background thread * without blocking the main UI thread. * * @param workerUrl - The URL to the worker script file * @returns Object containing worker operations and state * * @example * ```tsx * import { useWebWorker } from "rooks"; * * function HeavyComputationComponent() { * const { postMessage, data, status, error, terminate, isSupported } = * useWebWorker("/worker.js"); * * const handleCompute = () => { * postMessage({ type: "compute", value: 1000000 }); * }; * * if (!isSupported) { * return
Web Workers not supported
; * } * * return ( *
* * *

Status: {status}

* {data !== null &&

Result: {data}

} * {error &&

Error: {error.message}

} *
* ); * } * ``` * * @see https://rooks.vercel.app/docs/hooks/useWebWorker */ declare function useWebWorker(workerUrl: string): UseWebWorkerReturnValue; export { useWebWorker }; export type { UseWebWorkerReturnValue, WorkerStatus };