Skip to content

Commit

Permalink
Call onbeforequit first
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskerr committed Nov 3, 2023
1 parent 91d76c9 commit bbce8fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/zui/src/domain/updates/mac-win-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
})
}
Expand Down
1 change: 1 addition & 0 deletions apps/zui/src/domain/updates/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
13 changes: 11 additions & 2 deletions apps/zui/src/initializers/window-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
})
}

0 comments on commit bbce8fd

Please sign in to comment.