@@ -24,6 +24,12 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
24
24
private socketError : string | Error ;
25
25
private socketConnection : IDuplexSocket ;
26
26
private configuration : IAndroidLivesyncToolConfiguration ;
27
+ private pendingConnectionData : {
28
+ connectionTimer ?: NodeJS . Timer ,
29
+ socketTimer ?: NodeJS . Timer ,
30
+ rejectHandler ?: Function ,
31
+ socket ?: IDuplexSocket
32
+ } = null ;
27
33
28
34
constructor ( private $androidProcessService : Mobile . IAndroidProcessService ,
29
35
private $errors : IErrors ,
@@ -295,16 +301,25 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
295
301
let lastKnownError : Error | string ,
296
302
isConnected = false ;
297
303
298
- setTimeout ( ( ) => {
304
+ const connectionTimer = setTimeout ( ( ) => {
299
305
if ( ! isConnected ) {
300
306
isConnected = true ;
301
- reject ( lastKnownError ) ;
307
+ reject ( lastKnownError || { message : "Socket connection timeouted." } ) ;
308
+ this . pendingConnectionData = null ;
302
309
}
303
310
} , timeout ) ;
304
311
312
+ this . pendingConnectionData = {
313
+ connectionTimer,
314
+ rejectHandler : reject
315
+ } ;
316
+
305
317
const tryConnect = ( ) => {
318
+ const socket = factory ( ) ;
319
+
306
320
const tryConnectAfterTimeout = ( error : Error ) => {
307
321
if ( isConnected ) {
322
+ this . pendingConnectionData = null ;
308
323
return ;
309
324
}
310
325
@@ -313,10 +328,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
313
328
}
314
329
315
330
lastKnownError = error ;
316
- setTimeout ( tryConnect , 1000 ) ;
331
+ socket . removeAllListeners ( ) ;
332
+ this . pendingConnectionData . socketTimer = setTimeout ( tryConnect , 1000 ) ;
317
333
} ;
318
334
319
- const socket = factory ( ) ;
335
+ this . pendingConnectionData . socket = socket ;
320
336
321
337
socket . once ( "data" , data => {
322
338
socket . removeListener ( "close" , tryConnectAfterTimeout ) ;
@@ -366,6 +382,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
366
382
const error = this . getErrorWithMessage ( errorMessage ) ;
367
383
if ( this . socketConnection && this . socketConnection . uid === socketId ) {
368
384
this . end ( ) ;
385
+ this . socketConnection . removeAllListeners ( ) ;
369
386
this . socketConnection = null ;
370
387
this . socketError = error ;
371
388
}
@@ -410,6 +427,18 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
410
427
}
411
428
412
429
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
+ }
413
442
this . end ( ) ;
414
443
415
444
_ . keys ( this . operationPromises )
0 commit comments