Compute cost estimate on client side when possible

This commit is contained in:
Alex Liebowitz 2017-04-17 16:22:08 -04:00
parent 9b4ebbab0f
commit 757ab11779
4 changed files with 44 additions and 24 deletions

View file

@ -62,6 +62,7 @@ export let CreditAmount = React.createClass({
propTypes: { propTypes: {
amount: React.PropTypes.number.isRequired, amount: React.PropTypes.number.isRequired,
precision: React.PropTypes.number, precision: React.PropTypes.number,
isEstimate: React.PropTypes.bool,
label: React.PropTypes.bool, label: React.PropTypes.bool,
showFree: React.PropTypes.bool, showFree: React.PropTypes.bool,
look: React.PropTypes.oneOf(['indicator', 'plain']), look: React.PropTypes.oneOf(['indicator', 'plain']),
@ -100,7 +101,6 @@ export let FilePrice = React.createClass({
_isMounted: false, _isMounted: false,
propTypes: { propTypes: {
metadata: React.PropTypes.object,
uri: React.PropTypes.string.isRequired, uri: React.PropTypes.string.isRequired,
look: React.PropTypes.oneOf(['indicator', 'plain']), look: React.PropTypes.oneOf(['indicator', 'plain']),
}, },
@ -113,8 +113,8 @@ export let FilePrice = React.createClass({
componentWillMount: function() { componentWillMount: function() {
this.setState({ this.setState({
cost: this.props.metadata ? this.props.metadata.fee : null, cost: null,
isEstimate: this.props.metadata ? true : null, isEstimate: null,
}); });
}, },
@ -124,7 +124,7 @@ export let FilePrice = React.createClass({
if (this._isMounted) { if (this._isMounted) {
this.setState({ this.setState({
cost: cost, cost: cost,
isEstimate: includesData, isEstimate: !includesData,
}); });
} }
}, (err) => { }, (err) => {
@ -141,7 +141,7 @@ export let FilePrice = React.createClass({
return <span className={`credit-amount credit-amount--${this.props.look}`}>???</span>; 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} />
} }
}); });

View file

@ -26,7 +26,6 @@ export let FileTileStream = React.createClass({
return { return {
showNsfwHelp: false, showNsfwHelp: false,
isHidden: false, isHidden: false,
available: null,
} }
}, },
getDefaultProps: function() { getDefaultProps: function() {
@ -143,7 +142,6 @@ export let FileCardStream = React.createClass({
return { return {
showNsfwHelp: false, showNsfwHelp: false,
isHidden: false, isHidden: false,
available: null,
} }
}, },
getDefaultProps: function() { getDefaultProps: function() {
@ -232,7 +230,6 @@ export let FileTile = React.createClass({
propTypes: { propTypes: {
uri: React.PropTypes.string.isRequired, uri: React.PropTypes.string.isRequired,
available: React.PropTypes.bool,
}, },
getInitialState: function() { getInitialState: function() {

View file

@ -217,12 +217,18 @@ lbry.getPeersForBlobHash = function(blobHash, callback) {
lbry.costPromiseCache = {} lbry.costPromiseCache = {}
lbry.getCostInfo = function(lbryUri) { lbry.getCostInfo = function(lbryUri) {
if (lbry.costPromiseCache[lbryUri] === undefined) { if (lbry.costPromiseCache[lbryUri] === undefined) {
const COST_INFO_CACHE_KEY = 'cost_info_cache';
lbry.costPromiseCache[lbryUri] = new Promise((resolve, reject) => { lbry.costPromiseCache[lbryUri] = new Promise((resolve, reject) => {
const COST_INFO_CACHE_KEY = 'cost_info_cache';
let costInfoCache = getSession(COST_INFO_CACHE_KEY, {}) 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) { if (!lbryUri) {
reject(new Error(`URI required.`)); return reject(new Error(`URI required.`));
} }
if (costInfoCache[lbryUri] && costInfoCache[lbryUri].cost) { if (costInfoCache[lbryUri] && costInfoCache[lbryUri].cost) {
@ -231,27 +237,44 @@ lbry.getCostInfo = function(lbryUri) {
function getCost(lbryUri, size) { function getCost(lbryUri, size) {
lbry.stream_cost_estimate({uri: lbryUri, ... size !== null ? {size} : {}}).then((cost) => { lbry.stream_cost_estimate({uri: lbryUri, ... size !== null ? {size} : {}}).then((cost) => {
costInfoCache[lbryUri] = { cacheAndResolve(cost, size !== null);
cost: cost,
includesData: size !== null,
};
setSession(COST_INFO_CACHE_KEY, costInfoCache);
resolve(costInfoCache[lbryUri]);
}, reject); }, 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 uriObj = uri.parseLbryUri(lbryUri);
const name = uriObj.path || uriObj.name; const name = uriObj.path || uriObj.name;
lighthouse.get_size_for_name(name).then((size) => { lbry.settings_get({allow_cached: true}).then(({is_generous_host}) => {
if (size) { if (is_generous_host) {
getCost(name, size); 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);
} });
}, () => {
getCost(name, null);
}); });
}); });
} }

View file

@ -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) => { return new Promise((resolve, reject) => {
if (!lbryio.enabled && !evenIfDisabled && (resource != 'discover' || action != 'list')) { if (!lbryio.enabled && !evenIfDisabled && (resource != 'discover' || action != 'list')) {
reject(new Error("LBRY interal API is disabled")) reject(new Error("LBRY interal API is disabled"))