Tweak auto-updater flow

This commit is contained in:
Yuya Ochiai 2017-09-07 23:23:46 +09:00
parent 9d31066136
commit fb47c287d9
3 changed files with 59 additions and 29 deletions

View file

@ -652,14 +652,22 @@ app.on('ready', () => {
session.defaultSession.setPermissionRequestHandler(permissionRequestHandler(mainWindow, permissionManager));
autoUpdater.initialize(appState, mainWindow);
ipcMain.on('check-for-updates', () => {
ipcMain.on('check-for-updates', (isManual) => {
if (global.isDev) {
console.log('Development mode: Skip checking for updates');
} else {
autoUpdater.checkForUpdates();
autoUpdater.checkForUpdates(isManual);
}
});
mainWindow.once('show', () => {
if (autoUpdater.shouldCheckForUpdatesOnStart(appState.updateCheckedDate)) {
ipcMain.emit('check-for-updates');
} else {
setTimeout(() => {
ipcMain.emit('check-for-updates');
}, autoUpdater.INTERVAL_48_HOURS_IN_MS);
}
});
ipcMain.emit('check-for-updates');
// Open the DevTools.
// mainWindow.openDevTools();

View file

@ -3,7 +3,7 @@ const path = require('path');
const {autoUpdater} = require('electron-updater');
const semver = require('semver');
const interval48hours = 172800000; // 48 * 60 * 60 * 1000 [ms]
const INTERVAL_48_HOURS_IN_MS = 172800000; // 48 * 60 * 60 * 1000 [ms]
let updaterWindow = null;
@ -46,26 +46,34 @@ function createUpdaterWindow(options) {
return win;
}
function isUpdateApplicable(appState, updateInfo) {
const checkedTime = appState.updateCheckedDate.getTime();
function isUpdateApplicable(now, skippedVersion, updateInfo) {
const releaseTime = new Date(updateInfo.releaseDate).getTime();
if (checkedTime - releaseTime < interval48hours) {
// 48 hours after a new version is added to releases.mattermost.com, user receives a “New update is available” dialog
if (now.getTime() - releaseTime < INTERVAL_48_HOURS_IN_MS) {
return false;
}
if (appState.skippedVersion === null) {
return true;
// If a version was skipped, compare version.
if (skippedVersion) {
return semver.gt(updateInfo.version, skippedVersion);
}
return semver.gt(updateInfo.version, appState.skippedVersion);
return true;
}
function downloadAndInstall() {
autoUpdater.downloadUpdate().then(() => {
autoUpdater.quitAndInstall();
});
}
function initialize(appState, mainWindow) {
const assetsDir = path.resolve(app.getAppPath(), 'assets');
autoUpdater.on('error', (err) => {
console.error('Error in autoUpdater:', err.message);
}).on('checking-for-update', () => {
appState.updateCheckedDate = new Date();
}).on('update-available', (info) => {
if (isUpdateApplicable(appState, info)) {
if (isUpdateApplicable(new Date(), appState.skippedVersion, info)) {
updaterWindow = createUpdaterWindow({linuxAppIcon: path.join(assetsDir, 'appicon.png'), nextVersion: '0.0.0'});
updaterWindow.on('close', () => {
updaterWindow = null;
@ -74,33 +82,52 @@ function initialize(appState, mainWindow) {
appState.skippedVersion = info.version;
updaterWindow.close();
}).on('click-remind', () => {
setTimeout(autoUpdater.checkForUpdates, interval48hours);
appState.updateCheckedDate = new Date();
setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS);
updaterWindow.close();
}).on('click-install', () => {
autoUpdater.quitAndInstall();
downloadAndInstall();
updaterWindow.close();
}).on('click-release-notes', () => {
shell.openExternal(`https://github.com/mattermost/desktop/releases/v${info.version}`);
});
updaterWindow.focus();
} else if (autoUpdater.isManual) {
autoUpdater.emit('update-not-available');
}
}).on('update-not-available', () => {
dialog.showMessageBox(mainWindow, {
type: 'info',
buttons: ['Close'],
title: 'Your Desktop App is up to date',
message: 'You have the latest version of the Mattermost Desktop App.'
}, () => {}); // eslint-disable-line no-empty-function
setTimeout(autoUpdater.checkForUpdates, interval48hours);
if (autoUpdater.isManual) {
dialog.showMessageBox(mainWindow, {
type: 'info',
buttons: ['Close'],
title: 'Your Desktop App is up to date',
message: 'You have the latest version of the Mattermost Desktop App.'
}, () => {}); // eslint-disable-line no-empty-function
}
setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS);
});
}
function checkForUpdates() {
function shouldCheckForUpdatesOnStart(updateCheckedDate) {
if (updateCheckedDate) {
if (Date.now() - updateCheckedDate < INTERVAL_48_HOURS_IN_MS) {
return false;
}
}
return true;
}
function checkForUpdates(isManual = false) {
autoUpdater.isManual = isManual;
autoUpdater.autoDownload = false;
if (!updaterWindow) {
autoUpdater.checkForUpdates();
}
}
module.exports = {
INTERVAL_48_HOURS_IN_MS,
checkForUpdates,
shouldCheckForUpdatesOnStart,
initialize
};

View file

@ -230,15 +230,10 @@ function createTemplate(mainWindow, config, isDev) {
submenu.push({
label: `Version ${app.getVersion()}`,
enabled: false,
}, {
type: 'separator',
}, {
label: `Version ${app.getVersion()}`,
enabled: false,
}, {
label: 'Check for Updates...',
click() {
ipcMain.emit('check-for-updates');
ipcMain.emit('check-for-updates', true);
},
});
template.push({label: '&Help', submenu});