@@ -16,6 +16,7 @@ import {
1616 RefObject ,
1717 useCallback ,
1818 useContext ,
19+ useEffect ,
1920 useMemo ,
2021 useRef ,
2122} from "react" ;
@@ -306,12 +307,39 @@ export const ProjectProvider = ({
306307 setProjectEdited ,
307308 ]
308309 ) ;
310+
311+ const hideSimulatorPromiseRef = useRef < Promise < void > | null > ( null ) ;
312+
313+ const hideSimulator = useCallback ( async ( ) => {
314+ hideSimulatorPromiseRef . current =
315+ driverRef . current ?. hideSimulator ( ) ?? null ;
316+ await hideSimulatorPromiseRef . current ;
317+ } , [ driverRef ] ) ;
318+
319+ const initAsyncCalled = useRef ( false ) ;
320+ useEffect ( ( ) => {
321+ const initAsync = async ( ) => {
322+ // Hide simulator when not on code page to avoid it from making noise
323+ // when editor is not visible. Hiding it prevents it from loading.
324+ if ( window . location . pathname !== createCodePageUrl ( ) ) {
325+ initAsyncCalled . current = true ;
326+ await doAfterEditorUpdate ( ( ) => Promise . resolve ( ) ) ;
327+ await hideSimulator ( ) ;
328+ }
329+ } ;
330+ if ( ! initAsyncCalled . current ) {
331+ void initAsync ( ) ;
332+ }
333+ return ;
334+ } , [ doAfterEditorUpdate , driverRef , hideSimulator ] ) ;
335+
309336 const browserNavigationToEditor = useCallback ( async ( ) => {
310337 try {
311338 setProjectEdited ( ) ;
312- await doAfterEditorUpdate ( ( ) => {
313- return Promise . resolve ( ) ;
314- } ) ;
339+ await doAfterEditorUpdate ( ( ) => Promise . resolve ( ) ) ;
340+ await hideSimulatorPromiseRef . current ;
341+ // Show simulator when editor is visible.
342+ await driverRef . current ?. showSimulator ( ) ;
315343 return true ;
316344 } catch ( e ) {
317345 if ( e instanceof CodeEditorError ) {
@@ -323,7 +351,7 @@ export const ProjectProvider = ({
323351 logging . error ( e ) ;
324352 return false ;
325353 }
326- } , [ doAfterEditorUpdate , logging , setProjectEdited ] ) ;
354+ } , [ doAfterEditorUpdate , driverRef , logging , setProjectEdited ] ) ;
327355 const resetProject = useStore ( ( s ) => s . resetProject ) ;
328356 const loadDataset = useStore ( ( s ) => s . loadDataset ) ;
329357 const loadFile = useCallback (
@@ -473,7 +501,8 @@ export const ProjectProvider = ({
473501 focusVisible : openedViaKeyboardRef . current ,
474502 } ;
475503 navigate ( createTestingModelPageUrl ( ) , { state } ) ;
476- } , [ navigate ] ) ;
504+ void hideSimulator ( ) ;
505+ } , [ hideSimulator , navigate ] ) ;
477506 const onSave = saveHex ;
478507 const downloadActions = useDownloadActions ( ) ;
479508 const onDownload = useCallback (
0 commit comments