[Perf] Avoid redundant updates of chat and user usernames field
This commit is contained in:
parent
9080082823
commit
c34ef129ec
@ -9,6 +9,7 @@ import {
|
|||||||
} from '../../util/iteratees';
|
} from '../../util/iteratees';
|
||||||
import { selectChat, selectChatListType } from '../selectors';
|
import { selectChat, selectChatListType } from '../selectors';
|
||||||
import { updateThread, updateThreadInfo } from './messages';
|
import { updateThread, updateThreadInfo } from './messages';
|
||||||
|
import { areDeepEqual } from '../../util/areDeepEqual';
|
||||||
|
|
||||||
export function replaceChatListIds(
|
export function replaceChatListIds(
|
||||||
global: GlobalState,
|
global: GlobalState,
|
||||||
@ -125,15 +126,24 @@ function getUpdatedChat(
|
|||||||
) {
|
) {
|
||||||
const { byId } = global.chats;
|
const { byId } = global.chats;
|
||||||
const chat = byId[chatId];
|
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 = {
|
const updatedChat: ApiChat = {
|
||||||
...chat,
|
...chat,
|
||||||
...(shouldOmitMinInfo
|
...omit(chatUpdate, omitProps),
|
||||||
? omit(chatUpdate, ['isMin', 'accessHash'])
|
|
||||||
: chatUpdate),
|
|
||||||
...(photo && { photos: [photo, ...(chat.photos || [])] }),
|
...(photo && { photos: [photo, ...(chat.photos || [])] }),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import type { ApiUser, ApiUserStatus } from '../../api/types';
|
|||||||
import { omit, pick } from '../../util/iteratees';
|
import { omit, pick } from '../../util/iteratees';
|
||||||
import { MEMO_EMPTY_ARRAY } from '../../util/memo';
|
import { MEMO_EMPTY_ARRAY } from '../../util/memo';
|
||||||
import { updateChat } from './chats';
|
import { updateChat } from './chats';
|
||||||
|
import { areDeepEqual } from '../../util/areDeepEqual';
|
||||||
|
|
||||||
export function replaceUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
|
export function replaceUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
|
||||||
return {
|
return {
|
||||||
@ -109,11 +110,20 @@ export function addUsers(global: GlobalState, newById: Record<string, ApiUser>):
|
|||||||
function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial<ApiUser>) {
|
function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial<ApiUser>) {
|
||||||
const { byId } = global.users;
|
const { byId } = global.users;
|
||||||
const user = byId[userId];
|
const user = byId[userId];
|
||||||
|
const omitProps: (keyof ApiUser)[] = [];
|
||||||
|
|
||||||
const shouldOmitMinInfo = userUpdate.isMin && user && !user.isMin;
|
const shouldOmitMinInfo = userUpdate.isMin && user && !user.isMin;
|
||||||
|
if (shouldOmitMinInfo) {
|
||||||
|
omitProps.push('isMin', 'accessHash');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (areDeepEqual(user?.usernames, userUpdate.usernames)) {
|
||||||
|
omitProps.push('usernames');
|
||||||
|
}
|
||||||
|
|
||||||
const updatedUser = {
|
const updatedUser = {
|
||||||
...user,
|
...user,
|
||||||
...(shouldOmitMinInfo ? omit(userUpdate, ['isMin', 'accessHash']) : userUpdate),
|
...omit(userUpdate, omitProps),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!updatedUser.id || !updatedUser.type) {
|
if (!updatedUser.id || !updatedUser.type) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user