From 1566adcaeb3bc5258a563b208a511cdfb3938d7a Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Thu, 12 Dec 2024 10:13:35 -0800 Subject: [PATCH] fix: wait for sidebar before other commands --- extensions/vscode/src/commands.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index c02055dcc..88e3abbb3 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -284,6 +284,28 @@ async function processDiff( } } +function waitForSidebarReady( + sidebar: ContinueGUIWebviewViewProvider, + timeout: number, + interval: number, +): Promise { + return new Promise((resolve) => { + const startTime = Date.now(); + + const checkReadyState = () => { + if (sidebar.isReady) { + resolve(true); + } else if (Date.now() - startTime >= timeout) { + resolve(false); // Timed out + } else { + setTimeout(checkReadyState, interval); + } + }; + + checkReadyState(); + }); +} + // Copy everything over from extension.ts const getCommandsMap: ( ide: VsCodeIde, @@ -437,9 +459,14 @@ const getCommandsMap: ( // This is a temporary fix—sidebar.webviewProtocol.request is blocking // when the GUI hasn't yet been setup and we should instead be // immediately throwing an error, or returning a Result object + if (!sidebar.isReady) { focusGUI(); - return; + + const isReady = await waitForSidebarReady(sidebar, 5000, 100); + if (!isReady) { + return; + } } const historyLength = await sidebar.webviewProtocol.request(