Skip to content

Commit

Permalink
fix: message insert mode when loading a room for 1st time on non-empt…
Browse files Browse the repository at this point in the history
…y store
  • Loading branch information
valeriansaliou committed Jun 17, 2024
1 parent 3fb1c63 commit 692626d
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/store/tables/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,35 @@ const detectInsertMode = function (
container: InboxEntryMessages,
messages: Array<CoreMessage>
): InboxInsertMode {
// Notice: this compares the first message entry date with the last core \
// message date. If the first container message is more recent than \
// the last core message to be inserted, then it means the whole block \
// of messages to insert should be prepended BEFORE existing messages. \
// This is required if there are already messages in the store, that \
// could be more recent than those messages, eg. if a new message was \
// received in-band and the room is opened later.
// Important: acquire insert mode AFTER loading messages and \
// BEFORE inserting them to the store, since the loading could \
// have taken quite some time, and some messages might have been \
// inserted in the store mid-way.
if (
container.list.length > 0 &&
messages.length > 0 &&
container.list[0].timestamp >= messages[messages.length - 1].date.getTime()
) {
// Use restore mode
return InboxInsertMode.Restore;
const storeMessages = container.list;

if (messages.length > 0 && storeMessages.length > 0) {
const lastStoreMessageTime =
storeMessages[storeMessages.length - 1].timestamp;

const firstInsertMessageTime = messages[0].date.getTime(),
lastInsertMessageTime = messages[messages.length - 1].date.getTime();

// Notice: this compares the last store message date with the first \
// message date. If the last store message is more recent than \
// the first message to be inserted, then it means the whole block \
// of messages to insert should be prepended BEFORE existing messages. \
// This is required if there are already messages in the store, that \
// could be more recent than those messages, eg. if a new message was \
// received in-band and the room is opened later.
// Important: acquire insert mode AFTER loading messages and \
// BEFORE inserting them to the store, since the loading could \
// have taken quite some time, and some messages might have been \
// inserted in the store mid-way.
if (lastInsertMessageTime >= lastStoreMessageTime) {
// Use insert mode
return InboxInsertMode.Insert;
}

if (firstInsertMessageTime < lastStoreMessageTime) {
// Use restore mode
return InboxInsertMode.Restore;
}
}

// Use insert mode (default)
Expand Down

0 comments on commit 692626d

Please sign in to comment.