fix(updater): improve download cancellation and stall handling

This commit is contained in:
rihkddd authored and GitHub committed 2026-07-31 03:09:19 +08:00
1 parent c697d0c7ad
commit 27d1ad53ce
18 files changed
+631 -110

No files matched your search

+17 -4
View File
@@ -40,6 +40,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
...initialState,
});
const downloadAndInstall = vi.fn();
const cancelDownload = vi.fn();
const container = document.createElement("div");
document.body.append(container);
const app = createApp(
@@ -82,6 +83,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
updateReady: state.updateReady,
activeTaskCount,
"onDownload-and-install": downloadAndInstall,
"onCancel-download": cancelDownload,
"onInstall-downloaded": handleInstallDownloaded,
});
},
@@ -92,7 +94,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
app.mount(container);
await flushDialog();
return { state, downloadAndInstall, installDownloaded };
return { state, downloadAndInstall, cancelDownload, installDownloaded };
}
function buttonWithText(text: string): HTMLButtonElement | undefined {
@@ -171,6 +173,17 @@ describe("UpdateDialog active task guard", () => {
});
describe("UpdateDialog close protection", () => {
it("cancels the background download when the close button is clicked", async () => {
const { state, cancelDownload } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 42 });
const closeButton = document.body.querySelector<HTMLButtonElement>('[data-slot="dialog-close"]');
closeButton?.click();
await flushDialog();
expect(cancelDownload).toHaveBeenCalledOnce();
expect(state.open).toBe(false);
});
it("allows closing while a downloaded update is idle", async () => {
const { state } = await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 });
@@ -232,7 +245,7 @@ describe("UpdateDialog close protection", () => {
expect(downloadAndInstall).not.toHaveBeenCalled();
});
it("keeps the successful install flow protected", async () => {
it("allows dismissing after a successful install while restart remains available", async () => {
const installDownloaded = vi.fn(async () => {});
const { state, downloadAndInstall } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
@@ -242,9 +255,9 @@ describe("UpdateDialog close protection", () => {
expect(state.updateDownloaded).toBe(false);
expect(state.updateReady).toBe(true);
expect(buttonWithText("Restart")).toBeDefined();
expect(buttonWithText("Cancel")).toBeUndefined();
expect(buttonWithText("Cancel")).toBeDefined();
await pressEscape();
expect(state.open).toBe(true);
expect(state.open).toBe(false);
expect(installDownloaded).toHaveBeenCalledOnce();
expect(downloadAndInstall).not.toHaveBeenCalled();
});
@@ -24,6 +24,7 @@ const props = defineProps<{
const emit = defineEmits<{
"open-latest-release": [];
"download-and-install": [];
"cancel-download": [];
"install-downloaded": [];
restart: [];
}>();
@@ -32,8 +33,24 @@ const { t } = useI18n();
const isDesktop = isTauriRuntime();
const renderedNotes = ref("");
// Keep a downloaded package retryable after install errors; only active transitions must trap the dialog.
const isCloseBlocked = computed(() => props.isDownloadingUpdate || props.isInstallingUpdate || props.updateReady);
// Only active file replacement (installation) must trap the dialog.
const isCloseBlocked = computed(() => props.isInstallingUpdate);
function handleCancel() {
handleOpenChange(false);
}
function handleOpenChange(nextOpen: boolean) {
if (nextOpen) {
open.value = true;
return;
}
if (isCloseBlocked.value) return;
if (props.isDownloadingUpdate) {
emit("cancel-download");
}
open.value = false;
}
function handleReleaseNotesClick(event: MouseEvent) {
const target = event.target as HTMLElement;
@@ -65,7 +82,7 @@ watch(
</script>
<template>
<Dialog v-model:open="open">
<Dialog :open="open" @update:open="handleOpenChange">
<DialogContent
class="sm:max-w-[520px]"
:show-close-button="!isCloseBlocked"
@@ -118,7 +135,7 @@ watch(
</div>
</div>
<DialogFooter>
<Button v-if="!isCloseBlocked" variant="outline" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
<Button v-if="!isCloseBlocked" variant="outline" @click="handleCancel">{{ t("dangerDialog.cancel") }}</Button>
<template v-if="updateInfo?.update_available">
<Button variant="outline" @click="emit('open-latest-release')">{{ t("updates.openRelease") }}</Button>
<template v-if="canDownloadAndInstallUpdate(updateInfo, isDesktop)">