From 38e38a72701bdd1e939e4c0f16993cfc1c310eb2 Mon Sep 17 00:00:00 2001 From: "Hung Q. Le" Date: Fri, 9 Mar 2018 16:19:48 +0700 Subject: [PATCH] Simplify auto check for updates (#262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactored check for updates mechanism * Removed Check For Update option in settings * Added migration scheme for appConfig Removed ‘checkUpdate’ and ‘lastCheck’ from general settings --- app.js | 12 +++++++++ app/components/settings/General.jsx | 33 +++++++----------------- main/auto-check-updates.js | 23 ----------------- main/updater.js | 39 +++++++++++++++-------------- 4 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 main/auto-check-updates.js diff --git a/app.js b/app.js index 069c918b..f6ca2716 100644 --- a/app.js +++ b/app.js @@ -300,6 +300,18 @@ function migrateData() { }) }); }, + + 3: configs => { + // Return current configs if checkUpdate and lastCheck do not exist + const { checkUpdate, lastCheck} = configs.general; + if ( checkUpdate === undefined || lastCheck === undefined ) { + return configs; + } + // Remove checkUpdate and lastCheck + return Object.assign({}, configs, { + general: omit(configs.general, ['checkUpdate', 'lastCheck']) + }); + }, }; // Get the current Config const configs = appConfig.getAll(); diff --git a/app/components/settings/General.jsx b/app/components/settings/General.jsx index 20b7a682..0336ecc1 100644 --- a/app/components/settings/General.jsx +++ b/app/components/settings/General.jsx @@ -61,14 +61,18 @@ class General extends Component {
- +
@@ -87,25 +91,6 @@ class General extends Component {
-
-
-
- - -
-
-
); } diff --git a/main/auto-check-updates.js b/main/auto-check-updates.js deleted file mode 100644 index c4745ab5..00000000 --- a/main/auto-check-updates.js +++ /dev/null @@ -1,23 +0,0 @@ -// Libs -const { BrowserWindow, ipcMain } = require('electron'); -const { autoUpdater } = require('electron-updater'); -const { checkForUpdate } = require('./updater'); -const appConfig = require('electron-settings'); - -const checkUpdate = appConfig.get('general.checkUpdate'); -const lastCheck = appConfig.get('general.lastCheck'); -const now = Date.now(); - -const daysSinceLastCheck = Math.round( - Math.abs(now - lastCheck) / (1000 * 60 * 60 * 24) -); - -if (checkUpdate === 'daily' && daysSinceLastCheck >= 1) { - checkForUpdate(); - appConfig.set('general.lastCheck', Date.now()); // Reset last check timeStamp -} - -if (checkUpdate === 'weekly' && daysSinceLastCheck >= 7) { - checkForUpdate(); - appConfig.set('general.lastCheck', Date.now()); // Reset last check timeStamp -} diff --git a/main/updater.js b/main/updater.js index 7c232119..bf0c14c0 100644 --- a/main/updater.js +++ b/main/updater.js @@ -4,23 +4,22 @@ const { autoUpdater } = require('electron-updater'); const appConfig = require('electron-settings'); const isDev = require('electron-is-dev'); +// Set mainWindow +const mainWindowID = appConfig.get('mainWindowID'); +const mainWindow = BrowserWindow.fromId(mainWindowID); + // Disable Auto Downloading update; autoUpdater.autoDownload = false; // Check for update silently let silentMode = true; -// Set mainWindow -const mainWindowID = appConfig.get('mainWindowID'); -const mainWindow = BrowserWindow.fromId(mainWindowID); - - // Check for Updates -ipcMain.on('check-for-updates', (event) => { - if(!isDev) { - // Turn off silent mode - silentMode = false; - checkForUpdate(); - } +// HANDLING IPC +// Check for updates manually +ipcMain.on('check-for-updates', event => { + // Turn off silent mode first + silentMode = false; + checkForUpdate(); }); // Start Download @@ -28,8 +27,7 @@ ipcMain.on('update-download-started', () => { autoUpdater.downloadUpdate(); }); -// All AutoUpdater Events -// ==================================== +// CHECKING FOR UPDATE EVENTS // Checking for Update autoUpdater.on('checking-for-update', () => { // Only notice user when they checked manually @@ -58,10 +56,11 @@ autoUpdater.on('error', error => { errMessage = 'Unknown Error'; } else { errMessage = error.message; - } + }; mainWindow.send('update-error', errMessage); }); +// DOWNLOADING UPDATE EVENTS // Download Progress autoUpdater.on('download-progress', progressObj => { mainWindow.send('update-download-progress', progressObj.percent); @@ -72,11 +71,13 @@ autoUpdater.on('update-downloaded', info => { mainWindow.send('update-downloaded', info); }); -// Helper +// Main Function function checkForUpdate() { - autoUpdater.checkForUpdates(); + // Only check for update in Production + if (!isDev) { + autoUpdater.checkForUpdates(); + } } -module.exports = { - checkForUpdate, -}; +// Check for update on Startup +checkForUpdate();