Cache: Save more chats and less users
This commit is contained in:
parent
f5a019e02a
commit
94cdf97b0b
@ -20,8 +20,9 @@ export const LEGACY_SESSION_KEY = 'GramJs:sessionId';
|
||||
|
||||
export const GLOBAL_STATE_CACHE_DISABLED = false;
|
||||
export const GLOBAL_STATE_CACHE_KEY = 'tt-global-state';
|
||||
export const GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT = 30;
|
||||
export const GLOBAL_STATE_CACHE_USER_LIST_LIMIT = 5000;
|
||||
export const GLOBAL_STATE_CACHE_USER_LIST_LIMIT = 500;
|
||||
export const GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT = 200;
|
||||
export const GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT = 30;
|
||||
|
||||
export const MEDIA_CACHE_DISABLED = false;
|
||||
export const MEDIA_CACHE_NAME = 'tt-media';
|
||||
|
||||
@ -10,21 +10,29 @@ import {
|
||||
DEBUG,
|
||||
GLOBAL_STATE_CACHE_DISABLED,
|
||||
GLOBAL_STATE_CACHE_KEY,
|
||||
GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT,
|
||||
MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN,
|
||||
GLOBAL_STATE_CACHE_USER_LIST_LIMIT,
|
||||
GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT,
|
||||
GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT,
|
||||
MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN,
|
||||
DEFAULT_VOLUME,
|
||||
DEFAULT_PLAYBACK_RATE,
|
||||
ALL_FOLDER_ID,
|
||||
ARCHIVED_FOLDER_ID,
|
||||
} from '../config';
|
||||
import { IS_SINGLE_COLUMN_LAYOUT } from '../util/environment';
|
||||
import { isHeavyAnimating } from '../hooks/useHeavyAnimationCheck';
|
||||
import { pick } from '../util/iteratees';
|
||||
import { selectCurrentMessageList } from '../modules/selectors';
|
||||
import { pick, unique } from '../util/iteratees';
|
||||
import {
|
||||
selectCurrentChat,
|
||||
selectCurrentMessageList,
|
||||
selectVisibleUsers,
|
||||
} from '../modules/selectors';
|
||||
import { hasStoredSession } from '../util/sessions';
|
||||
import { INITIAL_STATE } from './initial';
|
||||
import { parseLocationHash } from '../util/routing';
|
||||
import { LOCATION_HASH } from '../hooks/useHistoryBack';
|
||||
import { isUserId } from '../modules/helpers';
|
||||
import { getOrderedIds } from '../util/folderManager';
|
||||
|
||||
const UPDATE_THROTTLE = 5000;
|
||||
|
||||
@ -263,10 +271,21 @@ function reduceShowChatInfo(global: GlobalState): boolean {
|
||||
}
|
||||
|
||||
function reduceUsers(global: GlobalState): GlobalState['users'] {
|
||||
const { users: { byId, statusesById } } = global;
|
||||
const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT).filter(isUserId);
|
||||
const userIds = Object.keys(byId);
|
||||
const idsToSave = chatIds.concat(userIds).slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT);
|
||||
const { users: { byId, statusesById }, currentUserId } = global;
|
||||
const { chatId: currentChatId } = selectCurrentMessageList(global) || {};
|
||||
const visibleUserIds = selectVisibleUsers(global)?.map(({ id }) => id);
|
||||
|
||||
const idsToSave = unique([
|
||||
...currentUserId ? [currentUserId] : [],
|
||||
...currentChatId && isUserId(currentChatId) ? [currentChatId] : [],
|
||||
...visibleUserIds || [],
|
||||
...global.topPeers.userIds || [],
|
||||
...getOrderedIds(ALL_FOLDER_ID)?.filter(isUserId) || [],
|
||||
...getOrderedIds(ARCHIVED_FOLDER_ID)?.filter(isUserId) || [],
|
||||
...global.contactList?.userIds || [],
|
||||
...global.globalSearch.recentlyFoundChatIds?.filter(isUserId) || [],
|
||||
...Object.keys(byId),
|
||||
]).slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT);
|
||||
|
||||
return {
|
||||
byId: pick(byId, idsToSave),
|
||||
@ -275,29 +294,33 @@ function reduceUsers(global: GlobalState): GlobalState['users'] {
|
||||
}
|
||||
|
||||
function reduceChats(global: GlobalState): GlobalState['chats'] {
|
||||
const newListIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT);
|
||||
const { chatId: currentChatId } = selectCurrentMessageList(global) || {};
|
||||
const idsToSave = newListIds.concat(currentChatId ? [currentChatId] : []);
|
||||
const { chats: { byId }, currentUserId } = global;
|
||||
const currentChat = selectCurrentChat(global);
|
||||
const idsToSave = unique([
|
||||
...currentUserId ? [currentUserId] : [],
|
||||
...currentChat ? [currentChat.id] : [],
|
||||
...getOrderedIds(ALL_FOLDER_ID) || [],
|
||||
...getOrderedIds(ARCHIVED_FOLDER_ID) || [],
|
||||
...global.globalSearch.recentlyFoundChatIds || [],
|
||||
...Object.keys(byId),
|
||||
]).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT);
|
||||
|
||||
return {
|
||||
...global.chats,
|
||||
byId: pick(global.chats.byId, idsToSave),
|
||||
listIds: {
|
||||
active: newListIds,
|
||||
},
|
||||
isFullyLoaded: {},
|
||||
orderedPinnedIds: {
|
||||
active: global.chats.orderedPinnedIds.active,
|
||||
},
|
||||
byId: pick(global.chats.byId, idsToSave),
|
||||
};
|
||||
}
|
||||
|
||||
function reduceMessages(global: GlobalState): GlobalState['messages'] {
|
||||
const { currentUserId } = global;
|
||||
const byChatId: GlobalState['messages']['byChatId'] = {};
|
||||
const { chatId: currentChatId } = selectCurrentMessageList(global) || {};
|
||||
|
||||
const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT);
|
||||
const chatIdsToSave = chatIds.concat(currentChatId ? [currentChatId] : []);
|
||||
const chatIdsToSave = [
|
||||
...currentChatId ? [currentChatId] : [],
|
||||
...currentUserId ? [currentUserId] : [],
|
||||
...getOrderedIds(ALL_FOLDER_ID)?.slice(0, GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT) || [],
|
||||
];
|
||||
|
||||
chatIdsToSave.forEach((chatId) => {
|
||||
const current = global.messages.byChatId[chatId];
|
||||
|
||||
@ -29,7 +29,7 @@ import {
|
||||
selectChat, selectUser, selectChatListType, selectIsChatPinned,
|
||||
selectChatFolder, selectSupportChat, selectChatByUsername, selectThreadTopMessageId,
|
||||
selectCurrentMessageList, selectThreadInfo, selectCurrentChat, selectLastServiceNotification,
|
||||
selectThreadParam, selectChatMessage,
|
||||
selectVisibleUsers,
|
||||
} from '../../selectors';
|
||||
import { buildCollectionByKey, omit } from '../../../util/iteratees';
|
||||
import { debounce, pause, throttle } from '../../../util/schedulers';
|
||||
@ -1011,23 +1011,10 @@ async function loadChats(
|
||||
global = getGlobal();
|
||||
|
||||
if (shouldReplace && listType === 'active') {
|
||||
const visibleChats = [];
|
||||
const visibleUsers = [];
|
||||
|
||||
const currentChat = selectCurrentChat(global);
|
||||
if (currentChat) {
|
||||
const { threadId } = selectCurrentMessageList(global)!;
|
||||
visibleChats.push(currentChat);
|
||||
const messageIds = selectThreadParam(global, currentChat.id, threadId, 'viewportIds');
|
||||
const messageSenders = messageIds ? messageIds
|
||||
.map((messageId) => {
|
||||
const { senderId } = selectChatMessage(global, currentChat.id, messageId) || {};
|
||||
return senderId ? selectUser(global, senderId) : undefined;
|
||||
})
|
||||
.filter(Boolean) : [];
|
||||
visibleUsers.push(...messageSenders);
|
||||
}
|
||||
const visibleChats = currentChat ? [currentChat] : [];
|
||||
|
||||
const visibleUsers = selectVisibleUsers(global) || [];
|
||||
if (global.currentUserId && global.users.byId[global.currentUserId]) {
|
||||
visibleUsers.push(global.users.byId[global.currentUserId]);
|
||||
}
|
||||
|
||||
@ -904,3 +904,21 @@ export function selectDefaultReaction(global: GlobalState, chatId: string) {
|
||||
|
||||
return defaultReaction;
|
||||
}
|
||||
|
||||
// Slow, not to be used in `withGlobal`
|
||||
export function selectVisibleUsers(global: GlobalState) {
|
||||
const { chatId, threadId } = selectCurrentMessageList(global) || {};
|
||||
if (!chatId || !threadId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const messageIds = selectThreadParam(global, chatId, threadId, 'viewportIds');
|
||||
if (!messageIds) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return messageIds.map((messageId) => {
|
||||
const { senderId } = selectChatMessage(global, chatId, messageId) || {};
|
||||
return senderId ? selectUser(global, senderId) : undefined;
|
||||
}).filter(Boolean);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user