fix: remove cleanup from from wasm canvas
This commit is contained in:
parent
bf5900edbb
commit
e7b44ba112
1 changed files with 16 additions and 43 deletions
|
|
@ -1,53 +1,26 @@
|
||||||
import { useEffect, useRef, useCallback } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
let wasmStarted = false;
|
||||||
|
|
||||||
export function CgolCanvas() {
|
export function CgolCanvas() {
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
const initializedRef = useRef(false);
|
|
||||||
const cleanupRef = useRef<(() => void) | null>(null);
|
|
||||||
|
|
||||||
const initializeWasm = useCallback(async () => {
|
|
||||||
if (typeof window === "undefined") return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const canvas = canvasRef.current;
|
|
||||||
if (!canvas || initializedRef.current) return;
|
|
||||||
|
|
||||||
const cgolModule = await import("cgol");
|
|
||||||
|
|
||||||
// Initialize WASM module
|
|
||||||
const initFunction = cgolModule.default;
|
|
||||||
if (initFunction && typeof initFunction === "function") {
|
|
||||||
await initFunction();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start CGOL
|
|
||||||
if (typeof cgolModule.start === "function") {
|
|
||||||
cgolModule.start();
|
|
||||||
initializedRef.current = true;
|
|
||||||
|
|
||||||
const cleanupFn = (cgolModule as { cleanup?: () => void }).cleanup;
|
|
||||||
if (typeof cleanupFn === "function") {
|
|
||||||
cleanupRef.current = cleanupFn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error: unknown) {
|
|
||||||
console.error("Failed to initialize CGOL WebAssembly module:", error);
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Initialize immediately without delay
|
if (wasmStarted) return;
|
||||||
void initializeWasm();
|
wasmStarted = true;
|
||||||
|
|
||||||
return () => {
|
import("cgol").then(async (cgolModule) => {
|
||||||
// Call cleanup if available from WASM module
|
if (typeof cgolModule.default === "function") {
|
||||||
if (cleanupRef.current) {
|
await cgolModule.default();
|
||||||
cleanupRef.current();
|
|
||||||
}
|
}
|
||||||
// Reset initialization state on unmount
|
if (typeof cgolModule.start === "function") {
|
||||||
initializedRef.current = false;
|
cgolModule.start();
|
||||||
};
|
}
|
||||||
}, [initializeWasm]);
|
}).catch((error: unknown) => {
|
||||||
|
console.error("Failed to initialize CGOL WebAssembly module:", error);
|
||||||
|
wasmStarted = false;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<canvas
|
<canvas
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue