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