update-electron-app icon indicating copy to clipboard operation
update-electron-app copied to clipboard

Any hooks to subscribe to when autoupdate is about to update?

Open yuvalkarmi opened this issue 1 year ago • 2 comments

Are there any events that auto updater sends that I can hook into? Something like autoupdate.on('about-to-update', function(){ ... }).

I'll explain the use case: My electron app prevent the app from quitting when a window closes on Mac (which is in line with how apps work on Mac). Instead, I have a flag called shouldQuit and only if it's true I call app.quit().

However, this prevent the auto updater from working. When a user clicks on "Update now" when prompted, I need to be able to set the flag shouldQuit to true to allow the app to actually quit.

Couldn't find anything about it in the documentation.

Thanks!

yuvalkarmi avatar Feb 16 '24 02:02 yuvalkarmi

We had the same problem and solved it like this.

export function ensureSafeQuitAndInstall() {
    const electron = require('electron');
    const app = electron.app;
    const BrowserWindow = electron.BrowserWindow;
    app.removeAllListeners('window-all-closed');
    const browserWindows = BrowserWindow.getAllWindows();
    for (const browserWindow of browserWindows) {
        browserWindow.removeAllListeners('close');
    }
}

When user clicks Update now an event is send to the main process.

import { IpcMain, IpcMainEvent } from 'electron';
import { autoUpdater } from 'electron-updater';
import { ensureSafeQuitAndInstall } from '../utils';

ipcMain?.handle('update-and-restart', (_event: IpcMainEvent) => {
    ensureSafeQuitAndInstall();
    autoUpdater.quitAndInstall(true, true);
});

hackal avatar Mar 18 '24 20:03 hackal

Dude, you're on fire today! I ended up moving to electron-builder and the electron-updater instead of the update-electron-app module, as the latter supports these events. Thanks!

By the way, why are you removing the listeners?

yuvalkarmi avatar Mar 18 '24 20:03 yuvalkarmi