From 4bdadc4551f51b11d5cb584dedf3a093eb62c23a Mon Sep 17 00:00:00 2001 From: Alex Liebowitz Date: Thu, 24 Mar 2016 21:20:58 -0400 Subject: [PATCH] Fixes to improve loading screen - Add optional "connection failed" callback to lbry.call() - Make loading screen handle condition where daemon is not running at all (i.e. AJAX connection fails) --- js/lbry.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/js/lbry.js b/js/lbry.js index 2810eee64..34525badc 100644 --- a/js/lbry.js +++ b/js/lbry.js @@ -6,7 +6,7 @@ var lbry = { } }; -lbry.call = function (method, params, callback) +lbry.call = function (method, params, callback, connectFailedCallback) { var xhr = new XMLHttpRequest; xhr.addEventListener('load', function() { @@ -19,6 +19,13 @@ lbry.call = function (method, params, callback) console.log(method_output.result); callback(method_output.result); }); + + if (connectFailedCallback) { + xhr.addEventListener('error', function (e) { + connectFailedCallback(e); + }); + } + xhr.open('POST', 'http://localhost:5279/lbryapi', true); xhr.send(JSON.stringify({ 'jsonrpc': '2.0', @@ -33,24 +40,34 @@ lbry.connect = function(callback) { // Check every half second to see if the daemon's running. // Returns true to callback once connected, or false if it takes too long and we give up. - var try_num = 0; - var check_connected = setInterval(function() { - lbry.call('is_running', {}, function(is_running) { - if (is_running) { + function checkDaemonRunning(tryNum=0) { + lbry.daemonRunningStatus(function (runningStatus) { + if (runningStatus) { lbry.isConnected = true; - clearInterval(check_connected); callback(true); } else { - if (try_num >= 20) { // Move this into a constant or config option - clearInterval(check_connected); + if (tryNum <= 50) { // Move # of tries into constant or config option + setTimeout(function () { + checkDaemonRunning(tryNum + 1); + }, 500); + } else { callback(false); } - try_num++; } }); - }, 500); + } + checkDaemonRunning(); } +lbry.daemonRunningStatus = function (callback) { + // Returns true/false whether the daemon is running (i.e. fully conncected to the network), + // or null if the AJAX connection to the daemon fails. + + lbry.call('is_running', {}, callback, function () { + callback(null); + }); +}; + lbry.getBalance = function(callback) { lbry.call("get_balance", {}, callback);