From c55a2c98ab92c72149c824ee5906aed4404fd89b Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 30 Sep 2019 17:24:24 +0100 Subject: [PATCH] move subscription latest to separate property --- dist/bundle.es.js | 22 +++++++++++++------- dist/bundle.js | 31 ++++++++++++++++------------- src/redux/actions/subscriptions.js | 5 +++-- src/redux/reducers/subscriptions.js | 20 ++++++++++--------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index d8b4ede..82c420d 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -498,6 +498,7 @@ const handleActions = (actionMap, defaultState) => (state = defaultState, action const defaultState = { enabledChannelNotifications: [], subscriptions: [], + latest: {}, unread: {}, suggested: {}, loading: false, @@ -533,11 +534,17 @@ var subscriptions = handleActions({ subscriptions: newSubscriptions }; }, - [SET_SUBSCRIPTION_LATEST]: (state, action) => ({ ...state, - subscriptions: state.subscriptions.map(subscription => subscription.channelName === action.data.subscription.channelName ? { ...subscription, - latest: action.data.uri - } : subscription) - }), + [SET_SUBSCRIPTION_LATEST]: (state, action) => { + const { + subscription, + uri + } = action.data; + const newLatest = Object.assign({}, state.latest); + newLatest[subscription.uri] = uri; + return { ...state, + latest: newLatest + }; + }, [UPDATE_SUBSCRIPTION_UNREADS]: (state, action) => { const { channel, @@ -1702,6 +1709,7 @@ const doCheckSubscription = (subscriptionUri, shouldNotify) => (dispatch, getSta const state = getState(); const savedSubscription = state.subscriptions.subscriptions.find(sub => sub.uri === subscriptionUri); + const subscriptionLatest = state.subscriptions.latest[subscriptionUri]; if (!savedSubscription) { throw Error(`Trying to find new content for ${subscriptionUri} but it doesn't exist in your subscriptions`); @@ -1724,12 +1732,12 @@ const doCheckSubscription = (subscriptionUri, shouldNotify) => (dispatch, getSta } // Determine if the latest subscription currently saved is actually the latest subscription - const latestIndex = claimsInChannel.findIndex(claim => claim.permanent_url === savedSubscription.latest); // If latest is -1, it is a newly subscribed channel or there have been 10+ claims published since last viewed + const latestIndex = claimsInChannel.findIndex(claim => claim.permanent_url === subscriptionLatest); // If latest is -1, it is a newly subscribed channel or there have been 10+ claims published since last viewed const latestIndexToNotify = latestIndex === -1 ? 10 : latestIndex; // If latest is 0, nothing has changed // Do not download/notify about new content, it would download/notify 10 claims per channel - if (latestIndex !== 0 && savedSubscription.latest) { + if (latestIndex !== 0 && subscriptionLatest) { let downloadCount = 0; const newUnread = []; claimsInChannel.slice(0, latestIndexToNotify).forEach(claim => { diff --git a/dist/bundle.js b/dist/bundle.js index a5a85b8..86011ec 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -1224,6 +1224,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope var defaultState = { enabledChannelNotifications: [], subscriptions: [], + latest: {}, unread: {}, suggested: {}, loading: false, @@ -1256,18 +1257,19 @@ var defaultState = { subscriptions: newSubscriptions }); }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["SET_SUBSCRIPTION_LATEST"], function (state, action) { + var _action$data = action.data, + subscription = _action$data.subscription, + uri = _action$data.uri; + var newLatest = Object.assign({}, state.latest); + newLatest[subscription.uri] = uri; return _objectSpread({}, state, { - subscriptions: state.subscriptions.map(function (subscription) { - return subscription.channelName === action.data.subscription.channelName ? _objectSpread({}, subscription, { - latest: action.data.uri - }) : subscription; - }) + latest: newLatest }); }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["UPDATE_SUBSCRIPTION_UNREADS"], function (state, action) { - var _action$data = action.data, - channel = _action$data.channel, - uris = _action$data.uris, - type = _action$data.type; + var _action$data2 = action.data, + channel = _action$data2.channel, + uris = _action$data2.uris, + type = _action$data2.type; return _objectSpread({}, state, { unread: _objectSpread({}, state.unread, _defineProperty({}, channel, { uris: uris, @@ -1275,9 +1277,9 @@ var defaultState = { })) }); }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["REMOVE_SUBSCRIPTION_UNREADS"], function (state, action) { - var _action$data2 = action.data, - channel = _action$data2.channel, - uris = _action$data2.uris; // If no channel is passed in, remove all unreads + var _action$data3 = action.data, + channel = _action$data3.channel, + uris = _action$data3.uris; // If no channel is passed in, remove all unreads var newUnread; @@ -3065,6 +3067,7 @@ var doCheckSubscription = function doCheckSubscription(subscriptionUri, shouldNo var savedSubscription = state.subscriptions.subscriptions.find(function (sub) { return sub.uri === subscriptionUri; }); + var subscriptionLatest = state.subscriptions.latest[subscriptionUri]; if (!savedSubscription) { throw Error("Trying to find new content for ".concat(subscriptionUri, " but it doesn't exist in your subscriptions")); @@ -3086,13 +3089,13 @@ var doCheckSubscription = function doCheckSubscription(subscriptionUri, shouldNo var latestIndex = claimsInChannel.findIndex(function (claim) { - return claim.permanent_url === savedSubscription.latest; + return claim.permanent_url === subscriptionLatest; }); // If latest is -1, it is a newly subscribed channel or there have been 10+ claims published since last viewed var latestIndexToNotify = latestIndex === -1 ? 10 : latestIndex; // If latest is 0, nothing has changed // Do not download/notify about new content, it would download/notify 10 claims per channel - if (latestIndex !== 0 && savedSubscription.latest) { + if (latestIndex !== 0 && subscriptionLatest) { var downloadCount = 0; var newUnread = []; claimsInChannel.slice(0, latestIndexToNotify).forEach(function (claim) { diff --git a/src/redux/actions/subscriptions.js b/src/redux/actions/subscriptions.js index de462b3..d2ab38c 100644 --- a/src/redux/actions/subscriptions.js +++ b/src/redux/actions/subscriptions.js @@ -141,6 +141,7 @@ export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: bool const savedSubscription = state.subscriptions.subscriptions.find( sub => sub.uri === subscriptionUri ); + const subscriptionLatest = state.subscriptions.latest[subscriptionUri]; if (!savedSubscription) { throw Error( @@ -165,7 +166,7 @@ export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: bool // Determine if the latest subscription currently saved is actually the latest subscription const latestIndex = claimsInChannel.findIndex( - claim => claim.permanent_url === savedSubscription.latest + claim => claim.permanent_url === subscriptionLatest ); // If latest is -1, it is a newly subscribed channel or there have been 10+ claims published since last viewed @@ -173,7 +174,7 @@ export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: bool // If latest is 0, nothing has changed // Do not download/notify about new content, it would download/notify 10 claims per channel - if (latestIndex !== 0 && savedSubscription.latest) { + if (latestIndex !== 0 && subscriptionLatest) { let downloadCount = 0; const newUnread = []; diff --git a/src/redux/reducers/subscriptions.js b/src/redux/reducers/subscriptions.js index ec7c5f3..6dc1f14 100644 --- a/src/redux/reducers/subscriptions.js +++ b/src/redux/reducers/subscriptions.js @@ -7,6 +7,7 @@ import { handleActions } from 'util/redux-utils'; const defaultState: SubscriptionState = { enabledChannelNotifications: [], subscriptions: [], + latest: {}, unread: {}, suggested: {}, loading: false, @@ -54,15 +55,16 @@ export default handleActions( [ACTIONS.SET_SUBSCRIPTION_LATEST]: ( state: SubscriptionState, action: SetSubscriptionLatest - ): SubscriptionState => ({ - ...state, - subscriptions: state.subscriptions.map( - subscription => - subscription.channelName === action.data.subscription.channelName - ? { ...subscription, latest: action.data.uri } - : subscription - ), - }), + ): SubscriptionState => { + const { subscription, uri } = action.data; + const newLatest = Object.assign({}, state.latest); + newLatest[subscription.uri] = uri; + + return { + ...state, + latest: newLatest, + }; + }, [ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: ( state: SubscriptionState, action: DoUpdateSubscriptionUnreads