Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: create less backups when an update is available
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Apr 15, 2021
1 parent e5167a0 commit e796e5d
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions app/javascripts/main/updateManager.ts
Expand Up @@ -102,14 +102,18 @@ export function setupUpdates(

const updateState = appState.updates;

function checkUpdateSafety() {
const isSafeToUpdate =
appState.store.get(StoreKeys.BackupsDisabled) ||
(updateState.enableAutoUpdate &&
typeof appState.lastBackupDate === 'number' &&
isLessThanOneHourFromNow(appState.lastBackupDate));
autoUpdater.autoInstallOnAppQuit = isSafeToUpdate;
autoUpdater.autoDownload = isSafeToUpdate;
function checkUpdateSafety(): boolean {
let canUpdate: boolean;
if (appState.store.get(StoreKeys.BackupsDisabled)) {
canUpdate = true;
} else {
canUpdate =
updateState.enableAutoUpdate &&
isLessThanOneHourFromNow(appState.lastBackupDate);
}
autoUpdater.autoInstallOnAppQuit = canUpdate;
autoUpdater.autoDownload = canUpdate;
return canUpdate;
}
autorun(checkUpdateSafety);

Expand All @@ -124,7 +128,12 @@ export function setupUpdates(
autoUpdater.on('error', logError);
autoUpdater.on('update-available', (info: { version?: string }) => {
updateState.checkedForUpdate(info.version || null);
backupsManager.performBackup();
if (updateState.enableAutoUpdate) {
const canUpdate = checkUpdateSafety();
if (!canUpdate) {
backupsManager.performBackup();
}
}
});
autoUpdater.on('update-not-available', (info: { version?: string }) => {
updateState.checkedForUpdate(info.version || null);
Expand Down Expand Up @@ -166,10 +175,10 @@ function quitAndInstall(window: BrowserWindow) {
}, 0);
}

function isLessThanOneHourFromNow(date: number) {
function isLessThanOneHourFromNow(date: number | null) {
const now = Date.now();
const onHourMs = 1 * 60 * 60 * 1000;
return now - date < onHourMs;
return now - (date ?? 0) < onHourMs;
}

export async function showUpdateInstallationDialog(
Expand Down

0 comments on commit e796e5d

Please sign in to comment.