Skip to content

Ensure only one reload extension dialog can be shown at a time #1473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/ui/ReloadExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,49 @@

import * as vscode from "vscode";
import { Workbench } from "../utilities/commands";
// eslint-disable-next-line @typescript-eslint/no-require-imports
import debounce = require("lodash.debounce");

/**
* Prompts the user to reload the extension in cases where we are unable to do
* so automatically.
* so automatically. Only one of these prompts will be shown at a time.
*
* @param message the warning message to display to the user
* @param items extra buttons to display
* @returns the selected button or undefined if cancelled
*/
export async function showReloadExtensionNotification<T extends string>(
message: string,
...items: T[]
): Promise<"Reload Extensions" | T | undefined> {
const buttons: ("Reload Extensions" | T)[] = ["Reload Extensions", ...items];
const selected = await vscode.window.showWarningMessage(message, ...buttons);
if (selected === "Reload Extensions") {
await vscode.commands.executeCommand(Workbench.ACTION_RELOADWINDOW);
}
return selected;
export function showReloadExtensionNotificationInstance<T extends string>() {
let inFlight: Promise<"Reload Extensions" | T | undefined> | null = null;

return async function (
message: string,
...items: T[]
): Promise<"Reload Extensions" | T | undefined> {
if (inFlight) {
return inFlight;
}

const buttons: ("Reload Extensions" | T)[] = ["Reload Extensions", ...items];
inFlight = (async () => {
try {
const selected = await vscode.window.showWarningMessage(message, ...buttons);
if (selected === "Reload Extensions") {
await vscode.commands.executeCommand(Workbench.ACTION_RELOADWINDOW);
}
return selected;
} finally {
inFlight = null;
}
})();

return inFlight;
};
}

// In case the user closes the dialog immediately we want to debounce showing it again
// for 10 seconds to prevent another popup perhaps immediately appearing.
export const showReloadExtensionNotification = debounce(
showReloadExtensionNotificationInstance(),
10_000,
{ leading: true }
);
25 changes: 24 additions & 1 deletion test/unit-tests/ui/ReloadExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import { beforeEach } from "mocha";
import { expect } from "chai";
import { mockGlobalObject } from "../../MockUtils";
import * as vscode from "vscode";
import { showReloadExtensionNotification } from "../../../src/ui/ReloadExtension";
import { showReloadExtensionNotificationInstance } from "../../../src/ui/ReloadExtension";
import { Workbench } from "../../../src/utilities/commands";

suite("showReloadExtensionNotification()", async function () {
const mockedVSCodeWindow = mockGlobalObject(vscode, "window");
const mockedVSCodeCommands = mockGlobalObject(vscode, "commands");
let showReloadExtensionNotification: (
message: string,
...items: string[]
) => Promise<string | undefined>;

beforeEach(() => {
showReloadExtensionNotification = showReloadExtensionNotificationInstance();
});

test("displays a warning message asking the user if they would like to reload the window", async () => {
mockedVSCodeWindow.showWarningMessage.resolves(undefined);
Expand Down Expand Up @@ -57,4 +66,18 @@ suite("showReloadExtensionNotification()", async function () {
);
expect(mockedVSCodeCommands.executeCommand).to.not.have.been.called;
});

test("only shows one dialog at a time", async () => {
mockedVSCodeWindow.showWarningMessage.resolves(undefined);

await Promise.all([
showReloadExtensionNotification("Want to reload?"),
showReloadExtensionNotification("Want to reload?"),
]);

expect(mockedVSCodeWindow.showWarningMessage).to.have.been.calledOnceWithExactly(
"Want to reload?",
"Reload Extensions"
);
});
});
Loading