diff --git a/src/net-utils.ts b/src/net-utils.ts index 4a29f05..d90a653 100644 --- a/src/net-utils.ts +++ b/src/net-utils.ts @@ -68,15 +68,32 @@ export async function openBrowserWhenReady( terminal?: vscode.Terminal, timeout: number = 10000 ): Promise { + if (configShinyPreviewType() === "none") { + // No need to wait for Shiny app to start or open the browser + return; + } + const portsOpenResult = await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, title: "Waiting for Shiny app to start...", cancellable: false, }, - async () => { + async (progress: vscode.Progress<{ increment: number }>) => { + let lastProgressReport = Date.now(); + + const reportProgress = () => { + const now = Date.now(); + const increment = ((now - lastProgressReport) / timeout) * 100; + progress.report({ increment }); + lastProgressReport = now; + }; + const portsOpen = [port, ...additionalPorts].map((p) => - retryUntilTimeout(timeout, () => isPortOpen("127.0.0.1", p)) + retryUntilTimeout(timeout, () => { + reportProgress(); + return isPortOpen("127.0.0.1", p); + }) ); const portsOpenPromise = Promise.all(portsOpen); @@ -92,14 +109,14 @@ export async function openBrowserWhenReady( ); if (!Array.isArray(portsOpenResult) || terminal?.exitStatus !== undefined) { - console.warn("[shiny] Terminal has been closed, not launching browser"); + console.warn("[shiny] Terminal has been closed, will not launch browser"); return; } if (portsOpenResult.filter((p) => !p).length > 0) { const timeoutStr = Math.floor(timeout / 1000); const action = await vscode.window.showErrorMessage( - `Shiny app took longer than ${timeoutStr}s to start, not launching browser.`, + `Shiny app took longer than ${timeoutStr}s to start so we have not opened the preview.`, terminal ? "Show Shiny process" : "", "Keep waiting" ); @@ -125,10 +142,14 @@ export async function openBrowserWhenReady( await openBrowser(previewUrl); } +function configShinyPreviewType(): string { + return ( + vscode.workspace.getConfiguration().get("shiny.previewType") || "internal" + ); +} + export async function openBrowser(url: string): Promise { - const previewType = vscode.workspace - .getConfiguration() - .get("shiny.previewType"); + const previewType = configShinyPreviewType(); switch (previewType) { case "none":