daedalOS/hooks/useWorker.ts
2022-07-31 21:56:07 -07:00

32 lines
745 B
TypeScript

import { useEffect, useRef } from "react";
const useWorker = <T>(
workerInit?: (info?: string) => Worker,
onMessage?: (message: MessageEvent<T>) => void,
workerInfo?: string
): React.MutableRefObject<Worker | undefined> => {
const worker = useRef<Worker>();
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;