Skip to content

Commit

Permalink
Wait for content to load after workspace loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed Dec 3, 2024
1 parent edab7d6 commit 5e062f1
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/hooks/project-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const ProjectProvider = ({

const project = useStore((s) => s.project);
const editorReadyPromiseRef = usePromiseRef<void>();
const editorContentLoadedPromiseRef = usePromiseRef<void>();
const initialProjects = useCallback(() => {
logging.log(
`[MakeCode] Initialising with header ID: ${project.header?.id}`
Expand All @@ -147,14 +148,23 @@ export const ProjectProvider = ({
const startUpTimestamp = useRef<number>(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<void> | undefined) => {
Expand Down Expand Up @@ -434,6 +444,7 @@ export const ProjectProvider = ({
onWorkspaceSave,
onDownload,
onBack,
onEditorContentLoaded,
onWorkspaceLoaded,
},
}),
Expand All @@ -450,6 +461,7 @@ export const ProjectProvider = ({
onWorkspaceSave,
onDownload,
onBack,
onEditorContentLoaded,
onWorkspaceLoaded,
]
);
Expand Down

0 comments on commit 5e062f1

Please sign in to comment.