From 6976e02a384e0d7d909665e57822abc9ce2a9668 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Thu, 1 Feb 2024 09:44:19 +0000 Subject: [PATCH] Store the current value of `openDocId` in a ref for use in `openDoc` (#16853) GitOrigin-RevId: 853a857eed832fde0be05fef3e149c875fd0c940 --- .../ide-react/context/editor-manager-context.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx index 5899ec86ff..95196b78f7 100644 --- a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx +++ b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx @@ -390,6 +390,11 @@ export const EditorManagerProvider: FC = ({ children }) => { [attachErrorHandlerToDocument, doOpenNewDocument, currentDocument] ) + const openDocIdRef = useRef(openDocId) + useEffect(() => { + openDocIdRef.current = openDocId + }, [openDocId]) + const openDoc = useCallback( async (doc: Doc, options: OpenDocOptions = {}) => { debugConsole.log(`[openDoc] Opening ${doc._id}`) @@ -421,15 +426,16 @@ export const EditorManagerProvider: FC = ({ children }) => { } } - // If we already have the document open we can return at this point. + // If we already have the document open, or are opening the document, we can return at this point. // Note: only use forceReopen:true to override this when the document is // out of sync and needs to be reloaded from the server. - if (doc._id === openDocId && !options.forceReopen) { + if (doc._id === openDocIdRef.current && !options.forceReopen) { done(false) return } // We're now either opening a new document or reloading a broken one. + openDocIdRef.current = doc._id as DocId setOpenDocId(doc._id as DocId) setOpenDocName(doc.name) setOpening(true) @@ -458,7 +464,6 @@ export const EditorManagerProvider: FC = ({ children }) => { [ eventEmitter, jumpToLine, - openDocId, openNewDocument, setCurrentDocument, setOpenDocId,