lbry-desktop/ui/redux/actions/reactions.js
jessopb a3398843c2
Optimize selectClaimIsMine (#7370)
Frequently used; top in perf profile

Most of the time, you already have the claim object in the current context. `selectClaimIsMineForUri` will retrieve the claim again, which is wasteful, even if it is memoized (looking up the cache still takes time).

Break apart the logic and added the alternative `selectClaimIsMine` for faster lookup.

Co-authored-by: infinite-persistence <inf.persistence@gmail.com>
2021-12-31 12:52:26 -05:00

70 lines
2.2 KiB
JavaScript

// @flow
import { Lbryio } from 'lbryinc';
import * as ACTIONS from 'constants/action_types';
import * as REACTION_TYPES from 'constants/reactions';
import { makeSelectMyReactionForUri } from 'redux/selectors/reactions';
import { selectClaimForUri } from 'redux/selectors/claims';
export const doFetchReactions = (claimId: string) => (dispatch: Dispatch) => {
dispatch({ type: ACTIONS.REACTIONS_LIST_STARTED });
return Lbryio.call('reaction', 'list', { claim_ids: claimId }, 'post')
.then((reactions: Array<number>) => {
dispatch({ type: ACTIONS.REACTIONS_LIST_COMPLETED, data: { claimId, reactions } });
})
.catch((error) => {
dispatch({ type: ACTIONS.REACTIONS_LIST_FAILED, data: error });
});
};
export const doReactionLike = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myReaction = makeSelectMyReactionForUri(uri)(state);
const claim = selectClaimForUri(state, uri);
const claimId = claim.claim_id;
const shouldRemove = myReaction === REACTION_TYPES.LIKE;
return Lbryio.call(
'reaction',
'react',
{
claim_ids: claimId,
type: REACTION_TYPES.LIKE,
clear_types: REACTION_TYPES.DISLIKE,
...(shouldRemove ? { remove: true } : {}),
},
'post'
)
.then(() => {
dispatch({ type: ACTIONS.REACTIONS_LIKE_COMPLETED, data: { claimId, shouldRemove } });
})
.catch((error) => {
dispatch({ type: ACTIONS.REACTIONS_NEW_FAILED, data: error });
});
};
export const doReactionDislike = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myReaction = makeSelectMyReactionForUri(uri)(state);
const claim = selectClaimForUri(state, uri);
const claimId = claim.claim_id;
const shouldRemove = myReaction === REACTION_TYPES.DISLIKE;
return Lbryio.call(
'reaction',
'react',
{
claim_ids: claimId,
type: REACTION_TYPES.DISLIKE,
clear_types: REACTION_TYPES.LIKE,
...(shouldRemove ? { remove: true } : {}),
},
'post'
)
.then(() => {
dispatch({ type: ACTIONS.REACTIONS_DISLIKE_COMPLETED, data: { claimId, shouldRemove } });
})
.catch((error) => {
dispatch({ type: ACTIONS.REACTIONS_NEW_FAILED, data: error });
});
};