[Perf] Avoid redundant updates of chat and user usernames field

This commit is contained in:
Alexander Zinchuk 2023-01-22 18:12:29 +01:00
parent 9080082823
commit c34ef129ec
2 changed files with 27 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import {
} from '../../util/iteratees';
import { selectChat, selectChatListType } from '../selectors';
import { updateThread, updateThreadInfo } from './messages';
import { areDeepEqual } from '../../util/areDeepEqual';
export function replaceChatListIds(
global: GlobalState,
@ -125,15 +126,24 @@ function getUpdatedChat(
) {
const { byId } = global.chats;
const chat = byId[chatId];
const shouldOmitMinInfo = chatUpdate.isMin && chat && !chat.isMin;
const omitProps: (keyof ApiChat)[] = [];
const shouldOmitMinInfo = chatUpdate.isMin && chat && !chat.isMin;
if (shouldOmitMinInfo) {
omitProps.push('isMin', 'accessHash');
}
if (!noOmitUnreadReactionCount) {
omitProps.push('unreadReactionsCount');
}
if (areDeepEqual(chat?.usernames, chatUpdate.usernames)) {
omitProps.push('usernames');
}
chatUpdate = noOmitUnreadReactionCount
? chatUpdate : omit(chatUpdate, ['unreadReactionsCount']);
const updatedChat: ApiChat = {
...chat,
...(shouldOmitMinInfo
? omit(chatUpdate, ['isMin', 'accessHash'])
: chatUpdate),
...omit(chatUpdate, omitProps),
...(photo && { photos: [photo, ...(chat.photos || [])] }),
};

View File

@ -4,6 +4,7 @@ import type { ApiUser, ApiUserStatus } from '../../api/types';
import { omit, pick } from '../../util/iteratees';
import { MEMO_EMPTY_ARRAY } from '../../util/memo';
import { updateChat } from './chats';
import { areDeepEqual } from '../../util/areDeepEqual';
export function replaceUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
return {
@ -109,11 +110,20 @@ export function addUsers(global: GlobalState, newById: Record<string, ApiUser>):
function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial<ApiUser>) {
const { byId } = global.users;
const user = byId[userId];
const omitProps: (keyof ApiUser)[] = [];
const shouldOmitMinInfo = userUpdate.isMin && user && !user.isMin;
if (shouldOmitMinInfo) {
omitProps.push('isMin', 'accessHash');
}
if (areDeepEqual(user?.usernames, userUpdate.usernames)) {
omitProps.push('usernames');
}
const updatedUser = {
...user,
...(shouldOmitMinInfo ? omit(userUpdate, ['isMin', 'accessHash']) : userUpdate),
...omit(userUpdate, omitProps),
};
if (!updatedUser.id || !updatedUser.type) {