Skip to content

Commit

Permalink
fix undesirable resubscribe after reconnect #46
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Nov 25, 2017
1 parent 600de58 commit d770f8e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/centrifuge.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,10 @@ centrifugeProto._refresh = function () {
this._jsonp(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, this._config.refreshData, cb);
} else if (transport === "custom") {
if (!this._config.refreshCallback) {
throw 'Missing \'authCallback\' for custom refresh transport';
throw 'Missing \'refreshCallback\' for custom refresh transport';
}
this._config.refreshCallback(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, data, cb);
context = {};
this._config.refreshCallback(context, cb);
} else {
throw 'Unknown refresh transport ' + transport;
}
Expand Down Expand Up @@ -859,7 +860,9 @@ centrifugeProto._connectResponse = function (message) {
this.startAuthBatching();
for (var channel in this._subs) {
var sub = this._subs[channel];
this._subscribe(sub);
if (sub._shouldResubscribe()) {
this._subscribe(sub);
}
}
this.stopAuthBatching();
this.stopBatching(true);
Expand Down Expand Up @@ -983,7 +986,7 @@ centrifugeProto._unsubscribeResponse = function (message) {
if (!errorExists(message)) {
if (!uid) {
// unsubscribe command from server – unsubscribe all current subs
sub._setUnsubscribed();
sub._setUnsubscribed(true);
}
// ignore client initiated successful unsubscribe responses as we
// already unsubscribed on client level.
Expand Down Expand Up @@ -1414,7 +1417,10 @@ centrifugeProto.stopAuthBatching = function() {
if (!this._config.authCallback) {
throw 'Missing \'authCallback\' for custom auth transport';
}
this._config.authCallback(this._config.authEndpoint, this._config.authParams, this._config.authHeaders, data, cb);
context = {
"data": data
};
this._config.authCallback(context, cb);
} else {
throw 'Unknown auth transport ' + transport;
}
Expand Down Expand Up @@ -1463,6 +1469,7 @@ function Sub(centrifuge, channel, events) {
this._recovered = false;
this._ready = false;
this._promise = null;
this._noResubscribe = false;
this._initializePromise();
}

Expand Down Expand Up @@ -1571,14 +1578,21 @@ subProto._triggerUnsubscribe = function() {
this.trigger("unsubscribe", [unsubscribeContext]);
};

subProto._setUnsubscribed = function() {
subProto._setUnsubscribed = function(noResubscribe) {
if (this._status == _STATE_UNSUBSCRIBED) {
return;
}
this._status = _STATE_UNSUBSCRIBED;
if (noResubscribe === true) {
this._noResubscribe = true;
}
this._triggerUnsubscribe();
};

subProto._shouldResubscribe = function() {
return !this._noResubscribe;
}

subProto._getSubscribeSuccessContext = function() {
return {
"channel": this.channel,
Expand Down Expand Up @@ -1613,7 +1627,7 @@ subProto.subscribe = function() {
};

subProto.unsubscribe = function () {
this._setUnsubscribed();
this._setUnsubscribed(true);
this._centrifuge._unsubscribe(this);
};

Expand Down

0 comments on commit d770f8e

Please sign in to comment.