Skip to content

Commit 5c41250

Browse files
committed
fix: socket not cleared on time for second sync
1 parent eb5140f commit 5c41250

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

lib/definitions/livesync.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,9 @@ interface IAndroidLivesyncTool {
452452

453453
/**
454454
* Closes the current socket connection.
455+
* @param error - Optional error for rejecting pending sync operations
455456
*/
456-
end(): void;
457+
end(error?: Error): void;
457458
}
458459

459460
interface IAndroidLivesyncToolConfiguration {

lib/services/livesync/android-livesync-tool.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ End will close the current liveSync socket. Any sync operations that are still i
177177
```TypeScript
178178
/**
179179
* Closes the current socket connection.
180+
* @param error - Optional error for rejecting pending sync operations
180181
*/
181-
end(): void;
182+
end(error? Error): void;
182183
```
183184
184185
* Example:

lib/services/livesync/android-livesync-tool.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -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) {

test/services/livesync/android-livesync-tool.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)