Track downloading files by outpoint

This commit is contained in:
6ea86b96 2017-07-21 15:02:29 +07:00
parent 82b6e3af07
commit b99d58c3f9
3 changed files with 27 additions and 22 deletions

View file

@ -5,7 +5,7 @@ import lbryuri from "lbryuri";
import { selectBalance } from "selectors/wallet";
import {
selectFileInfoForUri,
selectUrisDownloading,
selectDownloadingByOutpoint,
} from "selectors/file_info";
import { selectResolvingUris } from "selectors/content";
import { selectCostInfoForUri } from "selectors/cost_info";
@ -265,8 +265,9 @@ export function doPurchaseUri(uri, purchaseModalName) {
const state = getState();
const balance = selectBalance(state);
const fileInfo = selectFileInfoForUri(state, { uri });
const downloadingByUri = selectUrisDownloading(state);
const alreadyDownloading = !!downloadingByUri[uri];
const downloadingByOutpoint = selectDownloadingByOutpoint(state);
const alreadyDownloading =
fileInfo && !!downloadingByOutpoint[fileInfo.outpoint];
// we already fully downloaded the file.
if (fileInfo && fileInfo.completed) {

View file

@ -58,15 +58,15 @@ reducers[types.DOWNLOADING_STARTED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
const newDownloading = Object.assign({}, state.downloadingByOutpoin);
const newLoading = Object.assign({}, state.urisLoading);
newDownloading[uri] = true;
newDownloading[outpoint] = true;
newByOutpoint[outpoint] = fileInfo;
delete newLoading[uri];
return Object.assign({}, state, {
urisDownloading: newDownloading,
downloadingByOutpoint: newDownloading,
urisLoading: newLoading,
byOutpoint: newByOutpoint,
});
@ -76,14 +76,14 @@ reducers[types.DOWNLOADING_PROGRESSED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
const newDownloading = Object.assign({}, state.downloadingByOutpoint);
newByOutpoint[outpoint] = fileInfo;
newDownloading[uri] = true;
newDownloading[outpoint] = true;
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
urisDownloading: newDownloading,
downloadingByOutpoint: newDownloading,
});
};
@ -91,14 +91,14 @@ reducers[types.DOWNLOADING_COMPLETED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
const newDownloading = Object.assign({}, state.downloadingByOutpoint);
newByOutpoint[outpoint] = fileInfo;
delete newDownloading[uri];
delete newDownloading[outpoint];
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
urisDownloading: newDownloading,
downloadingByOutpoint: newDownloading,
});
};

View file

@ -39,14 +39,18 @@ export const makeSelectFileInfoForUri = () => {
return createSelector(selectFileInfoForUri, fileInfo => fileInfo);
};
export const selectUrisDownloading = createSelector(
export const selectDownloadingByOutpoint = createSelector(
_selectState,
state => state.urisDownloading || {}
state => state.downloadingByOutpoint || {}
);
const selectDownloadingForUri = (state, props) => {
const byUri = selectUrisDownloading(state);
return byUri[props.uri];
const byOutpoint = selectDownloadingByOutpoint(state);
const fileInfo = selectFileInfoForUri(state, props);
if (!fileInfo) return false;
return byOutpoint[fileInfo.outpoint];
};
export const makeSelectDownloadingForUri = () => {
@ -135,14 +139,14 @@ export const selectFileInfosByUri = createSelector(
);
export const selectDownloadingFileInfos = createSelector(
selectUrisDownloading,
selectFileInfosByUri,
(urisDownloading, byUri) => {
const uris = Object.keys(urisDownloading);
selectDownloadingByOutpoint,
selectFileInfosByOutpoint,
(downloadingByOutpoint, fileInfosByOutpoint) => {
const outpoints = Object.keys(downloadingByOutpoint);
const fileInfos = [];
uris.forEach(uri => {
const fileInfo = byUri[uri];
outpoints.forEach(outpoint => {
const fileInfo = fileInfosByOutpoint[outpoint];
if (fileInfo) fileInfos.push(fileInfo);
});