Fix syncing while in comments thread; Possible fix for reconnects (#1200)

This commit is contained in:
Alexander Zinchuk 2021-06-22 13:54:20 +03:00
parent 4c2768c5e2
commit c6550ca3e4
2 changed files with 49 additions and 5 deletions

View File

@ -52,9 +52,11 @@ class Connection {
} }
async disconnect() { async disconnect() {
this._connected = false; if (this._connected) {
await this._recvArray.push(undefined); this._connected = false;
await this.socket.close(); await this._recvArray.push(undefined);
await this.socket.close();
}
} }
async send(data) { async send(data) {

View File

@ -23,7 +23,7 @@ import {
replaceThreadParam, replaceThreadParam,
} from '../../reducers'; } from '../../reducers';
import { import {
selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectThreadInfo,
} from '../../selectors'; } from '../../selectors';
import { isChatPrivate } from '../../helpers'; import { isChatPrivate } from '../../helpers';
@ -187,7 +187,7 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) {
let users = savedUsers || []; let users = savedUsers || [];
let global = getGlobal(); let global = getGlobal();
const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; const { chatId: currentChatId, threadId: currentThreadId } = selectCurrentMessageList(global) || {};
// Memoize drafts // Memoize drafts
const draftChatIds = Object.keys(global.messages.byChatId).map(Number); const draftChatIds = Object.keys(global.messages.byChatId).map(Number);
@ -200,6 +200,7 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) {
const result = await loadTopMessages(global.chats.byId[currentChatId]); const result = await loadTopMessages(global.chats.byId[currentChatId]);
global = getGlobal(); global = getGlobal();
const { chatId: newCurrentChatId } = selectCurrentMessageList(global) || {}; const { chatId: newCurrentChatId } = selectCurrentMessageList(global) || {};
const threadInfo = currentThreadId && selectThreadInfo(global, currentChatId, currentThreadId);
if (result && newCurrentChatId === currentChatId) { if (result && newCurrentChatId === currentChatId) {
const currentMessageListInfo = global.messages.byChatId[currentChatId]; const currentMessageListInfo = global.messages.byChatId[currentChatId];
@ -226,6 +227,47 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) {
}, },
}; };
if (currentThreadId && threadInfo && threadInfo.originChannelId) {
const { originChannelId } = threadInfo;
const currentMessageListInfoOrigin = global.messages.byChatId[originChannelId];
const resultOrigin = await loadTopMessages(global.chats.byId[originChannelId]);
if (resultOrigin) {
const byIdOrigin = buildCollectionByKey(resultOrigin.messages, 'id');
const listedIdsOrigin = Object.keys(byIdOrigin)
.map(Number);
global = {
...global,
messages: {
...global.messages,
byChatId: {
...global.messages.byChatId,
[threadInfo.originChannelId]: {
byId: byIdOrigin,
threadsById: {
[MAIN_THREAD_ID]: {
...(currentMessageListInfoOrigin && currentMessageListInfoOrigin.threadsById[MAIN_THREAD_ID]),
listedIds: listedIdsOrigin,
viewportIds: listedIdsOrigin,
outlyingIds: undefined,
},
},
},
[currentChatId]: {
...global.messages.byChatId[currentChatId],
threadsById: {
...global.messages.byChatId[currentChatId].threadsById,
[currentThreadId]: {
...(currentMessageListInfo && currentMessageListInfo.threadsById[currentThreadId]),
outlyingIds: undefined,
},
},
},
},
},
};
}
}
global = updateChats(global, buildCollectionByKey(result.chats, 'id')); global = updateChats(global, buildCollectionByKey(result.chats, 'id'));
global = updateThreadInfos(global, currentChatId, result.threadInfos); global = updateThreadInfos(global, currentChatId, result.threadInfos);