From e704fcce8f710bdeccd85282b9642f041c3b69d9 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Thu, 31 Mar 2022 23:19:53 +0200 Subject: [PATCH] fix(App): Fix issues with updater on macOS --- src/electron/ipc-api/autoUpdate.js | 24 +++++++++++++----------- src/index.js | 17 ++++++++++++++--- src/models/ServiceBrowserView.ts | 4 +++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/electron/ipc-api/autoUpdate.js b/src/electron/ipc-api/autoUpdate.js index 2f86669f1..2af6a730d 100644 --- a/src/electron/ipc-api/autoUpdate.js +++ b/src/electron/ipc-api/autoUpdate.js @@ -1,23 +1,25 @@ -import { app, ipcMain } from 'electron'; +import { BrowserWindow, ipcMain } from 'electron'; import { autoUpdater } from 'electron-updater'; +import { appEvents } from '../..'; const debug = require('debug')('Franz:ipcApi:autoUpdate'); -export default (params) => { +export default ({ mainWindow, settings }) => { if (process.platform === 'darwin' || process.platform === 'win32' || process.env.APPIMAGE) { ipcMain.on('autoUpdate', (event, args) => { try { autoUpdater.autoInstallOnAppQuit = false; - autoUpdater.allowPrerelease = Boolean(params.settings.app.get('beta')); + autoUpdater.allowPrerelease = Boolean(settings.app.get('beta')); if (args.action === 'check') { autoUpdater.checkForUpdates(); } else if (args.action === 'install') { debug('install update'); + appEvents.emit('install-update'); + + const openedWindows = BrowserWindow.getAllWindows(); + openedWindows.forEach(window => window.close()); + autoUpdater.quitAndInstall(); - // we need to send a quit event - setTimeout(() => { - app.quit(); - }, 20); } } catch (e) { console.log(e); @@ -26,12 +28,12 @@ export default (params) => { autoUpdater.on('update-not-available', () => { debug('update-not-available'); - params.mainWindow.webContents.send('autoUpdate', { available: false }); + mainWindow.webContents.send('autoUpdate', { available: false }); }); autoUpdater.on('update-available', (event) => { debug('update-available'); - params.mainWindow.webContents.send('autoUpdate', { + mainWindow.webContents.send('autoUpdate', { version: event.version, available: true, }); @@ -47,12 +49,12 @@ export default (params) => { autoUpdater.on('update-downloaded', () => { debug('update-downloaded'); - params.mainWindow.webContents.send('autoUpdate', { downloaded: true }); + mainWindow.webContents.send('autoUpdate', { downloaded: true }); }); autoUpdater.on('error', () => { debug('update-error'); - params.mainWindow.webContents.send('autoUpdate', { error: true }); + mainWindow.webContents.send('autoUpdate', { error: true }); }); } }; diff --git a/src/index.js b/src/index.js index b0214f641..0c079aaf9 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ import path from 'path'; import windowStateKeeper from 'electron-window-state'; import { enforceMacOSAppLocation } from 'electron-util'; import * as remoteMain from '@electron/remote/main'; +import { EventEmitter } from 'events'; remoteMain.initialize(); @@ -60,7 +61,6 @@ import { asarPath } from './helpers/asar-helpers'; import { isValidExternalURL } from './helpers/url-helpers'; import userAgent from './helpers/userAgent-helpers'; import { openOverlay } from './electron/ipc-api/overlayWindow'; -import macosVersion from 'macos-version'; /* eslint-enable import/first */ const debug = require('debug')('Franz:App'); @@ -73,6 +73,9 @@ app.userAgentFallback = userAgent(); // be closed automatically when the JavaScript object is garbage collected. let mainWindow; let willQuitApp = false; +let overrideAppQuitForUpdate = false; + +export const appEvents = new EventEmitter(); // Register methods to be called once the window has been loaded. let onDidLoadFns = []; @@ -264,7 +267,8 @@ const createWindow = () => { debug('Window: hide'); mainWindow.hide(); } - } else { + } else if (!overrideAppQuitForUpdate) { + debug('Quitting the app'); app.quit(); } }); @@ -436,7 +440,9 @@ app.on('window-all-closed', () => { if (settings.get('runInBackground') === undefined || settings.get('runInBackground')) { debug('Window: all windows closed, quit app'); - app.quit(); + if (!overrideAppQuitForUpdate) { + app.quit(); + } } else { debug('Window: don\'t quit app'); } @@ -446,6 +452,11 @@ app.on('before-quit', () => { willQuitApp = true; }); +appEvents.on('install-update', () => { + willQuitApp = true; + overrideAppQuitForUpdate = true; +}); + app.on('activate', () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. diff --git a/src/models/ServiceBrowserView.ts b/src/models/ServiceBrowserView.ts index 66fb05fa3..69d68d09e 100644 --- a/src/models/ServiceBrowserView.ts +++ b/src/models/ServiceBrowserView.ts @@ -461,7 +461,9 @@ export class ServiceBrowserView { ...state, }; - this.window.webContents.send(UPDATE_SERVICE_STATE, { serviceId: this.config.id, state: this.webContentsState }); + if (!this.window.isDestroyed()) { + this.window.webContents.send(UPDATE_SERVICE_STATE, { serviceId: this.config.id, state: this.webContentsState }); + } } hacks() {