try a fix to prevent window from jumping with low number of messages

pull/2190/head
Audric Ackermann 3 years ago
parent 2e200df933
commit 75c7c7c27f
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -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 // 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 firstUnread = getFirstUnreadMessageIdInConversation(conversationId);
const numberOfMessagesInConvo = getMessagesCountByConversation(globalInstance, conversationId);
const floorLoadAllMessagesInConvo = 70;
if (messageId || firstUnread) { if (messageId || firstUnread) {
const messageFound = getMessageById(messageId || firstUnread); const messageFound = getMessageById(messageId || firstUnread);
@ -2310,7 +2313,10 @@ function getMessagesByConversation(conversationId, { messageId = null } = {}) {
.all({ .all({
conversationId, conversationId,
messageId: messageId || firstUnread, messageId: messageId || firstUnread,
limit: absLimit, limit:
numberOfMessagesInConvo < floorLoadAllMessagesInConvo
? floorLoadAllMessagesInConvo
: absLimit,
}); });
return map(rows, row => jsonToObject(row.json)); 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 const rows = globalInstance
.prepare( .prepare(

@ -329,6 +329,8 @@ async function getMessages({
conversationKey: string; conversationKey: string;
messageId: string | null; messageId: string | null;
}): Promise<Array<MessageModelPropsWithoutConvoProps>> { }): Promise<Array<MessageModelPropsWithoutConvoProps>> {
const beforeTimestamp = Date.now();
const conversation = getConversationController().get(conversationKey); const conversation = getConversationController().get(conversationKey);
if (!conversation) { if (!conversation) {
// no valid conversation, early return // no valid conversation, early return
@ -343,6 +345,8 @@ async function getMessages({
const messageProps: Array<MessageModelPropsWithoutConvoProps> = messageSet.models.map(m => const messageProps: Array<MessageModelPropsWithoutConvoProps> = messageSet.models.map(m =>
m.getMessageModelProps() m.getMessageModelProps()
); );
const time = Date.now() - beforeTimestamp;
window?.log?.info(`Loading ${messageProps.length} messages took ${time}ms to load.`);
return messageProps; return messageProps;
} }
@ -373,13 +377,10 @@ export const fetchTopMessagesForConversation = createAsyncThunk(
window.log.info('fetchTopMessagesForConversation: we are already at the top'); window.log.info('fetchTopMessagesForConversation: we are already at the top');
return null; return null;
} }
const beforeTimestamp = Date.now();
const messagesProps = await getMessages({ const messagesProps = await getMessages({
conversationKey, conversationKey,
messageId: oldTopMessageId, messageId: oldTopMessageId,
}); });
const time = Date.now() - beforeTimestamp;
window?.log?.info(`Loading ${messagesProps.length} messages took ${time}ms to load.`);
return { return {
conversationKey, conversationKey,
@ -405,7 +406,6 @@ export const fetchBottomMessagesForConversation = createAsyncThunk(
conversationKey: string; conversationKey: string;
oldBottomMessageId: string | null; oldBottomMessageId: string | null;
}): Promise<FetchedBottomMessageResults> => { }): Promise<FetchedBottomMessageResults> => {
const beforeTimestamp = Date.now();
// no need to load more bottom if we are already at the bottom // no need to load more bottom if we are already at the bottom
const mostRecentMessage = await getLastMessageInConversation(conversationKey); const mostRecentMessage = await getLastMessageInConversation(conversationKey);
@ -417,8 +417,6 @@ export const fetchBottomMessagesForConversation = createAsyncThunk(
conversationKey, conversationKey,
messageId: oldBottomMessageId, messageId: oldBottomMessageId,
}); });
const time = Date.now() - beforeTimestamp;
window?.log?.info(`Loading ${messagesProps.length} messages took ${time}ms to load.`);
return { return {
conversationKey, conversationKey,

Loading…
Cancel
Save