Merge pull request #61 from lbryio/latest-subscription

move subscription latest to separate property
This commit is contained in:
Akinwale Ariwodola 2019-09-30 21:20:30 +01:00 committed by GitHub
commit d99232ebc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 32 deletions

22
dist/bundle.es.js vendored
View file

@ -504,6 +504,7 @@ const handleActions = (actionMap, defaultState) => (state = defaultState, action
const defaultState = { const defaultState = {
enabledChannelNotifications: [], enabledChannelNotifications: [],
subscriptions: [], subscriptions: [],
latest: {},
unread: {}, unread: {},
suggested: {}, suggested: {},
loading: false, loading: false,
@ -539,11 +540,17 @@ var subscriptions = handleActions({
subscriptions: newSubscriptions subscriptions: newSubscriptions
}; };
}, },
[SET_SUBSCRIPTION_LATEST]: (state, action) => ({ ...state, [SET_SUBSCRIPTION_LATEST]: (state, action) => {
subscriptions: state.subscriptions.map(subscription => subscription.channelName === action.data.subscription.channelName ? { ...subscription, const {
latest: action.data.uri subscription,
} : subscription) uri
}), } = action.data;
const newLatest = Object.assign({}, state.latest);
newLatest[subscription.uri] = uri;
return { ...state,
latest: newLatest
};
},
[UPDATE_SUBSCRIPTION_UNREADS]: (state, action) => { [UPDATE_SUBSCRIPTION_UNREADS]: (state, action) => {
const { const {
channel, channel,
@ -1708,6 +1715,7 @@ const doCheckSubscription = (subscriptionUri, shouldNotify) => (dispatch, getSta
const state = getState(); const state = getState();
const savedSubscription = state.subscriptions.subscriptions.find(sub => sub.uri === subscriptionUri); const savedSubscription = state.subscriptions.subscriptions.find(sub => sub.uri === subscriptionUri);
const subscriptionLatest = state.subscriptions.latest[subscriptionUri];
if (!savedSubscription) { if (!savedSubscription) {
throw Error(`Trying to find new content for ${subscriptionUri} but it doesn't exist in your subscriptions`); throw Error(`Trying to find new content for ${subscriptionUri} but it doesn't exist in your subscriptions`);
@ -1730,12 +1738,12 @@ const doCheckSubscription = (subscriptionUri, shouldNotify) => (dispatch, getSta
} // Determine if the latest subscription currently saved is actually the latest subscription } // 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 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 // 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; let downloadCount = 0;
const newUnread = []; const newUnread = [];
claimsInChannel.slice(0, latestIndexToNotify).forEach(claim => { claimsInChannel.slice(0, latestIndexToNotify).forEach(claim => {

31
dist/bundle.js vendored
View file

@ -1234,6 +1234,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
var defaultState = { var defaultState = {
enabledChannelNotifications: [], enabledChannelNotifications: [],
subscriptions: [], subscriptions: [],
latest: {},
unread: {}, unread: {},
suggested: {}, suggested: {},
loading: false, loading: false,
@ -1266,18 +1267,19 @@ var defaultState = {
subscriptions: newSubscriptions subscriptions: newSubscriptions
}); });
}), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["SET_SUBSCRIPTION_LATEST"], function (state, action) { }), _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, { return _objectSpread({}, state, {
subscriptions: state.subscriptions.map(function (subscription) { latest: newLatest
return subscription.channelName === action.data.subscription.channelName ? _objectSpread({}, subscription, {
latest: action.data.uri
}) : subscription;
})
}); });
}), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["UPDATE_SUBSCRIPTION_UNREADS"], function (state, action) { }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["UPDATE_SUBSCRIPTION_UNREADS"], function (state, action) {
var _action$data = action.data, var _action$data2 = action.data,
channel = _action$data.channel, channel = _action$data2.channel,
uris = _action$data.uris, uris = _action$data2.uris,
type = _action$data.type; type = _action$data2.type;
return _objectSpread({}, state, { return _objectSpread({}, state, {
unread: _objectSpread({}, state.unread, _defineProperty({}, channel, { unread: _objectSpread({}, state.unread, _defineProperty({}, channel, {
uris: uris, uris: uris,
@ -1285,9 +1287,9 @@ var defaultState = {
})) }))
}); });
}), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["REMOVE_SUBSCRIPTION_UNREADS"], function (state, action) { }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["REMOVE_SUBSCRIPTION_UNREADS"], function (state, action) {
var _action$data2 = action.data, var _action$data3 = action.data,
channel = _action$data2.channel, channel = _action$data3.channel,
uris = _action$data2.uris; // If no channel is passed in, remove all unreads uris = _action$data3.uris; // If no channel is passed in, remove all unreads
var newUnread; var newUnread;
@ -3075,6 +3077,7 @@ var doCheckSubscription = function doCheckSubscription(subscriptionUri, shouldNo
var savedSubscription = state.subscriptions.subscriptions.find(function (sub) { var savedSubscription = state.subscriptions.subscriptions.find(function (sub) {
return sub.uri === subscriptionUri; return sub.uri === subscriptionUri;
}); });
var subscriptionLatest = state.subscriptions.latest[subscriptionUri];
if (!savedSubscription) { if (!savedSubscription) {
throw Error("Trying to find new content for ".concat(subscriptionUri, " but it doesn't exist in your subscriptions")); throw Error("Trying to find new content for ".concat(subscriptionUri, " but it doesn't exist in your subscriptions"));
@ -3096,13 +3099,13 @@ var doCheckSubscription = function doCheckSubscription(subscriptionUri, shouldNo
var latestIndex = claimsInChannel.findIndex(function (claim) { 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 }); // 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 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 // 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 downloadCount = 0;
var newUnread = []; var newUnread = [];
claimsInChannel.slice(0, latestIndexToNotify).forEach(function (claim) { claimsInChannel.slice(0, latestIndexToNotify).forEach(function (claim) {

View file

@ -141,6 +141,7 @@ export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: bool
const savedSubscription = state.subscriptions.subscriptions.find( const savedSubscription = state.subscriptions.subscriptions.find(
sub => sub.uri === subscriptionUri sub => sub.uri === subscriptionUri
); );
const subscriptionLatest = state.subscriptions.latest[subscriptionUri];
if (!savedSubscription) { if (!savedSubscription) {
throw Error( 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 // Determine if the latest subscription currently saved is actually the latest subscription
const latestIndex = claimsInChannel.findIndex( 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 // 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 // If latest is 0, nothing has changed
// Do not download/notify about new content, it would download/notify 10 claims per channel // 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; let downloadCount = 0;
const newUnread = []; const newUnread = [];

View file

@ -7,6 +7,7 @@ import { handleActions } from 'util/redux-utils';
const defaultState: SubscriptionState = { const defaultState: SubscriptionState = {
enabledChannelNotifications: [], enabledChannelNotifications: [],
subscriptions: [], subscriptions: [],
latest: {},
unread: {}, unread: {},
suggested: {}, suggested: {},
loading: false, loading: false,
@ -54,15 +55,16 @@ export default handleActions(
[ACTIONS.SET_SUBSCRIPTION_LATEST]: ( [ACTIONS.SET_SUBSCRIPTION_LATEST]: (
state: SubscriptionState, state: SubscriptionState,
action: SetSubscriptionLatest action: SetSubscriptionLatest
): SubscriptionState => ({ ): SubscriptionState => {
const { subscription, uri } = action.data;
const newLatest = Object.assign({}, state.latest);
newLatest[subscription.uri] = uri;
return {
...state, ...state,
subscriptions: state.subscriptions.map( latest: newLatest,
subscription => };
subscription.channelName === action.data.subscription.channelName },
? { ...subscription, latest: action.data.uri }
: subscription
),
}),
[ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: ( [ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: (
state: SubscriptionState, state: SubscriptionState,
action: DoUpdateSubscriptionUnreads action: DoUpdateSubscriptionUnreads