Tweak logic for handling failed connections in lbry.jsonrpc_call()

- Move AJAX error callback declaration to the right spot (before,
   it was inside the load callback)
 - Add support for custom timeouts
This commit is contained in:
Alex Liebowitz 2016-11-11 09:22:53 -05:00
parent e0d68a9619
commit e6fc50e01f

View file

@ -10,8 +10,20 @@ var lbry = {
}
};
lbry.jsonrpc_call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback) {
lbry.jsonrpc_call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback, timeout) {
var xhr = new XMLHttpRequest;
if (typeof connectFailedCallback !== 'undefined') {
if (timeout) {
xhr.timeout = timeout;
}
xhr.addEventListener('error', function (e) {
connectFailedCallback(e);
});
xhr.addEventListener('timeout', function() {
connectFailedCallback(new Error('XMLHttpRequest connection timed out'));
})
}
xhr.addEventListener('load', function() {
var response = JSON.parse(xhr.responseText);
@ -24,12 +36,6 @@ lbry.jsonrpc_call = function (connectionString, method, params, callback, errorC
}
});
if (connectFailedCallback) {
xhr.addEventListener('error', function (e) {
connectFailedCallback(e);
});
}
xhr.open('POST', connectionString, true);
xhr.send(JSON.stringify({
'jsonrpc': '2.0',