From 121b0f0cd6738ae93525320deb3a6527b125efb5 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Tue, 9 Nov 2021 07:02:41 +0800 Subject: [PATCH] ~~ Revert to allow restoring to commits ~~ This reverts commit c97cab0ebb292b90f2188978d413ad0a14c8fccc. --- ui/redux/reducers/claims.js | 104 ++++++----------------------------- ui/redux/selectors/app.js | 10 ++-- ui/redux/selectors/claims.js | 37 ++----------- 3 files changed, 28 insertions(+), 123 deletions(-) diff --git a/ui/redux/reducers/claims.js b/ui/redux/reducers/claims.js index af6a5032d..1c321d351 100644 --- a/ui/redux/reducers/claims.js +++ b/ui/redux/reducers/claims.js @@ -111,63 +111,11 @@ const defaultState = { checkingReflecting: false, }; -// **************************************************************************** -// Helpers -// **************************************************************************** - -function isObjEmpty(object: any) { - return Object.keys(object).length === 0; -} - -function resolveDelta(original: any, delta: any) { - if (isObjEmpty(delta)) { - // Don't invalidate references when there are no changes, so return original. - return original; - } else { - // When there are changes: create a new object, spread existing references, - // and overwrite specific items with new data. - return { ...original, ...delta }; - } -} - -function claimObjHasNewData(original, fresh) { - // Don't blow away 'is_my_output' just because the next query didn't ask for it. - const ignoreIsMyOutput = original.is_my_output !== undefined && fresh.is_my_output === undefined; - - // Something is causing the tags to be re-ordered differently - // (https://github.com/OdyseeTeam/odysee-frontend/issues/116#issuecomment-962747147). - // Just do a length comparison for now, which covers 99% of cases while we - // figure out what's causing the order to change. - const ignoreTags = - original.value && - fresh.value && - original.value.tags && - fresh.value.tags && - original.value.tags.length !== fresh.value.tags.length; - - const excludeKeys = (key, value) => { - if (key === 'confirmations' || (ignoreTags && key === 'tags') || (ignoreIsMyOutput && key === 'is_my_output')) { - return undefined; - } - - return value; - }; - - const originalStringified = JSON.stringify(original, excludeKeys); - const freshStringified = JSON.stringify(fresh, excludeKeys); - - return originalStringified !== freshStringified; -} - -// **************************************************************************** -// handleClaimAction -// **************************************************************************** - function handleClaimAction(state: State, action: any): State { const { resolveInfo }: ClaimActionResolveInfo = action.data; const byUri = Object.assign({}, state.claimsByUri); - const byIdDelta = {}; + const byId = Object.assign({}, state.byId); const channelClaimCounts = Object.assign({}, state.channelClaimCounts); const pendingById = state.pendingById; let newResolvingUrls = new Set(state.resolvingUris); @@ -180,13 +128,10 @@ function handleClaimAction(state: State, action: any): State { if (stream) { if (pendingById[stream.claim_id]) { - byIdDelta[stream.claim_id] = mergeClaim(stream, state.byId[stream.claim_id]); + byId[stream.claim_id] = mergeClaim(stream, byId[stream.claim_id]); } else { - if (!state.byId[stream.claim_id] || claimObjHasNewData(state.byId[stream.claim_id], stream)) { - byIdDelta[stream.claim_id] = stream; - } + byId[stream.claim_id] = stream; } - byUri[url] = stream.claim_id; // If url isn't a canonical_url, make sure that is added too @@ -213,9 +158,9 @@ function handleClaimAction(state: State, action: any): State { } if (pendingById[channel.claim_id]) { - byIdDelta[channel.claim_id] = mergeClaim(channel, state.byId[channel.claim_id]); + byId[channel.claim_id] = mergeClaim(channel, byId[channel.claim_id]); } else { - byIdDelta[channel.claim_id] = channel; + byId[channel.claim_id] = channel; } byUri[channel.permanent_url] = channel.claim_id; @@ -226,11 +171,10 @@ function handleClaimAction(state: State, action: any): State { if (collection) { if (pendingById[collection.claim_id]) { - byIdDelta[collection.claim_id] = mergeClaim(collection, state.byId[collection.claim_id]); + byId[collection.claim_id] = mergeClaim(collection, byId[collection.claim_id]); } else { - byIdDelta[collection.claim_id] = collection; + byId[collection.claim_id] = collection; } - byUri[url] = collection.claim_id; byUri[collection.canonical_url] = collection.claim_id; byUri[collection.permanent_url] = collection.claim_id; @@ -249,7 +193,7 @@ function handleClaimAction(state: State, action: any): State { }); return Object.assign({}, state, { - byId: resolveDelta(state.byId, byIdDelta), + byId, claimsByUri: byUri, channelClaimCounts, resolvingUris: Array.from(newResolvingUrls), @@ -257,10 +201,6 @@ function handleClaimAction(state: State, action: any): State { }); } -// **************************************************************************** -// Reducers -// **************************************************************************** - reducers[ACTIONS.RESOLVE_URIS_STARTED] = (state: State, action: any): State => { const { uris }: { uris: Array } = action.data; @@ -297,8 +237,7 @@ reducers[ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED] = (state: State, action: any): const byId = Object.assign({}, state.byId); const byUri = Object.assign({}, state.claimsByUri); - const pendingByIdDelta = {}; - + const pendingById = Object.assign({}, state.pendingById); let myClaimIds = new Set(state.myClaims); let urlsForCurrentPage = []; @@ -306,10 +245,8 @@ reducers[ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED] = (state: State, action: any): const { permanent_url: permanentUri, claim_id: claimId, canonical_url: canonicalUri } = claim; if (claim.type && claim.type.match(/claim|update/)) { urlsForCurrentPage.push(permanentUri); - if (claim.confirmations < 1) { - pendingByIdDelta[claimId] = claim; - + pendingById[claimId] = claim; if (byId[claimId]) { byId[claimId] = mergeClaim(claim, byId[claimId]); } else { @@ -318,7 +255,6 @@ reducers[ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED] = (state: State, action: any): } else { byId[claimId] = claim; } - byUri[permanentUri] = claimId; byUri[canonicalUri] = claimId; myClaimIds.add(claimId); @@ -329,7 +265,7 @@ reducers[ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED] = (state: State, action: any): isFetchingClaimListMine: false, myClaims: Array.from(myClaimIds), byId, - pendingById: resolveDelta(state.pendingById, pendingByIdDelta), + pendingById, claimsByUri: byUri, myClaimsPageResults: urlsForCurrentPage, myClaimsPageNumber: page, @@ -343,7 +279,7 @@ reducers[ACTIONS.FETCH_CHANNEL_LIST_STARTED] = (state: State): State => reducers[ACTIONS.FETCH_CHANNEL_LIST_COMPLETED] = (state: State, action: any): State => { const { claims }: { claims: Array } = action.data; let myClaimIds = new Set(state.myClaims); - const pendingByIdDelta = {}; + const pendingById = Object.assign({}, state.pendingById); let myChannelClaims; const byId = Object.assign({}, state.byId); const byUri = Object.assign({}, state.claimsByUri); @@ -366,10 +302,8 @@ reducers[ACTIONS.FETCH_CHANNEL_LIST_COMPLETED] = (state: State, action: any): St // $FlowFixMe myChannelClaims.add(claimId); - if (confirmations < 1) { - pendingByIdDelta[claimId] = claim; - + pendingById[claimId] = claim; if (byId[claimId]) { byId[claimId] = mergeClaim(claim, byId[claimId]); } else { @@ -378,14 +312,13 @@ reducers[ACTIONS.FETCH_CHANNEL_LIST_COMPLETED] = (state: State, action: any): St } else { byId[claimId] = claim; } - myClaimIds.add(claimId); }); } return Object.assign({}, state, { byId, - pendingById: resolveDelta(state.pendingById, pendingByIdDelta), + pendingById, claimsByUri: byUri, channelClaimCounts, fetchingMyChannels: false, @@ -409,7 +342,7 @@ reducers[ACTIONS.FETCH_COLLECTION_LIST_COMPLETED] = (state: State, action: any): const { claims }: { claims: Array } = action.data; const myClaims = state.myClaims || []; let myClaimIds = new Set(myClaims); - const pendingByIdDelta = {}; + const pendingById = Object.assign({}, state.pendingById); let myCollectionClaimsSet = new Set([]); const byId = Object.assign({}, state.byId); const byUri = Object.assign({}, state.claimsByUri); @@ -424,11 +357,9 @@ reducers[ACTIONS.FETCH_COLLECTION_LIST_COMPLETED] = (state: State, action: any): // $FlowFixMe myCollectionClaimsSet.add(claimId); - // we don't want to overwrite a pending result with a resolve if (confirmations < 1) { - pendingByIdDelta[claimId] = claim; - + pendingById[claimId] = claim; if (byId[claimId]) { byId[claimId] = mergeClaim(claim, byId[claimId]); } else { @@ -437,7 +368,6 @@ reducers[ACTIONS.FETCH_COLLECTION_LIST_COMPLETED] = (state: State, action: any): } else { byId[claimId] = claim; } - myClaimIds.add(claimId); }); } @@ -445,7 +375,7 @@ reducers[ACTIONS.FETCH_COLLECTION_LIST_COMPLETED] = (state: State, action: any): return { ...state, byId, - pendingById: resolveDelta(state.pendingById, pendingByIdDelta), + pendingById, claimsByUri: byUri, fetchingMyCollections: false, myCollectionClaims: Array.from(myCollectionClaimsSet), diff --git a/ui/redux/selectors/app.js b/ui/redux/selectors/app.js index 37f6786ba..923556e72 100644 --- a/ui/redux/selectors/app.js +++ b/ui/redux/selectors/app.js @@ -1,5 +1,5 @@ import { createSelector } from 'reselect'; -import { selectClaimWithId, selectMyChannelClaims, makeSelectStakedLevelForChannelUri } from 'redux/selectors/claims'; +import { selectClaimsById, selectMyChannelClaims, makeSelectStakedLevelForChannelUri } from 'redux/selectors/claims'; export const selectState = (state) => state.app || {}; @@ -70,13 +70,15 @@ export const selectSplashAnimationEnabled = (state) => selectState(state).splash export const selectActiveChannelId = (state) => selectState(state).activeChannel; export const selectActiveChannelClaim = createSelector( - (state) => selectClaimWithId(state, selectActiveChannelId(state)), // i.e. 'byId[activeChannelId]' specifically, instead of just 'byId'. + selectActiveChannelId, + selectClaimsById, selectMyChannelClaims, - (activeChannelClaim, myChannelClaims) => { - if (!activeChannelClaim || !myChannelClaims || !myChannelClaims.length) { + (activeChannelClaimId, claimsById, myChannelClaims) => { + if (!activeChannelClaimId || !claimsById || !myChannelClaims || !myChannelClaims.length) { return undefined; } + const activeChannelClaim = claimsById[activeChannelClaimId]; if (activeChannelClaim) { return activeChannelClaim; } diff --git a/ui/redux/selectors/claims.js b/ui/redux/selectors/claims.js index 1ee07dd04..326e221a8 100644 --- a/ui/redux/selectors/claims.js +++ b/ui/redux/selectors/claims.js @@ -43,21 +43,6 @@ export const selectClaimsByUri = createSelector(selectClaimIdsByUri, selectClaim return claims; }); -/** - * Returns the claim with the specified ID. The claim could be undefined if does - * not exist or have not fetched. Take note of the second parameter, which means - * an inline function or helper would be required when used as an input to - * 'createSelector'. - * - * @param state - * @param claimId - * @returns {*} - */ -export const selectClaimWithId = (state: State, claimId: string) => { - const byId = selectClaimsById(state); - return byId[claimId]; -}; - export const selectAllClaimsByChannel = createSelector(selectState, (state) => state.paginatedClaimsByChannel || {}); export const selectPendingIds = createSelector(selectState, (state) => Object.keys(state.pendingById) || []); @@ -419,26 +404,14 @@ export const selectFetchingMyCollections = (state: State) => selectState(state). export const selectMyChannelClaimIds = (state: State) => selectState(state).myChannelClaims; -export const selectMyChannelClaims = createSelector(selectMyChannelClaimIds, (myChannelClaimIds) => { - if (!myChannelClaimIds) { - return myChannelClaimIds; +export const selectMyChannelClaims = createSelector(selectState, selectClaimsById, (state, byId) => { + const ids = state.myChannelClaims; + if (!ids) { + return ids; } - if (!window || !window.store) { - return undefined; - } - - // Note: Grabbing the store and running the selector this way is anti-pattern, - // but it is _needed_ and works only because we know for sure that 'byId[]' - // will be populated with the same claims as when 'myChannelClaimIds' is populated. - // If we put 'state' or 'byId' as the input selector, it essentially - // recalculates every time. Putting 'state' as input to createSelector() is - // always wrong from a memoization standpoint. - const state = window.store.getState(); - const byId = selectClaimsById(state); - const claims = []; - myChannelClaimIds.forEach((id) => { + ids.forEach((id) => { if (byId[id]) { // I'm not sure why this check is necessary, but it ought to be a quick fix for https://github.com/lbryio/lbry-desktop/issues/544 claims.push(byId[id]);