import { useEffect, useRef } from "react"; const useWorker = ( workerInit?: (info?: string) => Worker, onMessage?: (message: MessageEvent) => void, workerInfo?: string ): React.MutableRefObject => { const worker = useRef(); useEffect(() => { if (workerInit && !worker.current) { worker.current = workerInit(workerInfo); if (onMessage) { worker.current.addEventListener("message", onMessage, { passive: true, }); } worker.current.postMessage("init"); } return () => { worker.current?.terminate(); worker.current = undefined; }; }, [onMessage, workerInfo, workerInit]); return worker; }; export default useWorker;