From 0b58ae6e6e10adb920949e98c409fa67195e7fb6 Mon Sep 17 00:00:00 2001 From: Alex Liebowitz Date: Wed, 7 Dec 2016 18:00:31 -0500 Subject: [PATCH] Add error and connection failure callbacks to Lighthouse methods --- js/lighthouse.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/js/lighthouse.js b/js/lighthouse.js index 73315cabb..588b924c8 100644 --- a/js/lighthouse.js +++ b/js/lighthouse.js @@ -15,23 +15,27 @@ var lighthouse = { 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) { 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 { // Randomly choose one of the other search servers to switch to let otherServers = lighthouse.servers.slice(); otherServers.splice(otherServers.indexOf(lighthouse.server), 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); }, 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); } };