building-detection / src /hooks /useGeoAIWorker.js
mhassanch's picture
add building detection demo
1ed908c
import { useState, useEffect, useRef } from "react";
export function useGeoAIWorker() {
const workerRef = useRef(null);
const [isReady, setIsReady] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);
const [result, setResult] = useState(null);
useEffect(() => {
workerRef.current = new Worker(new URL("./worker.js", import.meta.url));
workerRef.current.onmessage = e => {
const { type, payload } = e.data;
switch (type) {
case "ready":
setIsReady(true);
break;
case "result":
setResult(payload);
setIsProcessing(false);
break;
case "error":
console.error("Worker error:", payload);
setIsProcessing(false);
break;
}
};
return () => workerRef.current?.terminate();
}, []);
const initialize = (tasks, providerParams) => {
workerRef.current?.postMessage({
type: "init",
payload: { tasks, providerParams },
});
};
const runInference = params => {
if (!isReady) return;
setIsProcessing(true);
workerRef.current?.postMessage({
type: "inference",
payload: params,
});
};
return { isReady, isProcessing, result, initialize, runInference };
}