lbry-desktop/ui/js/lighthouse.js
Alex Liebowitz 1567a2de0a Revamp API wrapper code
- Refactoring throughout JSON-RPC, lbrynet and Lighthouse logic
 - Move JSON-RPC stuff into its own module
 - Add ability to directly call API methods on the lbry and lighthouse
   modules, e.g. lbry.file_list({name: 'what'})
 - New-style API calls use promises instead of callbacks.
 - Converted some lbrynet calls and all Lighthouse calls to use the new
   style
2017-03-09 17:54:46 -05:00

65 lines
1.8 KiB
JavaScript

import lbry from './lbry.js';
import jsonrpc from './jsonrpc.js';
const queryTimeout = 5000;
const maxQueryTries = 5;
const defaultServers = [
'http://lighthouse4.lbry.io:50005',
'http://lighthouse5.lbry.io:50005',
'http://lighthouse6.lbry.io:50005',
];
const path = '/';
let server = null;
let connectTryNum = 0;
function getServers() {
return lbry.getClientSetting('useCustomLighthouseServers')
? lbry.getClientSetting('customLighthouseServers')
: defaultServers;
}
function call(method, params, callback, errorCallback) {
if (connectTryNum > maxQueryTries) {
if (connectFailedCallback) {
connectFailedCallback();
} else {
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${server}`);
}
}
/**
* Set the Lighthouse server if it hasn't been set yet, if the current server is not in current
* set of servers (most likely because of a settings change), or we're re-trying after a failed
* query.
*/
if (!server || !getServers().includes(server) || connectTryNum > 0) {
// If there's a current server, filter it out so we get a new one
const newServerChoices = server ? getServers().filter((s) => s != server) : getServers();
server = newServerChoices[Math.round(Math.random() * (newServerChoices.length - 1))];
}
jsonrpc.call(server + path, method, params, (response) => {
connectTryNum = 0;
callback(response);
}, (error) => {
connectTryNum = 0;
errorCallback(error);
}, () => {
connectTryNum++;
call(method, params, callback, errorCallback);
});
}
const lighthouse = new Proxy({}, {
get: function(target, name) {
return function(...params) {
return new Promise((resolve, reject) => {
call(name, params, resolve, reject);
});
};
},
});
export default lighthouse;