Skip to content

A full-featured download manager.

fix: app reset engine not start

Dr_rOot3e84230b3351
diff --git a/src/main/Application.js b/src/main/Application.js
index de68f302..829fa5d4 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -41,7 +41,7 @@ export default class Application extends EventEmitter {
}
init () {
- this.configManager = new ConfigManager()
+ this.configManager = this.initConfigManager()
this.locale = this.configManager.getLocale()
this.localeManager = setupLocaleManager(this.locale)
@@ -84,6 +84,22 @@ export default class Application extends EventEmitter {
this.emit('application:initialized')
}
+ initConfigManager () {
+ this.configListeners = {}
+ return new ConfigManager()
+ }
+
+ offConfigListeners () {
+ try {
+ Object.keys(this.configListeners).forEach((key) => {
+ this.configListeners[key]()
+ })
+ } catch (e) {
+ logger.warn('[Motrix] offConfigListeners===>', e)
+ }
+ this.configListeners = {}
+ }
+
setupApplicationMenu () {
this.menuManager = new MenuManager()
this.menuManager.setup(this.locale)
@@ -172,7 +188,9 @@ export default class Application extends EventEmitter {
}
watchTraySpeedometerEnabledChange () {
- this.configManager.userConfig.onDidChange('tray-speedometer', async (newValue, oldValue) => {
+ const { userConfig } = this.configManager
+ const key = 'tray-speedometer'
+ this.configListeners[key] = userConfig.onDidChange('tray-speedometer', async (newValue, oldValue) => {
logger.info('[Motrix] detected tray speedometer value change event:', newValue, oldValue)
this.trayManager.handleSpeedometerEnableChange(newValue)
})
@@ -230,10 +248,11 @@ export default class Application extends EventEmitter {
}
watchUPnPPortsChange () {
+ const { systemConfig } = this.configManager
const watchKeys = ['listen-port', 'dht-listen-port']
watchKeys.forEach((key) => {
- this.configManager.systemConfig.onDidChange(key, async (newValue, oldValue) => {
+ this.configListeners[key] = systemConfig.onDidChange(key, async (newValue, oldValue) => {
logger.info('[Motrix] detected port change event:', key, newValue, oldValue)
const enable = this.configManager.getUserConfig('enable-upnp')
if (!enable) {
@@ -254,7 +273,9 @@ export default class Application extends EventEmitter {
}
watchUPnPEnabledChange () {
- this.configManager.userConfig.onDidChange('enable-upnp', async (newValue, oldValue) => {
+ const { userConfig } = this.configManager
+ const key = 'enable-upnp'
+ this.configListeners[key] = userConfig.onDidChange(key, async (newValue, oldValue) => {
logger.info('[Motrix] detected enable-upnp value change event:', newValue, oldValue)
if (newValue) {
this.startUPnPMapping()
@@ -546,12 +567,27 @@ export default class Application extends EventEmitter {
})
}
- relaunch () {
- this.stop()
+ async relaunch () {
+ await this.stop()
app.relaunch()
app.exit()
}
+ async resetSession () {
+ await this.stopEngine()
+
+ app.clearRecentDocuments()
+
+ const sessionPath = this.configManager.getUserConfig('session-path') || getSessionPath()
+ setTimeout(() => {
+ unlink(sessionPath, function (err) {
+ logger.info('[Motrix] Removed the download seesion file:', err)
+ })
+
+ this.engine.start()
+ }, 3000)
+ }
+
savePreference (config = {}) {
logger.info('[Motrix] save preference:', config)
const { system, user } = config
@@ -602,22 +638,10 @@ export default class Application extends EventEmitter {
this.hide(page)
})
- this.on('application:reset-session', () => {
- this.engine.stop()
-
- app.clearRecentDocuments()
-
- const sessionPath = this.configManager.getUserConfig('session-path') || getSessionPath()
- setTimeout(() => {
- unlink(sessionPath, function (err) {
- logger.info('[Motrix] Removed the download seesion file:', err)
- })
-
- this.engine.start()
- }, 3000)
- })
+ this.on('application:reset-session', () => this.resetSession())
this.on('application:reset', () => {
+ this.offConfigListeners()
this.configManager.reset()
this.relaunch()
})