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:
parent
e0d68a9619
commit
e6fc50e01f
1 changed files with 13 additions and 7 deletions
20
js/lbry.js
20
js/lbry.js
|
@ -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',
|
||||
|
|
Loading…
Reference in a new issue