diff --git a/src/renderer/redux/actions/subscriptions.js b/src/renderer/redux/actions/subscriptions.js index 3d7edb87f..5fd208924 100644 --- a/src/renderer/redux/actions/subscriptions.js +++ b/src/renderer/redux/actions/subscriptions.js @@ -5,7 +5,7 @@ import * as SETTINGS from 'constants/settings'; import rewards from 'rewards'; import type { Dispatch, SubscriptionNotifications } from 'redux/reducers/subscriptions'; import type { Subscription } from 'types/subscription'; -import { selectSubscriptions, selectDownloadingCount } from 'redux/selectors/subscriptions'; +import { selectSubscriptions } from 'redux/selectors/subscriptions'; import { makeSelectClientSetting } from 'redux/selectors/settings'; import { Lbry, buildURI, parseURI } from 'lbry-redux'; import { doPurchaseUri } from 'redux/actions/content'; @@ -14,7 +14,7 @@ import Promise from 'bluebird'; import Lbryio from 'lbryio'; const CHECK_SUBSCRIPTIONS_INTERVAL = 15 * 60 * 1000; -const SUBSCRIPTION_DOWNLOAD_LIMIT = 3; +const SUBSCRIPTION_DOWNLOAD_LIMIT = 1; export const doFetchMySubscriptions = () => (dispatch: Dispatch, getState: () => any) => { const { @@ -124,11 +124,10 @@ export const setSubscriptionNotification = ( }, }); -export const doCheckSubscription = ( - subscription: Subscription, - notify?: boolean, - preventDownload?: boolean -) => (dispatch: Dispatch, getState: () => {}) => { +export const doCheckSubscription = (subscription: Subscription, notify?: boolean) => ( + dispatch: Dispatch, + getState: () => {} +) => { // no dispatching FETCH_CHANNEL_CLAIMS_STARTED; causes loading issues on Lbry.claim_list_by_channel({ uri: subscription.uri, page: 1 }).then(result => { @@ -140,13 +139,12 @@ export const doCheckSubscription = ( ); if (claimsInChannel.length && latestIndex !== 0) { + let downloadCount = 0; claimsInChannel.slice(0, latestIndex === -1 ? 10 : latestIndex).forEach(claim => { const uri = buildURI({ contentName: claim.name, claimId: claim.claim_id }, false); const state = getState(); - const downloadCount = selectDownloadingCount(state); const shouldDownload = Boolean( - !preventDownload && - downloadCount < SUBSCRIPTION_DOWNLOAD_LIMIT && + downloadCount < SUBSCRIPTION_DOWNLOAD_LIMIT && !claim.value.stream.metadata.fee && makeSelectClientSetting(SETTINGS.AUTO_DOWNLOAD)(state) ); @@ -160,6 +158,7 @@ export const doCheckSubscription = ( ); } if (shouldDownload) { + downloadCount += 1; dispatch(doPurchaseUri(uri, { cost: 0 })); } }); @@ -233,7 +232,7 @@ export const doChannelSubscribe = (subscription: Subscription) => ( dispatch(doClaimRewardType(rewards.SUBSCRIPTION, { failSilently: true })); } - dispatch(doCheckSubscription(subscription, false, true)); + dispatch(doCheckSubscription(subscription, true)); }; export const doChannelUnsubscribe = (subscription: Subscription) => ( @@ -267,7 +266,10 @@ export const doCheckSubscriptions = () => (dispatch: Dispatch, getState: () => a }; export const doCheckSubscriptionsInit = () => (dispatch: Dispatch) => { - setTimeout(() => dispatch(doCheckSubscriptions()), 5000); // bad fix for not getting subs on load + // doCheckSubscriptionsInit is called by doDaemonReady + // setTimeout below is a hack to ensure redux is hydrated when subscriptions are checked + // this will be replaced with which reqiures a package upgrade + setTimeout(() => dispatch(doCheckSubscriptions()), 5000); const checkSubscriptionsTimer = setInterval( () => dispatch(doCheckSubscriptions()), CHECK_SUBSCRIPTIONS_INTERVAL diff --git a/src/renderer/redux/selectors/subscriptions.js b/src/renderer/redux/selectors/subscriptions.js index 7b95604d0..12c35ae74 100644 --- a/src/renderer/redux/selectors/subscriptions.js +++ b/src/renderer/redux/selectors/subscriptions.js @@ -1,4 +1,3 @@ -import * as NOTIFICATION_TYPES from 'constants/notification_types'; import { createSelector } from 'reselect'; import { selectAllClaimsByChannel, @@ -44,11 +43,13 @@ export const selectSubscriptionClaims = createSelector( }); } - fetchedSubscriptions = fetchedSubscriptions.concat([{ - claims: [...channelClaims], - channelName: subscription.channelName, - uri: subscription.uri, - }]); + fetchedSubscriptions = fetchedSubscriptions.concat([ + { + claims: [...channelClaims], + channelName: subscription.channelName, + uri: subscription.uri, + }, + ]); }); return [...fetchedSubscriptions]; @@ -70,8 +71,3 @@ export const selectSubscriptionsBeingFetched = createSelector( return fetchingSubscriptionMap; } ); - -export const selectDownloadingCount = createSelector( - selectNotifications, - notifs => Object.values(notifs).reduce((acc, notif) => notif.type === NOTIFICATION_TYPES.DOWNLOADING ? acc + 1 : acc, 0) -);