Skip to content

Commit

Permalink
Improve messaging of "took too long" message (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Aug 28, 2024
1 parent dc5e0ff commit 7cc3d79
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/net-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,32 @@ export async function openBrowserWhenReady(
terminal?: vscode.Terminal,
timeout: number = 10000
): Promise<void> {
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);
Expand All @@ -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"
);
Expand All @@ -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<void> {
const previewType = vscode.workspace
.getConfiguration()
.get("shiny.previewType");
const previewType = configShinyPreviewType();

switch (previewType) {
case "none":
Expand Down

0 comments on commit 7cc3d79

Please sign in to comment.