Folder Manager: Reset when signing out

This commit is contained in:
Alexander Zinchuk 2022-02-08 22:29:45 +01:00
parent 79c3103b15
commit 12dca88a4c

View File

@ -1,4 +1,4 @@
import { addCallback, getGlobal } from '../lib/teact/teactn'; import { addCallback, addReducer, getGlobal } from '../lib/teact/teactn';
import { GlobalState } from '../global/types'; import { GlobalState } from '../global/types';
import { NotifyException, NotifySettings } from '../types'; import { NotifyException, NotifySettings } from '../types';
@ -46,7 +46,9 @@ interface ChatSummary {
const UPDATE_THROTTLE = 500; const UPDATE_THROTTLE = 500;
const DEBUG_DURATION_LIMIT = 6; const DEBUG_DURATION_LIMIT = 6;
const prevGlobal: { const initials = buildInitials();
let prevGlobal: {
allFolderListIds?: GlobalState['chats']['listIds']['active']; allFolderListIds?: GlobalState['chats']['listIds']['active'];
allFolderPinnedIds?: GlobalState['chats']['orderedPinnedIds']['active']; allFolderPinnedIds?: GlobalState['chats']['orderedPinnedIds']['active'];
archivedFolderListIds?: GlobalState['chats']['listIds']['archived']; archivedFolderListIds?: GlobalState['chats']['listIds']['archived'];
@ -58,50 +60,30 @@ const prevGlobal: {
usersById: Record<string, ApiUser>; usersById: Record<string, ApiUser>;
notifySettings: NotifySettings; notifySettings: NotifySettings;
notifyExceptions?: Record<number, NotifyException>; notifyExceptions?: Record<number, NotifyException>;
} = { } = initials.prevGlobal;
foldersById: {},
chatsById: {},
usersById: {},
notifySettings: {} as NotifySettings,
notifyExceptions: {},
};
const prepared: { let prepared: {
folderSummariesById: Record<string, FolderSummary>; folderSummariesById: Record<string, FolderSummary>;
chatSummariesById: Map<string, ChatSummary>; chatSummariesById: Map<string, ChatSummary>;
folderIdsByChatId: Record<string, number[]>; folderIdsByChatId: Record<string, number[]>;
chatIdsByFolderId: Record<number, Set<string>>; chatIdsByFolderId: Record<number, Set<string> | undefined>;
isOrderedListJustPatched: Record<number, boolean | undefined>; isOrderedListJustPatched: Record<number, boolean | undefined>;
} = { } = initials.prepared;
folderSummariesById: {},
chatSummariesById: new Map(),
folderIdsByChatId: {},
chatIdsByFolderId: {},
isOrderedListJustPatched: {},
};
const results: { let results: {
orderedIdsByFolderId: Record<string, string[] | undefined>; orderedIdsByFolderId: Record<string, string[] | undefined>;
chatsCountByFolderId: Record<string, number | undefined>; chatsCountByFolderId: Record<string, number | undefined>;
unreadCountersByFolderId: Record<string, { unreadCountersByFolderId: Record<string, {
chatsCount: number; chatsCount: number;
notificationsCount: number; notificationsCount: number;
} | undefined>; } | undefined>;
} = { } = initials.results;
orderedIdsByFolderId: {},
chatsCountByFolderId: {},
unreadCountersByFolderId: {},
};
const callbacks: { let callbacks: {
orderedIdsByFolderId: Record<number, CallbackManager>; orderedIdsByFolderId: Record<number, CallbackManager>;
chatsCountByFolderId: CallbackManager; chatsCountByFolderId: CallbackManager;
unreadCountersByFolderId: CallbackManager; unreadCountersByFolderId: CallbackManager;
} = { } = initials.callbacks;
orderedIdsByFolderId: {},
chatsCountByFolderId: createCallbackManager(),
unreadCountersByFolderId: createCallbackManager(),
};
const updateFolderManagerThrottled = throttle(() => { const updateFolderManagerThrottled = throttle(() => {
onIdle(() => { onIdle(() => {
@ -112,7 +94,11 @@ const updateFolderManagerThrottled = throttle(() => {
let inited = false; let inited = false;
function init() { function init() {
inited = true;
addCallback(updateFolderManagerThrottled); addCallback(updateFolderManagerThrottled);
addReducer('reset', reset);
updateFolderManager(getGlobal()); updateFolderManager(getGlobal());
} }
@ -529,7 +515,7 @@ function updateListsForChat(chatId: string, currentFolderIds: number[], newFolde
let currentFolderOrderedIds = results.orderedIdsByFolderId[folderId]; let currentFolderOrderedIds = results.orderedIdsByFolderId[folderId];
if (currentFolderIdsSet.has(folderId) && !newFolderIdsSet.has(folderId)) { if (currentFolderIdsSet.has(folderId) && !newFolderIdsSet.has(folderId)) {
prepared.chatIdsByFolderId[folderId].delete(chatId); prepared.chatIdsByFolderId[folderId]!.delete(chatId);
deletedFolderIds.push(folderId); deletedFolderIds.push(folderId);
@ -542,7 +528,7 @@ function updateListsForChat(chatId: string, currentFolderIds: number[], newFolde
prepared.chatIdsByFolderId[folderId] = new Set(); prepared.chatIdsByFolderId[folderId] = new Set();
} }
prepared.chatIdsByFolderId[folderId].add(chatId); prepared.chatIdsByFolderId[folderId]!.add(chatId);
if (currentFolderOrderedIds) { if (currentFolderOrderedIds) {
currentFolderOrderedIds.push(chatId); currentFolderOrderedIds.push(chatId);
@ -564,6 +550,10 @@ function updateResults(affectedFolderIds: number[]) {
Array.from(affectedFolderIds).forEach((folderId) => { Array.from(affectedFolderIds).forEach((folderId) => {
const newOrderedIds = buildFolderOrderedIds(folderId); const newOrderedIds = buildFolderOrderedIds(folderId);
// When signed out
if (!newOrderedIds) {
return;
}
const currentOrderedIds = results.orderedIdsByFolderId[folderId]; const currentOrderedIds = results.orderedIdsByFolderId[folderId];
const areOrderedIdsChanged = ( const areOrderedIdsChanged = (
@ -610,8 +600,13 @@ function updateResults(affectedFolderIds: number[]) {
} }
function buildFolderOrderedIds(folderId: number) { function buildFolderOrderedIds(folderId: number) {
const folderSummary = prepared.folderSummariesById[folderId];
if (!folderSummary) {
return undefined;
}
const { orderedPinnedIds, pinnedChatIds } = folderSummary;
const { const {
folderSummariesById: { [folderId]: { orderedPinnedIds, pinnedChatIds } },
chatSummariesById, chatSummariesById,
chatIdsByFolderId: { [folderId]: chatIds }, chatIdsByFolderId: { [folderId]: chatIds },
} = prepared; } = prepared;
@ -667,3 +662,44 @@ function buildFolderUnreadCounters(folderId: number) {
notificationsCount: 0, notificationsCount: 0,
}); });
} }
function buildInitials() {
return {
prevGlobal: {
foldersById: {},
chatsById: {},
usersById: {},
notifySettings: {} as NotifySettings,
notifyExceptions: {},
},
prepared: {
folderSummariesById: {},
chatSummariesById: new Map(),
folderIdsByChatId: {},
chatIdsByFolderId: {},
isOrderedListJustPatched: {},
},
results: {
orderedIdsByFolderId: {},
chatsCountByFolderId: {},
unreadCountersByFolderId: {},
},
callbacks: {
orderedIdsByFolderId: {},
chatsCountByFolderId: createCallbackManager(),
unreadCountersByFolderId: createCallbackManager(),
},
};
}
function reset() {
const newInitials = buildInitials();
prevGlobal = newInitials.prevGlobal;
prepared = newInitials.prepared;
results = newInitials.results;
callbacks = newInitials.callbacks;
}