diff --git a/lib/definitions/livesync.d.ts b/lib/definitions/livesync.d.ts index 75291a5922..d10d86fd43 100644 --- a/lib/definitions/livesync.d.ts +++ b/lib/definitions/livesync.d.ts @@ -452,8 +452,9 @@ interface IAndroidLivesyncTool { /** * Closes the current socket connection. + * @param error - Optional error for rejecting pending sync operations */ - end(): void; + end(error?: Error): void; } interface IAndroidLivesyncToolConfiguration { diff --git a/lib/services/livesync/android-livesync-tool.md b/lib/services/livesync/android-livesync-tool.md index 1ea6f60fe5..dd4c9939bc 100644 --- a/lib/services/livesync/android-livesync-tool.md +++ b/lib/services/livesync/android-livesync-tool.md @@ -177,8 +177,9 @@ End will close the current liveSync socket. Any sync operations that are still i ```TypeScript /** * Closes the current socket connection. + * @param error - Optional error for rejecting pending sync operations */ -end(): void; +end(error? Error): void; ``` * Example: diff --git a/lib/services/livesync/android-livesync-tool.ts b/lib/services/livesync/android-livesync-tool.ts index 6b959e043a..7801b67b27 100644 --- a/lib/services/livesync/android-livesync-tool.ts +++ b/lib/services/livesync/android-livesync-tool.ts @@ -164,9 +164,17 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool { return operationPromise; } - public end() { + public end(error?: Error) { if (this.socketConnection) { - this.socketConnection.end(); + const socketUid = this.socketConnection.uid; + const socket = this.socketConnection; + error = error || this.getErrorWithMessage("Socket connection ended before sync operation is complete."); + //remove listeners and delete this.socketConnection + this.cleanState(socketUid); + //call end of the connection (close and error callbacks won't be called - listeners removed) + socket.end(); + //reject all pending sync requests and clear timeouts + this.rejectPendingSyncOperations(socketUid, error); } } @@ -381,12 +389,21 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool { private handleSocketError(socketId: string, errorMessage: string) { const error = this.getErrorWithMessage(errorMessage); if (this.socketConnection && this.socketConnection.uid === socketId) { - this.end(); + this.socketError = error; + this.end(error); + } else { + this.rejectPendingSyncOperations(socketId, error); + } + } + + private cleanState(socketId: string) { + if (this.socketConnection && this.socketConnection.uid === socketId) { this.socketConnection.removeAllListeners(); this.socketConnection = null; - this.socketError = error; } + } + private rejectPendingSyncOperations(socketId: string, error: Error) { _.keys(this.operationPromises) .forEach(operationId => { const operationPromise = this.operationPromises[operationId]; @@ -395,7 +412,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool { operationPromise.reject(error); delete this.operationPromises[operationId]; } - }); + }); } private getErrorWithMessage(errorMessage: string) { diff --git a/test/services/livesync/android-livesync-tool.ts b/test/services/livesync/android-livesync-tool.ts new file mode 100644 index 0000000000..e69de29bb2