@@ -164,9 +164,17 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
164
164
return operationPromise ;
165
165
}
166
166
167
- public end ( ) {
167
+ public end ( error ?: Error ) {
168
168
if ( this . socketConnection ) {
169
- this . socketConnection . end ( ) ;
169
+ const socketUid = this . socketConnection . uid ;
170
+ const socket = this . socketConnection ;
171
+ error = error || this . getErrorWithMessage ( "Socket connection ended before sync operation is complete." ) ;
172
+ //remove listeners and delete this.socketConnection
173
+ this . cleanState ( socketUid ) ;
174
+ //call end of the connection (close and error callbacks won't be called - listeners removed)
175
+ socket . end ( ) ;
176
+ //reject all pending sync requests and clear timeouts
177
+ this . rejectPendingSyncOperations ( socketUid , error ) ;
170
178
}
171
179
}
172
180
@@ -381,12 +389,21 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
381
389
private handleSocketError ( socketId : string , errorMessage : string ) {
382
390
const error = this . getErrorWithMessage ( errorMessage ) ;
383
391
if ( this . socketConnection && this . socketConnection . uid === socketId ) {
384
- this . end ( ) ;
392
+ this . socketError = error ;
393
+ this . end ( error ) ;
394
+ } else {
395
+ this . rejectPendingSyncOperations ( socketId , error ) ;
396
+ }
397
+ }
398
+
399
+ private cleanState ( socketId : string ) {
400
+ if ( this . socketConnection && this . socketConnection . uid === socketId ) {
385
401
this . socketConnection . removeAllListeners ( ) ;
386
402
this . socketConnection = null ;
387
- this . socketError = error ;
388
403
}
404
+ }
389
405
406
+ private rejectPendingSyncOperations ( socketId : string , error : Error ) {
390
407
_ . keys ( this . operationPromises )
391
408
. forEach ( operationId => {
392
409
const operationPromise = this . operationPromises [ operationId ] ;
@@ -395,7 +412,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
395
412
operationPromise . reject ( error ) ;
396
413
delete this . operationPromises [ operationId ] ;
397
414
}
398
- } ) ;
415
+ } ) ;
399
416
}
400
417
401
418
private getErrorWithMessage ( errorMessage : string ) {
0 commit comments