diff --git a/js/lbry.js b/js/lbry.js
index 4be044fb0..9c013c0fa 100644
--- a/js/lbry.js
+++ b/js/lbry.js
@@ -1,3 +1,5 @@
+import lighthouse from './lighthouse.js';
+
var lbry = {
isConnected: false,
rootPath: '.',
@@ -179,14 +181,59 @@ lbry.getMyClaim = function(name, callback) {
lbry.call('get_my_claim', { name: name }, callback);
}
-lbry.getCostEstimate = function(name, callback) {
+lbry.getKeyFee = function(name, callback) {
lbry.call('get_est_cost', { name: name }, callback);
}
+lbry.getTotalCost = function(name, size, callback) {
+ lbry.call('get_est_cost', {
+ name: name,
+ size: size,
+ }, callback);
+}
+
lbry.getPeersForBlobHash = function(blobHash, callback) {
lbry.call('get_peers_for_hash', { blob_hash: blobHash }, callback)
}
+lbry.getCostInfoForName = function(name, callback) {
+ /**
+ * Takes a LBRY name; will first try and calculate a total cost using
+ * Lighthouse. If Lighthouse can't be reached, it just retrives the
+ * key fee.
+ *
+ * Returns an object with members:
+ * - cost: Number; the calculated cost of the name
+ * - includes_data: Boolean; indicates whether or not the data fee info
+ * from Lighthouse is included.
+ */
+ function getCostWithData(size, callback) {
+ lbry.getTotalCost(name, size, (cost) => {
+ callback({
+ cost: cost,
+ includesData: true,
+ });
+ });
+ }
+
+ function getCostNoData(name, callback) {
+ lbry.getKeyFee(name, (cost) => {
+ callback({
+ cost: cost,
+ includesData: false,
+ });
+ });
+ }
+
+ lighthouse.getSizeForName(name, (size) => {
+ getCostWithData(name, size, callback);
+ }, () => {
+ getCostNoData(name, callback);
+ }, () => {
+ getCostNoData(name, callback);
+ });
+}
+
lbry.getFileStatus = function(name, callback) {
lbry.call('get_lbry_file', { 'name': name }, callback);
}
diff --git a/js/lighthouse.js b/js/lighthouse.js
index 73315cabb..0c5d6934a 100644
--- a/js/lighthouse.js
+++ b/js/lighthouse.js
@@ -15,23 +15,31 @@ 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);
+ },
+
+ getSizeForName: function(name, callback, errorCallback, connectFailedCallback, timeout) {
+ return lighthouse.call('get_size_for_name', [name], callback, errorCallback, connectFailedCallback, timeout);
}
};
diff --git a/js/page/discover.js b/js/page/discover.js
index f533a3afb..77633ca84 100644
--- a/js/page/discover.js
+++ b/js/page/discover.js
@@ -46,8 +46,7 @@ var SearchResults = React.createClass({
var mediaType = lbry.getMediaType(result.value.content_type);
rows.push(