@@ -24,6 +24,12 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
2424 private socketError : string | Error ;
2525 private socketConnection : IDuplexSocket ;
2626 private configuration : IAndroidLivesyncToolConfiguration ;
27+ private pendingConnectionData : {
28+ connectionTimer ?: NodeJS . Timer ,
29+ socketTimer ?: NodeJS . Timer ,
30+ rejectHandler ?: Function ,
31+ socket ?: IDuplexSocket
32+ } = null ;
2733
2834 constructor ( private $androidProcessService : Mobile . IAndroidProcessService ,
2935 private $errors : IErrors ,
@@ -295,16 +301,25 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
295301 let lastKnownError : Error | string ,
296302 isConnected = false ;
297303
298- setTimeout ( ( ) => {
304+ const connectionTimer = setTimeout ( ( ) => {
299305 if ( ! isConnected ) {
300306 isConnected = true ;
301- reject ( lastKnownError ) ;
307+ reject ( lastKnownError || { message : "Socket connection timeouted." } ) ;
308+ this . pendingConnectionData = null ;
302309 }
303310 } , timeout ) ;
304311
312+ this . pendingConnectionData = {
313+ connectionTimer,
314+ rejectHandler : reject
315+ } ;
316+
305317 const tryConnect = ( ) => {
318+ const socket = factory ( ) ;
319+
306320 const tryConnectAfterTimeout = ( error : Error ) => {
307321 if ( isConnected ) {
322+ this . pendingConnectionData = null ;
308323 return ;
309324 }
310325
@@ -313,10 +328,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
313328 }
314329
315330 lastKnownError = error ;
316- setTimeout ( tryConnect , 1000 ) ;
331+ socket . removeAllListeners ( ) ;
332+ this . pendingConnectionData . socketTimer = setTimeout ( tryConnect , 1000 ) ;
317333 } ;
318334
319- const socket = factory ( ) ;
335+ this . pendingConnectionData . socket = socket ;
320336
321337 socket . once ( "data" , data => {
322338 socket . removeListener ( "close" , tryConnectAfterTimeout ) ;
@@ -366,6 +382,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
366382 const error = this . getErrorWithMessage ( errorMessage ) ;
367383 if ( this . socketConnection && this . socketConnection . uid === socketId ) {
368384 this . end ( ) ;
385+ this . socketConnection . removeAllListeners ( ) ;
369386 this . socketConnection = null ;
370387 this . socketError = error ;
371388 }
@@ -410,6 +427,18 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
410427 }
411428
412429 private dispose ( ) : void {
430+ if ( this . pendingConnectionData ) {
431+ clearTimeout ( this . pendingConnectionData . connectionTimer ) ;
432+
433+ if ( this . pendingConnectionData . socketTimer ) {
434+ clearTimeout ( this . pendingConnectionData . socketTimer ) ;
435+ }
436+ if ( this . pendingConnectionData . socket ) {
437+ this . pendingConnectionData . socket . removeAllListeners ( ) ;
438+ }
439+
440+ this . pendingConnectionData . rejectHandler ( "LiveSync aborted." ) ;
441+ }
413442 this . end ( ) ;
414443
415444 _ . keys ( this . operationPromises )
0 commit comments