Skip to content

Commit

Permalink
Speed up getAuthor for message
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Oct 28, 2021
1 parent 71ee056 commit 9458b39
Showing 1 changed file with 60 additions and 40 deletions.
100 changes: 60 additions & 40 deletions ts/state/selectors/message.ts
Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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)(
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -596,7 +616,7 @@ export const getPropsForMessage = createSelectorCreator(memoizeByRoot)(

getAttachmentsForMessage,
processBodyRanges,
getAuthorForMessage,
getCachedAuthorForMessage,
getPreviewsForMessage,
getReactionsForMessage,
getPropsForQuote,
Expand Down

0 comments on commit 9458b39

Please sign in to comment.