Skip to content

Commit

Permalink
fix(App): Fix issues with updater on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Mar 31, 2022
1 parent 6a8fdae commit e704fcc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/electron/ipc-api/autoUpdate.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
});
Expand All @@ -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 });
});
}
};
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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');
Expand All @@ -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 = [];
Expand Down Expand Up @@ -264,7 +267,8 @@ const createWindow = () => {
debug('Window: hide');
mainWindow.hide();
}
} else {
} else if (!overrideAppQuitForUpdate) {
debug('Quitting the app');
app.quit();
}
});
Expand Down Expand Up @@ -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');
}
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/models/ServiceBrowserView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e704fcc

Please sign in to comment.