Skip to content

A full-featured download manager.

refactor: auto check update

Dr_rOotb89c651132f5
diff --git a/src/main/Application.js b/src/main/Application.js
index 1c1966bc..256a3e4e 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -16,6 +16,7 @@ import MenuManager from './ui/MenuManager'
import TouchBarManager from './ui/TouchBarManager'
import TrayManager from './ui/TrayManager'
import ThemeManager from './ui/ThemeManager'
+import { AUTO_CHECK_UPDATE_INTERVAL } from '@shared/constants'
export default class Application extends EventEmitter {
constructor () {
@@ -201,14 +202,22 @@ export default class Application extends EventEmitter {
return
}
this.updateManager = new UpdateManager({
- autoCheck: this.configManager.getUserConfig('auto-check-update')
- ? (new Date().getTime() - this.configManager.getUserConfig('last-check-update-time') > 7 * 24 * 60 * 60 * 1000)
- : false,
+ autoCheck: this.isNeedAutoCheck(),
setCheckTime: this.configManager
})
this.handleUpdaterEvents()
}
+ isNeedAutoCheck () {
+ const enable = this.configManager.getUserConfig('auto-check-update')
+ if (!enable) {
+ return false
+ }
+
+ const lastCheck = this.configManager.getUserConfig('last-check-update-time')
+ return (Date.now() - lastCheck > AUTO_CHECK_UPDATE_INTERVAL)
+ }
+
handleUpdaterEvents () {
this.updateManager.on('checking', (event) => {
this.menuManager.updateMenuItemEnabledState('app.check-for-updates', false)
diff --git a/src/main/core/UpdateManager.js b/src/main/core/UpdateManager.js
index ad265514..a98ad4d4 100644
--- a/src/main/core/UpdateManager.js
+++ b/src/main/core/UpdateManager.js
@@ -43,13 +43,13 @@ export default class UpdateManager extends EventEmitter {
if (this.autoCheckData.checkEnable) {
this.autoCheckData.userCheck = false
- this.options.setCheckTime.setUserConfig('last-check-update-time', new Date().getTime())
+ this.options.setCheckTime.setUserConfig('last-check-update-time', Date.now())
this.updater.checkForUpdates()
}
}
check () {
- this.options.setCheckTime.setUserConfig('last-check-update-time', new Date().getTime())
+ this.options.setCheckTime.setUserConfig('last-check-update-time', Date.now())
this.autoCheckData.userCheck = true
this.updater.checkForUpdates()
}
@@ -112,7 +112,10 @@ export default class UpdateManager extends EventEmitter {
updateError (event, error) {
this.emit('update-error', error)
- const msg = error == null ? this.i18n.t('update-error-message') : (error.stack || error).toString()
+ const msg = (error == null)
+ ? this.i18n.t('update-error-message')
+ : (error.stack || error).toString()
+
this.updater.logger.warn(`[Motrix] update-error: ${msg}`)
dialog.showErrorBox(msg)
}
diff --git a/src/shared/constants.js b/src/shared/constants.js
index c3e4b498..85a7140c 100644
--- a/src/shared/constants.js
+++ b/src/shared/constants.js
@@ -3,3 +3,6 @@ export const DARK_THEME = 'dark'
export const BEST_TRACKERS_URL = 'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt'
export const BEST_TRACKERS_IP_URL = 'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best_ip.txt'
+
+// One Week
+export const AUTO_CHECK_UPDATE_INTERVAL = 7 * 24 * 60 * 60 * 1000