Compute cost estimate on client side when possible
This commit is contained in:
parent
9b4ebbab0f
commit
757ab11779
4 changed files with 44 additions and 24 deletions
|
@ -62,6 +62,7 @@ export let CreditAmount = React.createClass({
|
|||
propTypes: {
|
||||
amount: React.PropTypes.number.isRequired,
|
||||
precision: React.PropTypes.number,
|
||||
isEstimate: React.PropTypes.bool,
|
||||
label: React.PropTypes.bool,
|
||||
showFree: React.PropTypes.bool,
|
||||
look: React.PropTypes.oneOf(['indicator', 'plain']),
|
||||
|
@ -100,7 +101,6 @@ export let FilePrice = React.createClass({
|
|||
_isMounted: false,
|
||||
|
||||
propTypes: {
|
||||
metadata: React.PropTypes.object,
|
||||
uri: React.PropTypes.string.isRequired,
|
||||
look: React.PropTypes.oneOf(['indicator', 'plain']),
|
||||
},
|
||||
|
@ -113,8 +113,8 @@ export let FilePrice = React.createClass({
|
|||
|
||||
componentWillMount: function() {
|
||||
this.setState({
|
||||
cost: this.props.metadata ? this.props.metadata.fee : null,
|
||||
isEstimate: this.props.metadata ? true : null,
|
||||
cost: null,
|
||||
isEstimate: null,
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -124,7 +124,7 @@ export let FilePrice = React.createClass({
|
|||
if (this._isMounted) {
|
||||
this.setState({
|
||||
cost: cost,
|
||||
isEstimate: includesData,
|
||||
isEstimate: !includesData,
|
||||
});
|
||||
}
|
||||
}, (err) => {
|
||||
|
@ -141,7 +141,7 @@ export let FilePrice = React.createClass({
|
|||
return <span className={`credit-amount credit-amount--${this.props.look}`}>???</span>;
|
||||
}
|
||||
|
||||
return <CreditAmount label={false} amount={this.props.metadata.fee.amount} isEstimate={true} showFree={true} />
|
||||
return <CreditAmount label={false} amount={this.state.cost} isEstimate={this.state.isEstimate} showFree={true} />
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ export let FileTileStream = React.createClass({
|
|||
return {
|
||||
showNsfwHelp: false,
|
||||
isHidden: false,
|
||||
available: null,
|
||||
}
|
||||
},
|
||||
getDefaultProps: function() {
|
||||
|
@ -143,7 +142,6 @@ export let FileCardStream = React.createClass({
|
|||
return {
|
||||
showNsfwHelp: false,
|
||||
isHidden: false,
|
||||
available: null,
|
||||
}
|
||||
},
|
||||
getDefaultProps: function() {
|
||||
|
@ -232,7 +230,6 @@ export let FileTile = React.createClass({
|
|||
|
||||
propTypes: {
|
||||
uri: React.PropTypes.string.isRequired,
|
||||
available: React.PropTypes.bool,
|
||||
},
|
||||
|
||||
getInitialState: function() {
|
||||
|
|
|
@ -217,12 +217,18 @@ lbry.getPeersForBlobHash = function(blobHash, callback) {
|
|||
lbry.costPromiseCache = {}
|
||||
lbry.getCostInfo = function(lbryUri) {
|
||||
if (lbry.costPromiseCache[lbryUri] === undefined) {
|
||||
const COST_INFO_CACHE_KEY = 'cost_info_cache';
|
||||
lbry.costPromiseCache[lbryUri] = new Promise((resolve, reject) => {
|
||||
const COST_INFO_CACHE_KEY = 'cost_info_cache';
|
||||
let costInfoCache = getSession(COST_INFO_CACHE_KEY, {})
|
||||
|
||||
function cacheAndResolve(cost, includesData) {
|
||||
costInfoCache[lbryUri] = {cost, includesData};
|
||||
setSession(COST_INFO_CACHE_KEY, costInfoCache);
|
||||
resolve({cost, includesData});
|
||||
}
|
||||
|
||||
if (!lbryUri) {
|
||||
reject(new Error(`URI required.`));
|
||||
return reject(new Error(`URI required.`));
|
||||
}
|
||||
|
||||
if (costInfoCache[lbryUri] && costInfoCache[lbryUri].cost) {
|
||||
|
@ -231,27 +237,44 @@ lbry.getCostInfo = function(lbryUri) {
|
|||
|
||||
function getCost(lbryUri, size) {
|
||||
lbry.stream_cost_estimate({uri: lbryUri, ... size !== null ? {size} : {}}).then((cost) => {
|
||||
costInfoCache[lbryUri] = {
|
||||
cost: cost,
|
||||
includesData: size !== null,
|
||||
};
|
||||
setSession(COST_INFO_CACHE_KEY, costInfoCache);
|
||||
resolve(costInfoCache[lbryUri]);
|
||||
cacheAndResolve(cost, size !== null);
|
||||
}, reject);
|
||||
}
|
||||
|
||||
function getCostGenerous(lbryUri) {
|
||||
// If generous is on, the calculation is simple enough that we might as well do it here in the front end
|
||||
lbry.resolve({uri: lbryUri}).then((resolutionInfo) => {
|
||||
const fee = resolutionInfo.claim.value.stream.metadata.fee;
|
||||
if (fee === undefined) {
|
||||
cacheAndResolve(0, true);
|
||||
} else if (fee.currency == 'LBC') {
|
||||
cacheAndResolve(fee.amount, true);
|
||||
} else {
|
||||
lbryio.getExchangeRates().then(({lbc_usd}) => {
|
||||
cacheAndResolve(fee.amount / lbc_usd, true);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const uriObj = uri.parseLbryUri(lbryUri);
|
||||
const name = uriObj.path || uriObj.name;
|
||||
|
||||
lighthouse.get_size_for_name(name).then((size) => {
|
||||
if (size) {
|
||||
getCost(name, size);
|
||||
lbry.settings_get({allow_cached: true}).then(({is_generous_host}) => {
|
||||
if (is_generous_host) {
|
||||
return getCostGenerous(lbryUri);
|
||||
}
|
||||
else {
|
||||
|
||||
lighthouse.get_size_for_name(name).then((size) => {
|
||||
if (size) {
|
||||
getCost(name, size);
|
||||
}
|
||||
else {
|
||||
getCost(name, null);
|
||||
}
|
||||
}, () => {
|
||||
getCost(name, null);
|
||||
}
|
||||
}, () => {
|
||||
getCost(name, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ lbryio.getExchangeRates = function() {
|
|||
});
|
||||
}
|
||||
|
||||
lbryio.call = function(resource, action, params={}, method='get', evenIfDisabled=false) { // evenIfDisabled is for development, when we may have some calls working and some not
|
||||
lbryio.call = function(resource, action, params={}, method='get', evenIfDisabled=false) { // evenIfDisabled is just for development, when we may have some calls working and some not
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!lbryio.enabled && !evenIfDisabled && (resource != 'discover' || action != 'list')) {
|
||||
reject(new Error("LBRY interal API is disabled"))
|
||||
|
|
Loading…
Reference in a new issue