From 5e062f14d639d53aaf57fb35505e40d8e846874b Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 3 Dec 2024 14:26:56 +0000 Subject: [PATCH] Wait for content to load after workspace loaded --- src/hooks/project-hooks.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/hooks/project-hooks.tsx b/src/hooks/project-hooks.tsx index 7e43b3c48..b518f8648 100644 --- a/src/hooks/project-hooks.tsx +++ b/src/hooks/project-hooks.tsx @@ -135,6 +135,7 @@ export const ProjectProvider = ({ const project = useStore((s) => s.project); const editorReadyPromiseRef = usePromiseRef(); + const editorContentLoadedPromiseRef = usePromiseRef(); const initialProjects = useCallback(() => { logging.log( `[MakeCode] Initialising with header ID: ${project.header?.id}` @@ -147,14 +148,23 @@ export const ProjectProvider = ({ const startUpTimestamp = useRef(Date.now()); const onWorkspaceLoaded = useCallback(async () => { - // Wait another second because it's not really ready at this point. - await new Promise((res) => setTimeout(res, 1000)); logging.log("[MakeCode] Workspace loaded"); - + await editorContentLoadedPromiseRef.current.promise; // Get latest start up state and only mark editor ready if editor has not timed out. getEditorStartUp() !== "timed out" && editorReady(); editorReadyPromiseRef.current.resolve(); - }, [editorReady, editorReadyPromiseRef, getEditorStartUp, logging]); + }, [ + editorContentLoadedPromiseRef, + editorReady, + editorReadyPromiseRef, + getEditorStartUp, + logging, + ]); + + const onEditorContentLoaded = useCallback(() => { + logging.log("[MakeCode] Editor content loaded"); + editorContentLoadedPromiseRef.current.resolve(); + }, [editorContentLoadedPromiseRef, logging]); const checkIfEditorStartUpTimedOut = useCallback( async (promise: Promise | undefined) => { @@ -434,6 +444,7 @@ export const ProjectProvider = ({ onWorkspaceSave, onDownload, onBack, + onEditorContentLoaded, onWorkspaceLoaded, }, }), @@ -450,6 +461,7 @@ export const ProjectProvider = ({ onWorkspaceSave, onDownload, onBack, + onEditorContentLoaded, onWorkspaceLoaded, ] );