Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions lib/services/livesync/android-livesync-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
private socketError: string | Error;
private socketConnection: IDuplexSocket;
private configuration: IAndroidLivesyncToolConfiguration;
private pendingConnectionData: {
connectionTimer?: NodeJS.Timer,
socketTimer?: NodeJS.Timer,
rejectHandler?: Function,
socket?: IDuplexSocket
} = null;

constructor(private $androidProcessService: Mobile.IAndroidProcessService,
private $errors: IErrors,
Expand Down Expand Up @@ -295,16 +301,25 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
let lastKnownError: Error | string,
isConnected = false;

setTimeout(() => {
const connectionTimer = setTimeout(() => {
if (!isConnected) {
isConnected = true;
reject(lastKnownError);
reject(lastKnownError || { message: "Socket connection timeouted." });
this.pendingConnectionData = null;
}
}, timeout);

this.pendingConnectionData = {
connectionTimer,
rejectHandler: reject
};

const tryConnect = () => {
const socket = factory();

const tryConnectAfterTimeout = (error: Error) => {
if (isConnected) {
this.pendingConnectionData = null;
return;
}

Expand All @@ -313,10 +328,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
}

lastKnownError = error;
setTimeout(tryConnect, 1000);
socket.removeAllListeners();
this.pendingConnectionData.socketTimer = setTimeout(tryConnect, 1000);
};

const socket = factory();
this.pendingConnectionData.socket = socket;

socket.once("data", data => {
socket.removeListener("close", tryConnectAfterTimeout);
Expand Down Expand Up @@ -366,6 +382,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
const error = this.getErrorWithMessage(errorMessage);
if (this.socketConnection && this.socketConnection.uid === socketId) {
this.end();
this.socketConnection.removeAllListeners();
this.socketConnection = null;
this.socketError = error;
}
Expand Down Expand Up @@ -410,6 +427,18 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
}

private dispose(): void {
if (this.pendingConnectionData) {
clearTimeout(this.pendingConnectionData.connectionTimer);

if (this.pendingConnectionData.socketTimer) {
clearTimeout(this.pendingConnectionData.socketTimer);
}
if (this.pendingConnectionData.socket) {
this.pendingConnectionData.socket.removeAllListeners();
}

this.pendingConnectionData.rejectHandler("LiveSync aborted.");
}
this.end();

_.keys(this.operationPromises)
Expand Down