From 75c7c7c27fa6bdec1cf0490a7085a0ab78d91ee1 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Wed, 9 Mar 2022 11:44:30 +1100 Subject: [PATCH] try a fix to prevent window from jumping with low number of messages --- app/sql.js | 13 +++++++++++-- ts/state/ducks/conversations.ts | 10 ++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/sql.js b/app/sql.js index 8d773f32c..deb91b955 100644 --- a/app/sql.js +++ b/app/sql.js @@ -2286,6 +2286,9 @@ function getMessagesByConversation(conversationId, { messageId = null } = {}) { // If messageId is null, it means we are just opening the convo to the last unread message, or at the bottom const firstUnread = getFirstUnreadMessageIdInConversation(conversationId); + const numberOfMessagesInConvo = getMessagesCountByConversation(globalInstance, conversationId); + const floorLoadAllMessagesInConvo = 70; + if (messageId || firstUnread) { const messageFound = getMessageById(messageId || firstUnread); @@ -2310,7 +2313,10 @@ function getMessagesByConversation(conversationId, { messageId = null } = {}) { .all({ conversationId, messageId: messageId || firstUnread, - limit: absLimit, + limit: + numberOfMessagesInConvo < floorLoadAllMessagesInConvo + ? floorLoadAllMessagesInConvo + : absLimit, }); return map(rows, row => jsonToObject(row.json)); @@ -2320,7 +2326,10 @@ function getMessagesByConversation(conversationId, { messageId = null } = {}) { ); } - const limit = 2 * absLimit; + const limit = + numberOfMessagesInConvo < floorLoadAllMessagesInConvo + ? floorLoadAllMessagesInConvo + : 2 * absLimit; const rows = globalInstance .prepare( diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 4186b6b4a..7a807639f 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -329,6 +329,8 @@ async function getMessages({ conversationKey: string; messageId: string | null; }): Promise> { + const beforeTimestamp = Date.now(); + const conversation = getConversationController().get(conversationKey); if (!conversation) { // no valid conversation, early return @@ -343,6 +345,8 @@ async function getMessages({ const messageProps: Array = messageSet.models.map(m => m.getMessageModelProps() ); + const time = Date.now() - beforeTimestamp; + window?.log?.info(`Loading ${messageProps.length} messages took ${time}ms to load.`); return messageProps; } @@ -373,13 +377,10 @@ export const fetchTopMessagesForConversation = createAsyncThunk( window.log.info('fetchTopMessagesForConversation: we are already at the top'); return null; } - const beforeTimestamp = Date.now(); const messagesProps = await getMessages({ conversationKey, messageId: oldTopMessageId, }); - const time = Date.now() - beforeTimestamp; - window?.log?.info(`Loading ${messagesProps.length} messages took ${time}ms to load.`); return { conversationKey, @@ -405,7 +406,6 @@ export const fetchBottomMessagesForConversation = createAsyncThunk( conversationKey: string; oldBottomMessageId: string | null; }): Promise => { - const beforeTimestamp = Date.now(); // no need to load more bottom if we are already at the bottom const mostRecentMessage = await getLastMessageInConversation(conversationKey); @@ -417,8 +417,6 @@ export const fetchBottomMessagesForConversation = createAsyncThunk( conversationKey, messageId: oldBottomMessageId, }); - const time = Date.now() - beforeTimestamp; - window?.log?.info(`Loading ${messagesProps.length} messages took ${time}ms to load.`); return { conversationKey,