From bbce8fdc7993b6bdb0d2fc2b660c037fe3b935f3 Mon Sep 17 00:00:00 2001 From: James Kerr Date: Fri, 3 Nov 2023 10:01:37 -0700 Subject: [PATCH] Call onbeforequit first --- apps/zui/src/domain/updates/mac-win-updater.ts | 5 +++++ apps/zui/src/domain/updates/operations.ts | 1 + apps/zui/src/initializers/window-events.ts | 13 +++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/zui/src/domain/updates/mac-win-updater.ts b/apps/zui/src/domain/updates/mac-win-updater.ts index 079785f698..56603bc2a0 100644 --- a/apps/zui/src/domain/updates/mac-win-updater.ts +++ b/apps/zui/src/domain/updates/mac-win-updater.ts @@ -2,6 +2,7 @@ import {autoUpdater} from "electron-updater" import {Updater} from "./types" import semver from "semver" import {app} from "electron" +import {getMainObject} from "src/core/main" autoUpdater.autoDownload = false autoUpdater.autoInstallOnAppQuit = false @@ -32,6 +33,10 @@ export class MacWinUpdater implements Updater { autoUpdater.on("error", reject) autoUpdater.downloadUpdate() }).then(() => { + // `autoUpdater.quitAndInstall()` will close all application windows first and only emit `before-quit` event on `app` after that. + // We have some logic when closing windows that checks to see if we are quitting or not. + // So we call onBeforeQuit manually here to tell the main object we are quitting + getMainObject().onBeforeQuit() autoUpdater.quitAndInstall() }) } diff --git a/apps/zui/src/domain/updates/operations.ts b/apps/zui/src/domain/updates/operations.ts index 706981eb27..140105481d 100644 --- a/apps/zui/src/domain/updates/operations.ts +++ b/apps/zui/src/domain/updates/operations.ts @@ -38,6 +38,7 @@ export const install = createOperation( dispatch(Updates.setIsDownloading(true)) dispatch(Updates.setDownloadProgress(0)) await updater.install(onProgress) + info("Finished calling install") } catch (e) { info("Error Installing") dispatch(Updates.setError(errorToString(e))) diff --git a/apps/zui/src/initializers/window-events.ts b/apps/zui/src/initializers/window-events.ts index a0e1ff617f..3133131d36 100644 --- a/apps/zui/src/initializers/window-events.ts +++ b/apps/zui/src/initializers/window-events.ts @@ -3,6 +3,7 @@ import log from "electron-log" import env from "src/app/core/env" import {MainObject} from "../core/main/main-object" import {moveToCurrentDisplayOp} from "../electron/ops/move-to-current-display-op" +import {debug} from "src/core/log" export function initialize(main: MainObject) { app.on("second-instance", (e, argv) => { @@ -34,6 +35,7 @@ export function initialize(main: MainObject) { }) main.windows.on("window-will-close", (e) => { + debug("window-will-close", "isQuitting:", main.isQuitting) if (!main.isQuitting && main.windows.visible.length === 1) { e.preventDefault() if (env.isMac) { @@ -45,13 +47,20 @@ export function initialize(main: MainObject) { }) // Looks like this gets called twice on linux and windows - app.on("before-quit", () => main.onBeforeQuit()) + app.on("before-quit", () => { + debug("before-quit") + main.onBeforeQuit() + }) // https://www.electronjs.org/docs/latest/api/auto-updater#event-before-quit-for-update // When autoUpdater.quitAndInstall() is called, the "before-quit" event doesn't fire - autoUpdater.on("before-quit-for-update", () => main.onBeforeQuit()) + autoUpdater.on("before-quit-for-update", () => { + debug("before-quit-for-update") + main.onBeforeQuit() + }) app.on("will-quit", () => { + debug("will-quit") main.stop() }) }