Skip to content

Commit 9c68ca5

Browse files
committed
Merge pull request #230 from ParsePlatform/peterjs.reconnectFix
LQ reconnect fix
2 parents b9a661d + c10394c commit 9c68ca5

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/LiveQueryClient.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export default class LiveQueryClient extends events.EventEmitter {
149149
throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
150150
}
151151

152+
this.reconnectHandle = null;
152153
this.attempts = 1;;
153154
this.id = 0;
154155
this.requestId = 1;
@@ -427,13 +428,25 @@ export default class LiveQueryClient extends events.EventEmitter {
427428

428429
_handleReconnect() {
429430
// if closed or currently reconnecting we stop attempting to reconnect
430-
if (this.state === CLIENT_STATE.DISCONNECTED || this.state === CLIENT_STATE.RECONNECTING) {
431+
if (this.state === CLIENT_STATE.DISCONNECTED) {
431432
return;
432433
}
434+
433435
this.state = CLIENT_STATE.RECONNECTING;
434436
let time = generateInterval(this.attempts);
435-
console.info('attempting to reconnect after ' + time + 'ms');
436-
setTimeout((() => {
437+
438+
// handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
439+
// we're unable to distinguish different between close/error when we're unable to reconnect therefore
440+
// we try to reonnect in both cases
441+
// server side ws and browser WebSocket behave differently in when close/error get triggered
442+
443+
if (this.reconnectHandle) {
444+
clearTimeout(this.reconnectHandle);
445+
} else {
446+
console.info('attempting to reconnect');
447+
}
448+
449+
this.reconnectHandle = setTimeout((() => {
437450
this.attempts++;
438451
this.connectPromise = new ParsePromise();
439452
this.open();

0 commit comments

Comments
 (0)