@@ -39,9 +39,7 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
3939 if ( ! this . socket ) {
4040 helpers . connectEventually ( ( ) => net . connect ( IOSLiveSyncService . BACKEND_PORT ) , ( socket : net . Socket ) => {
4141 this . socket = socket ;
42- if ( this . $options . watch ) {
43- this . attachProcessExitHandlers ( ) ;
44- }
42+ this . attachEventHandlersIfNecessary ( ) ;
4543 this . sendPageReloadMessage ( ) ;
4644 } ) ;
4745 } else {
@@ -52,12 +50,27 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
5250 if ( ! this . socket ) {
5351 this . $iOSSocketRequestExecutor . executeAttachRequest ( this . device , timeout ) . wait ( ) ;
5452 this . socket = this . device . connectToPort ( IOSLiveSyncService . BACKEND_PORT ) ;
53+ this . attachEventHandlersIfNecessary ( ) ;
5554 }
5655 this . sendPageReloadMessage ( ) ;
5756 }
5857 } ) . future < void > ( ) ( ) ;
5958 }
6059
60+ private attachEventHandlersIfNecessary ( ) : void {
61+ if ( this . $options . watch ) {
62+ this . attachProcessExitHandlers ( ) ;
63+ this . attachSocketCloseEvent ( ) ;
64+ }
65+ }
66+
67+ private attachSocketCloseEvent ( ) : void {
68+ this . socket . on ( "close" , ( hadError : boolean ) => {
69+ this . $logger . trace ( `Socket closed, hadError is ${ hadError } .` ) ;
70+ this . socket = null ;
71+ } ) ;
72+ }
73+
6174 private sendPageReloadMessage ( ) : void {
6275 try {
6376 this . sendPageReloadMessageCore ( ) ;
@@ -101,8 +114,10 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
101114 }
102115
103116 private destroySocket ( ) : void {
104- this . socket . destroy ( ) ;
105- this . socket = null ;
117+ if ( this . socket ) {
118+ this . socket . destroy ( ) ;
119+ this . socket = null ;
120+ }
106121 }
107122}
108123$injector . register ( "iosLiveSyncServiceLocator" , { factory : IOSLiveSyncService } ) ;
0 commit comments