[Perf] Optimize updateCache

This commit is contained in:
Alexander Zinchuk 2021-12-04 13:44:23 +01:00
parent e281dd93b5
commit 652be2823c

View File

@ -256,10 +256,9 @@ function reduceShowChatInfo(global: GlobalState): boolean {
function reduceUsers(global: GlobalState): GlobalState['users'] { function reduceUsers(global: GlobalState): GlobalState['users'] {
const { users: { byId, statusesById, selectedId } } = global; const { users: { byId, statusesById, selectedId } } = global;
const idsToSave = [ const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT).filter(isUserId);
...(global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT).filter(isUserId), const userIds = Object.keys(byId);
...Object.keys(byId), const idsToSave = chatIds.concat(userIds).slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT);
].slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT);
return { return {
byId: pick(byId, idsToSave), byId: pick(byId, idsToSave),
@ -269,16 +268,15 @@ function reduceUsers(global: GlobalState): GlobalState['users'] {
} }
function reduceChats(global: GlobalState): GlobalState['chats'] { function reduceChats(global: GlobalState): GlobalState['chats'] {
const chatIdsToSave = [ const newListIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT);
...(global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT),
];
const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; const { chatId: currentChatId } = selectCurrentMessageList(global) || {};
const idsToSave = newListIds.concat(currentChatId ? [currentChatId] : []);
return { return {
...global.chats, ...global.chats,
byId: pick(global.chats.byId, currentChatId ? [...chatIdsToSave, currentChatId] : chatIdsToSave), byId: pick(global.chats.byId, idsToSave),
listIds: { listIds: {
active: chatIdsToSave, active: newListIds,
}, },
isFullyLoaded: {}, isFullyLoaded: {},
orderedPinnedIds: { orderedPinnedIds: {
@ -291,10 +289,9 @@ function reduceMessages(global: GlobalState): GlobalState['messages'] {
const byChatId: GlobalState['messages']['byChatId'] = {}; const byChatId: GlobalState['messages']['byChatId'] = {};
const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; const { chatId: currentChatId } = selectCurrentMessageList(global) || {};
const chatIdsToSave = [ const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT);
...(global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT), const chatIdsToSave = chatIds.concat(currentChatId ? [currentChatId] : []);
...(currentChatId ? [currentChatId] : []),
];
chatIdsToSave.forEach((chatId) => { chatIdsToSave.forEach((chatId) => {
const current = global.messages.byChatId[chatId]; const current = global.messages.byChatId[chatId];
if (!current) { if (!current) {