@@ -41,7 +41,7 @@ export default class Application extends EventEmitter {
4141 }
4242
4343 init ( ) {
44- this . configManager = new ConfigManager ( )
44+ this . configManager = this . initConfigManager ( )
4545
4646 this . locale = this . configManager . getLocale ( )
4747 this . localeManager = setupLocaleManager ( this . locale )
@@ -84,6 +84,22 @@ export default class Application extends EventEmitter {
8484 this . emit ( 'application:initialized' )
8585 }
8686
87+ initConfigManager ( ) {
88+ this . configListeners = { }
89+ return new ConfigManager ( )
90+ }
91+
92+ offConfigListeners ( ) {
93+ try {
94+ Object . keys ( this . configListeners ) . forEach ( ( key ) => {
95+ this . configListeners [ key ] ( )
96+ } )
97+ } catch ( e ) {
98+ logger . warn ( '[Motrix] offConfigListeners===>' , e )
99+ }
100+ this . configListeners = { }
101+ }
102+
87103 setupApplicationMenu ( ) {
88104 this . menuManager = new MenuManager ( )
89105 this . menuManager . setup ( this . locale )
@@ -172,7 +188,9 @@ export default class Application extends EventEmitter {
172188 }
173189
174190 watchTraySpeedometerEnabledChange ( ) {
175- this . configManager . userConfig . onDidChange ( 'tray-speedometer' , async ( newValue , oldValue ) => {
191+ const { userConfig } = this . configManager
192+ const key = 'tray-speedometer'
193+ this . configListeners [ key ] = userConfig . onDidChange ( 'tray-speedometer' , async ( newValue , oldValue ) => {
176194 logger . info ( '[Motrix] detected tray speedometer value change event:' , newValue , oldValue )
177195 this . trayManager . handleSpeedometerEnableChange ( newValue )
178196 } )
@@ -230,10 +248,11 @@ export default class Application extends EventEmitter {
230248 }
231249
232250 watchUPnPPortsChange ( ) {
251+ const { systemConfig } = this . configManager
233252 const watchKeys = [ 'listen-port' , 'dht-listen-port' ]
234253
235254 watchKeys . forEach ( ( key ) => {
236- this . configManager . systemConfig . onDidChange ( key , async ( newValue , oldValue ) => {
255+ this . configListeners [ key ] = systemConfig . onDidChange ( key , async ( newValue , oldValue ) => {
237256 logger . info ( '[Motrix] detected port change event:' , key , newValue , oldValue )
238257 const enable = this . configManager . getUserConfig ( 'enable-upnp' )
239258 if ( ! enable ) {
@@ -254,7 +273,9 @@ export default class Application extends EventEmitter {
254273 }
255274
256275 watchUPnPEnabledChange ( ) {
257- this . configManager . userConfig . onDidChange ( 'enable-upnp' , async ( newValue , oldValue ) => {
276+ const { userConfig } = this . configManager
277+ const key = 'enable-upnp'
278+ this . configListeners [ key ] = userConfig . onDidChange ( key , async ( newValue , oldValue ) => {
258279 logger . info ( '[Motrix] detected enable-upnp value change event:' , newValue , oldValue )
259280 if ( newValue ) {
260281 this . startUPnPMapping ( )
@@ -546,12 +567,27 @@ export default class Application extends EventEmitter {
546567 } )
547568 }
548569
549- relaunch ( ) {
550- this . stop ( )
570+ async relaunch ( ) {
571+ await this . stop ( )
551572 app . relaunch ( )
552573 app . exit ( )
553574 }
554575
576+ async resetSession ( ) {
577+ await this . stopEngine ( )
578+
579+ app . clearRecentDocuments ( )
580+
581+ const sessionPath = this . configManager . getUserConfig ( 'session-path' ) || getSessionPath ( )
582+ setTimeout ( ( ) => {
583+ unlink ( sessionPath , function ( err ) {
584+ logger . info ( '[Motrix] Removed the download seesion file:' , err )
585+ } )
586+
587+ this . engine . start ( )
588+ } , 3000 )
589+ }
590+
555591 savePreference ( config = { } ) {
556592 logger . info ( '[Motrix] save preference:' , config )
557593 const { system, user } = config
@@ -602,22 +638,10 @@ export default class Application extends EventEmitter {
602638 this . hide ( page )
603639 } )
604640
605- this . on ( 'application:reset-session' , ( ) => {
606- this . engine . stop ( )
607-
608- app . clearRecentDocuments ( )
609-
610- const sessionPath = this . configManager . getUserConfig ( 'session-path' ) || getSessionPath ( )
611- setTimeout ( ( ) => {
612- unlink ( sessionPath , function ( err ) {
613- logger . info ( '[Motrix] Removed the download seesion file:' , err )
614- } )
615-
616- this . engine . start ( )
617- } , 3000 )
618- } )
641+ this . on ( 'application:reset-session' , ( ) => this . resetSession ( ) )
619642
620643 this . on ( 'application:reset' , ( ) => {
644+ this . offConfigListeners ( )
621645 this . configManager . reset ( )
622646 this . relaunch ( )
623647 } )
0 commit comments