Add error and connection failure callbacks to Lighthouse methods

This commit is contained in:
Alex Liebowitz 2016-12-07 18:00:31 -05:00
parent 6963d877df
commit 0b58ae6e6e

View file

@ -15,23 +15,27 @@ var lighthouse = {
lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback, connectFailedCallback, timeout); lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback, connectFailedCallback, timeout);
}, },
search: function(query, callback) { search: function(query, callback, errorCallback, connectFailedCallback, timeout) {
let handleSearchFailed = function(tryNum=0) { let handleSearchFailed = function(tryNum=0) {
if (tryNum > lighthouse._max_search_tries) { if (tryNum > lighthouse._max_search_tries) {
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lighthouse.server}`); if (connectFailedCallback) {
connectFailedCallback();
} else {
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lighthouse.server}`);
}
} else { } else {
// Randomly choose one of the other search servers to switch to // Randomly choose one of the other search servers to switch to
let otherServers = lighthouse.servers.slice(); let otherServers = lighthouse.servers.slice();
otherServers.splice(otherServers.indexOf(lighthouse.server), 1); otherServers.splice(otherServers.indexOf(lighthouse.server), 1);
lighthouse.server = otherServers[Math.round(Math.random() * (otherServers.length - 1))]; lighthouse.server = otherServers[Math.round(Math.random() * (otherServers.length - 1))];
lighthouse.call('search', [query], callback, undefined, function() { lighthouse.call('search', [query], callback, errorCallback, function() {
handleSearchFailed(tryNum + 1); handleSearchFailed(tryNum + 1);
}, lighthouse._search_timeout); }, lighthouse._search_timeout);
} }
} }
lighthouse.call('search', [query], callback, undefined, function() { handleSearchFailed() }, lighthouse._search_timeout); lighthouse.call('search', [query], callback, errorCallback, function() { handleSearchFailed() }, lighthouse._search_timeout);
} }
}; };