diff --git a/js/lbry.js b/js/lbry.js index 76bd21a81..9c013c0fa 100644 --- a/js/lbry.js +++ b/js/lbry.js @@ -1,3 +1,5 @@ +import lighthouse from './lighthouse.js'; + var lbry = { isConnected: false, rootPath: '.', @@ -194,6 +196,44 @@ 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); }