Allow to properly cancel download upgrade and prevent multiple downloads.

This commit is contained in:
Franco Montenegro 2022-03-04 12:01:08 -03:00
parent 6e97fbf5f8
commit 0ea5d01062
3 changed files with 29 additions and 31 deletions

View file

@ -51,11 +51,9 @@ let showingAutoUpdateCloseAlert = false;
// https://www.electronjs.org/docs/latest/api/auto-updater#autoupdatercheckforupdates // https://www.electronjs.org/docs/latest/api/auto-updater#autoupdatercheckforupdates
let keepCheckingForUpdates = true; let keepCheckingForUpdates = true;
// Auto updater doesn't support Linux installations (only trough AppImages) let downloadUpgradeInitiated = false;
// this is why, for that case, we download a full executable (.deb package)
// as a fallback support. This variable will be used to prevent let downloadUpgradeItem;
// multiple downloads when auto updater isn't supported.
let downloadUpgradeInProgress = false;
// Keep a global reference, if you don't, they will be closed automatically when the JavaScript // Keep a global reference, if you don't, they will be closed automatically when the JavaScript
// object is garbage collected. // object is garbage collected.
@ -328,23 +326,43 @@ ipcMain.on('get-disk-space', async (event) => {
} }
}); });
ipcMain.on('cancel-download-upgrade', () => {
if (downloadUpgradeItem) {
// Cancel the download and execute the onCancel
// callback set in the options.
downloadUpgradeItem.cancel();
}
});
ipcMain.on('download-upgrade', async (event, params) => { ipcMain.on('download-upgrade', async (event, params) => {
if (downloadUpgradeInProgress) { // Prevent downloading multiple times.
if (downloadUpgradeInitiated || downloadUpgradeItem) {
return; return;
} }
const { url, options } = params; const { url, options } = params;
const dir = fs.mkdtempSync(app.getPath('temp') + path.sep); const dir = fs.mkdtempSync(app.getPath('temp') + path.sep);
downloadUpgradeInitiated = true;
// Grab the download item's handler to allow
// cancelling the operation if required.
options.onStarted = function(downloadItem) {
downloadUpgradeItem = downloadItem;
};
options.onCancel = function() {
downloadUpgradeItem = undefined;
downloadUpgradeInitiated = false;
};
options.onProgress = function(p) { options.onProgress = function(p) {
rendererWindow.webContents.send('download-progress-update', p); rendererWindow.webContents.send('download-progress-update', p);
}; };
options.directory = dir; options.directory = dir;
options.onCompleted = function(c) { options.onCompleted = function(c) {
downloadUpgradeInProgress = false; downloadUpgradeInitiated = false;
downloadUpgradeItem = undefined;
rendererWindow.webContents.send('download-update-complete', c); rendererWindow.webContents.send('download-update-complete', c);
}; };
const win = BrowserWindow.getFocusedWindow(); const win = BrowserWindow.getFocusedWindow();
downloadUpgradeInProgress = true;
await download(win, url, options).catch(e => console.log('e', e)); await download(win, url, options).catch(e => console.log('e', e));
}); });

View file

@ -28,7 +28,6 @@ import {
import { import {
selectIsUpgradeSkipped, selectIsUpgradeSkipped,
selectUpdateUrl, selectUpdateUrl,
selectUpgradeDownloadItem,
selectUpgradeDownloadPath, selectUpgradeDownloadPath,
selectAutoUpdateDeclined, selectAutoUpdateDeclined,
selectRemoteVersion, selectRemoteVersion,
@ -163,25 +162,8 @@ export function doAutoUpdateDeclined() {
} }
export function doCancelUpgrade() { export function doCancelUpgrade() {
return (dispatch, getState) => { ipcRenderer.send('cancel-download-upgrade');
const state = getState(); return { type: ACTIONS.UPGRADE_CANCELLED };
const upgradeDownloadItem = selectUpgradeDownloadItem(state);
if (upgradeDownloadItem) {
/*
* Right now the remote reference to the download item gets garbage collected as soon as the
* the download is over (maybe even earlier), so trying to cancel a finished download may
* throw an error.
*/
try {
upgradeDownloadItem.cancel();
} catch (err) {
console.error(err); // eslint-disable-line no-console
}
}
dispatch({ type: ACTIONS.UPGRADE_CANCELLED });
};
} }
export function doCheckUpgradeAvailable() { export function doCheckUpgradeAvailable() {

View file

@ -50,8 +50,6 @@ export const selectIsUpgradeSkipped = createSelector(selectState, (state) => sta
export const selectUpgradeDownloadPath = createSelector(selectState, (state) => state.downloadPath); export const selectUpgradeDownloadPath = createSelector(selectState, (state) => state.downloadPath);
export const selectUpgradeDownloadItem = createSelector(selectState, (state) => state.downloadItem);
export const selectAutoUpdateDownloaded = createSelector(selectState, (state) => state.autoUpdateDownloaded); export const selectAutoUpdateDownloaded = createSelector(selectState, (state) => state.autoUpdateDownloaded);
export const selectAutoUpdateDeclined = createSelector(selectState, (state) => state.autoUpdateDeclined); export const selectAutoUpdateDeclined = createSelector(selectState, (state) => state.autoUpdateDeclined);