From fb47c287d9fd0511c25b1568a1244b863349f96a Mon Sep 17 00:00:00 2001 From: Yuya Ochiai Date: Thu, 7 Sep 2017 23:23:46 +0900 Subject: [PATCH] Tweak auto-updater flow --- src/main.js | 14 +++++++-- src/main/autoUpdater.js | 67 +++++++++++++++++++++++++++++------------ src/main/menus/app.js | 7 +---- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/main.js b/src/main.js index 4829edad..1c36cae6 100644 --- a/src/main.js +++ b/src/main.js @@ -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(); diff --git a/src/main/autoUpdater.js b/src/main/autoUpdater.js index 450d76f3..af20122b 100644 --- a/src/main/autoUpdater.js +++ b/src/main/autoUpdater.js @@ -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 }; diff --git a/src/main/menus/app.js b/src/main/menus/app.js index 35795df3..296038e3 100644 --- a/src/main/menus/app.js +++ b/src/main/menus/app.js @@ -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});