@@ -11,26 +11,33 @@ function App() {
1111 useEffect ( ( ) => {
1212 // Load the WASM-related JavaScript file dynamically when modal is shown
1313 if ( showModal ) {
14- const script = document . createElement ( 'script' ) ;
15- script . src = '/wasm/sorting_algorithms.js' ; // Path to your WASM JS file
16- script . async = true ;
17- script . onload = ( ) => {
18- console . log ( "WASM script loaded successfully." ) ;
14+ const existingScript = document . querySelector ( 'script[src="/wasm/sorting_algorithms.js"]' ) ;
15+ if ( ! existingScript ) {
16+ const script = document . createElement ( 'script' ) ;
17+ script . src = '/wasm/sorting_algorithms.js' ; // Path to your WASM JS file
18+ script . async = true ;
19+ script . onload = ( ) => {
20+ console . log ( "WASM script loaded successfully." ) ;
1921
20- // Ensure the canvas is available before initializing WebAssembly
21- const canvasElement = document . getElementById ( 'canvas' ) ;
22- if ( canvasElement ) {
23- console . log ( "Canvas element is available." ) ;
24- // Initialize the WASM module here, ensuring it works with the canvas
25- if ( window . Module ) {
26- window . Module . canvas = canvasElement ;
22+ // Ensure the canvas is available before initializing WebAssembly
23+ const canvasElement = document . getElementById ( 'canvas' ) ;
24+ if ( canvasElement ) {
25+ console . log ( "Canvas element is available." ) ;
26+ // Initialize the WASM module here, ensuring it works with the canvas
27+ if ( window . Module ) {
28+ window . Module . canvas = canvasElement ;
29+ }
2730 }
28- }
29- } ;
30- document . body . appendChild ( script ) ;
31+ } ;
32+ document . body . appendChild ( script ) ;
33+ }
3134
3235 return ( ) => {
33- document . body . removeChild ( script ) ; // Cleanup the script tag when modal is closed
36+ // Cleanup the script tag when modal is closed
37+ const scriptToRemove = document . querySelector ( 'script[src="/wasm/sorting_algorithms.js"]' ) ;
38+ if ( scriptToRemove ) {
39+ document . body . removeChild ( scriptToRemove ) ;
40+ }
3441 } ;
3542 }
3643 } , [ showModal ] ) ;
0 commit comments