Speed up getAuthor for message

This commit is contained in:
Fedor Indutny 2021-10-28 15:39:16 -07:00 committed by GitHub
parent 71ee0568c0
commit 9458b399d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -176,13 +176,20 @@ export function getSourceUuid(
return ourUuid; return ourUuid;
} }
export function getContactId( export type GetContactOptions = Pick<
GetPropsForBubbleOptions,
'conversationSelector' | 'ourConversationId' | 'ourNumber' | 'ourUuid'
>;
function getContactId(
message: MessageAttributesType, message: MessageAttributesType,
conversationSelector: GetConversationByIdType, {
ourConversationId: string, conversationSelector,
ourNumber: string | undefined, ourConversationId,
ourUuid: string | undefined ourNumber,
): string | undefined { ourUuid,
}: GetContactOptions
): string {
const source = getSource(message, ourNumber); const source = getSource(message, ourNumber);
const sourceUuid = getSourceUuid(message, ourUuid); const sourceUuid = getSourceUuid(message, ourUuid);
@ -194,11 +201,6 @@ export function getContactId(
return conversation.id; return conversation.id;
} }
export type GetContactOptions = Pick<
GetPropsForBubbleOptions,
'conversationSelector' | 'ourConversationId' | 'ourNumber' | 'ourUuid'
>;
// TODO: DESKTOP-2145 // TODO: DESKTOP-2145
export function getContact( export function getContact(
message: MessageAttributesType, message: MessageAttributesType,
@ -294,36 +296,57 @@ export const processBodyRanges = createSelectorCreator(memoizeByRoot, isEqual)(
(_: MessageAttributesType, ranges?: BodyRangesType) => ranges (_: MessageAttributesType, ranges?: BodyRangesType) => ranges
); );
export const getAuthorForMessage = createSelectorCreator( const getAuthorForMessage = createSelectorCreator(memoizeByRoot)(
memoizeByRoot,
isEqual
)(
// `memoizeByRoot` requirement // `memoizeByRoot` requirement
identity, identity,
( getContact,
message: MessageAttributesType,
options: GetContactOptions (_: MessageAttributesType, convo: ConversationType): PropsData['author'] => {
): PropsData['author'] => { const {
const unsafe = pick(getContact(message, options), [ acceptedMessageRequest,
'acceptedMessageRequest', avatarPath,
'avatarPath', color,
'color', id,
'id', isMe,
'isMe', name,
'name', phoneNumber,
'phoneNumber', profileName,
'profileName', sharedGroupNames,
'sharedGroupNames', title,
'title', unblurredAvatarPath,
'unblurredAvatarPath', } = convo;
]);
const unsafe = {
acceptedMessageRequest,
avatarPath,
color,
id,
isMe,
name,
phoneNumber,
profileName,
sharedGroupNames,
title,
unblurredAvatarPath,
};
const safe: AssertProps<PropsData['author'], typeof unsafe> = unsafe; const safe: AssertProps<PropsData['author'], typeof unsafe> = unsafe;
return safe; return safe;
}, }
(_: MessageAttributesType, author: PropsData['author']) => author );
const getCachedAuthorForMessage = createSelectorCreator(memoizeByRoot, isEqual)(
// `memoizeByRoot` requirement
identity,
getAuthorForMessage,
(
_: MessageAttributesType,
author: PropsData['author']
): PropsData['author'] => author
); );
export const getPreviewsForMessage = createSelectorCreator(memoizeByRoot)( export const getPreviewsForMessage = createSelectorCreator(memoizeByRoot)(
@ -538,16 +561,13 @@ const getShallowPropsForMessage = createSelectorCreator(memoizeByRoot, isEqual)(
{} {}
).emoji; ).emoji;
const author = getContact(message, { const authorId = getContactId(message, {
conversationSelector, conversationSelector,
ourConversationId, ourConversationId,
ourNumber, ourNumber,
ourUuid, ourUuid,
}); });
const contactNameColor = contactNameColorSelector( const contactNameColor = contactNameColorSelector(conversationId, authorId);
conversationId,
author.id
);
const defaultConversationColor = window.Events.getDefaultConversationColor(); const defaultConversationColor = window.Events.getDefaultConversationColor();
@ -596,7 +616,7 @@ export const getPropsForMessage = createSelectorCreator(memoizeByRoot)(
getAttachmentsForMessage, getAttachmentsForMessage,
processBodyRanges, processBodyRanges,
getAuthorForMessage, getCachedAuthorForMessage,
getPreviewsForMessage, getPreviewsForMessage,
getReactionsForMessage, getReactionsForMessage,
getPropsForQuote, getPropsForQuote,