mirror of
https://github.com/LBRYFoundation/lbry-desktop.git
synced 2025-08-23 17:47:24 +00:00
- 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
69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
const jsonrpc = {};
|
|
|
|
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);
|
|
|
|
if (response.error) {
|
|
if (errorCallback) {
|
|
errorCallback(response.error);
|
|
} else {
|
|
var errorEvent = new CustomEvent('unhandledError', {
|
|
detail: {
|
|
connectionString: connectionString,
|
|
method: method,
|
|
params: params,
|
|
code: response.error.code,
|
|
message: response.error.message,
|
|
data: response.error.data
|
|
}
|
|
});
|
|
document.dispatchEvent(errorEvent)
|
|
}
|
|
} else if (callback) {
|
|
callback(response.result);
|
|
}
|
|
});
|
|
|
|
if (connectFailedCallback) {
|
|
xhr.addEventListener('error', function (event) {
|
|
connectFailedCallback(event);
|
|
});
|
|
} else {
|
|
xhr.addEventListener('error', function (event) {
|
|
var errorEvent = new CustomEvent('unhandledError', {
|
|
detail: {
|
|
connectionString: connectionString,
|
|
method: method,
|
|
params: params,
|
|
code: xhr.status,
|
|
message: 'Connection to API server failed'
|
|
}
|
|
});
|
|
document.dispatchEvent(errorEvent);
|
|
});
|
|
}
|
|
|
|
xhr.open('POST', connectionString, true);
|
|
xhr.send(JSON.stringify({
|
|
'jsonrpc': '2.0',
|
|
'method': method,
|
|
'params': params,
|
|
'id': 0
|
|
}));
|
|
};
|
|
|
|
export default jsonrpc;
|