Recommended: optimize nextUri calculation

Instead of filtering the entire list for a suitable nextUri and then picking the first one, short-circuit when candidate is found.
This commit is contained in:
infinite-persistence 2022-03-04 20:25:49 +08:00 committed by Thomas Zarebczan
parent cc780c95ae
commit de0bc8cf99

View file

@ -117,7 +117,8 @@ export const selectRecommendedContentForUri = createCachedSelector(
}); });
// Claim to play next: playable and free claims not played before in history // Claim to play next: playable and free claims not played before in history
const nextUriToPlay = recommendedContent.filter((nextRecommendedUri) => { for (let i = 0; i < recommendedContent.length; ++i) {
const nextRecommendedUri = recommendedContent[i];
const costInfo = costInfoByUri[nextRecommendedUri] && costInfoByUri[nextRecommendedUri].cost; const costInfo = costInfoByUri[nextRecommendedUri] && costInfoByUri[nextRecommendedUri].cost;
const recommendedClaim = claimsByUri[nextRecommendedUri]; const recommendedClaim = claimsByUri[nextRecommendedUri];
const isVideo = recommendedClaim && recommendedClaim.value && recommendedClaim.value.stream_type === 'video'; const isVideo = recommendedClaim && recommendedClaim.value && recommendedClaim.value.stream_type === 'video';
@ -133,16 +134,18 @@ export const selectRecommendedContentForUri = createCachedSelector(
); );
} catch (e) {} } catch (e) {}
return !historyMatch && costInfo === 0 && (isVideo || isAudio); if (!historyMatch && costInfo === 0 && (isVideo || isAudio)) {
})[0]; // Better next-uri found, swap with top entry:
if (i > 0) {
const index = recommendedContent.indexOf(nextUriToPlay);
if (index > 0) {
const a = recommendedContent[0]; const a = recommendedContent[0];
recommendedContent[0] = nextUriToPlay; recommendedContent[0] = nextRecommendedUri;
recommendedContent[index] = a; recommendedContent[i] = a;
}
break;
} }
} }
}
return recommendedContent; return recommendedContent;
} }
)((state, uri) => String(uri)); )((state, uri) => String(uri));