@@ -164,9 +164,17 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
164164 return operationPromise ;
165165 }
166166
167- public end ( ) {
167+ public end ( error ?: Error ) {
168168 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 ) ;
170178 }
171179 }
172180
@@ -381,12 +389,21 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
381389 private handleSocketError ( socketId : string , errorMessage : string ) {
382390 const error = this . getErrorWithMessage ( errorMessage ) ;
383391 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 ) {
385401 this . socketConnection . removeAllListeners ( ) ;
386402 this . socketConnection = null ;
387- this . socketError = error ;
388403 }
404+ }
389405
406+ private rejectPendingSyncOperations ( socketId : string , error : Error ) {
390407 _ . keys ( this . operationPromises )
391408 . forEach ( operationId => {
392409 const operationPromise = this . operationPromises [ operationId ] ;
@@ -395,7 +412,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
395412 operationPromise . reject ( error ) ;
396413 delete this . operationPromises [ operationId ] ;
397414 }
398- } ) ;
415+ } ) ;
399416 }
400417
401418 private getErrorWithMessage ( errorMessage : string ) {
0 commit comments