Fail all callbacks disconnection / error

Callback promises are resolved when their answer is received
If the websocket disconnects or fails before receives its answer,
it's promise is rejected.
This commit is contained in:
David Cormier 2013-10-27 10:49:54 -04:00
parent 5e70391bcf
commit d6ceadb1be
1 changed files with 14 additions and 0 deletions

View File

@ -304,9 +304,22 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
}); });
} }
/*
* Fails every currently subscribed callback for the
* given reason
* @param reason reason for failure
*/
failCallbacks = function(reason) {
for(i in callbacks) {
callbacks[i].cb.reject(reason);
}
}
websocket.onclose = function (evt) { websocket.onclose = function (evt) {
$log.info("Disconnected from relay"); $log.info("Disconnected from relay");
$rootScope.connected = false; $rootScope.connected = false;
failCallbacks('disconnection');
if ($rootScope.passwordError == true) { if ($rootScope.passwordError == true) {
$log.info("wrong password"); $log.info("wrong password");
} }
@ -332,6 +345,7 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
$rootScope.passwordError = false; $rootScope.passwordError = false;
if (evt.type == "error" && websocket.readyState != 1) { if (evt.type == "error" && websocket.readyState != 1) {
failCallbacks('error');
$rootScope.errorMessage = true; $rootScope.errorMessage = true;
} }
$log.error("Relay error " + evt.data); $log.error("Relay error " + evt.data);