-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: message duplication when some messages have the same creation timestamp #1421
Conversation
src/utils.ts
Outdated
// In case there are several array elements with the same comparable value, search around the insertion | ||
// point to possibly find an element with the same key. If found, prefer it. | ||
// This, for example, prevents duplication of messages with the same creation date. | ||
if (selectKey) { | ||
const needleKey = selectKey(needle); | ||
for (let i = left; sortedArray[i] === comparableNeedle; i += sortDirection === 'ascending' ? -1 : +1) { | ||
if (selectKey(sortedArray[i]) === needleKey) { | ||
return i; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍒
if (newMessages[insertionIndex - 1] && newMessage.id === newMessages[insertionIndex - 1].id) { | ||
newMessages[insertionIndex - 1] = newMessage; | ||
return newMessages; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need to check this case: if a message needs to be updated, insertionIndex
is guaranteed to point at it.
Size Change: +670 B (+0.15%) Total Size: 462 kB
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @MartinCupela
To reproduce an existing bug:
<StrictMode />
is enabled and the effect inChannelList
runs twice)The issue is that when inserting items into a sorted array with duplicating creation date, the insertion index points next to a fairly random message with the same creation date. That is not necessarily the message with same id, it could be any other message with the same creation id. So, instead of updating an existing message, we add it again.
This is fixed by performing an additional look-around when inserting a message, searching for the message with the same creation date and the same id. It adds very little overhead, since the search range is very limited. If the message with the same key is found, it is returned as an insertion index.