Skip to content

Commit

Permalink
set interval to avoid block
Browse files Browse the repository at this point in the history
  • Loading branch information
iFwu committed Oct 7, 2024
1 parent c817ce6 commit 9baa0c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/hooks/useOpenCV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,28 @@ export function useOpenCV() {
}
};
useEffect(() => {
let intervalId: number;
const initialize = async () => {
if (typeof cv !== 'undefined') {
if (window.cv) {
await new Promise<void>((resolve) => {
cv.onRuntimeInitialized = resolve;
});
await onOpenCVReady();
} else {
document.addEventListener('opencv-loaded', onOpenCVReady);
window.addEventListener('opencv-loaded', onOpenCVReady);
intervalId = setInterval(() => {
if (!isLoading) {
onOpenCVReady();
}
}, 500);
}
}

initialize();

return () => {
document.removeEventListener('opencv-loaded', onOpenCVReady);
window.removeEventListener('opencv-loaded', onOpenCVReady);
clearInterval(intervalId);
};
}, []);

Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default defineConfig({
injectTo: 'body',
attrs: {
async: true,
onload: 'cv.onRuntimeInitialized = () => { document.dispatchEvent(new Event(\'opencv-loaded\')); }',
onload: 'cv.onRuntimeInitialized = () => { window.dispatchEvent(new Event(\'opencv-loaded\')); }',
},
};
},
Expand Down

0 comments on commit 9baa0c3

Please sign in to comment.