Merge pull request #770 from glowing-bear/connectionlock

Add a lock on the connection
This commit is contained in:
Tor Hveem 2016-04-13 17:33:26 +02:00
commit a08452548e

View file

@ -15,6 +15,9 @@ weechat.factory('connection',
var connectionData = [];
var reconnectTimer;
// Global connection lock to prevent multiple connections from being opened
var locked = false;
// Takes care of the connection and websocket hooks
var connect = function (host, port, passwd, ssl, noCompression, successCallback, failCallback) {
$rootScope.passwordError = false;
@ -120,6 +123,7 @@ weechat.factory('connection',
*/
$log.info("Disconnected from relay");
$rootScope.$emit('relayDisconnect');
locked = false;
if ($rootScope.userdisconnect || !$rootScope.waseverconnected) {
handleClose(evt);
$rootScope.userdisconnect = false;
@ -154,6 +158,7 @@ weechat.factory('connection',
* the relay.
*/
$log.error("Relay error", evt);
locked = false; // release connection lock
$rootScope.lastError = Date.now();
if (evt.type === "error" && this.readyState !== 1) {
@ -162,6 +167,13 @@ weechat.factory('connection',
}
};
if (locked) {
// We already have an open connection
$log.debug("Aborting connection (lock in use)");
}
// Kinda need a compare-and-swap here...
locked = true;
try {
ngWebsockets.connect(url,
protocol,
@ -173,6 +185,7 @@ weechat.factory('connection',
'onerror': onerror
});
} catch(e) {
locked = false;
$log.debug("Websocket caught DOMException:", e);
$rootScope.lastError = Date.now();
$rootScope.errorMessage = true;
@ -250,6 +263,7 @@ weechat.factory('connection',
// The connection can time out on its own
ngWebsockets.failCallbacks('disconnection');
$rootScope.connected = false;
locked = false; // release the connection lock
$rootScope.$emit('relayDisconnect');
$rootScope.$apply();
});