fix: message duplication when some messages have the same creation timestamp#1421
Merged
myandrienko merged 8 commits intomasterfrom Dec 18, 2024
Merged
fix: message duplication when some messages have the same creation timestamp#1421myandrienko merged 8 commits intomasterfrom
myandrienko merged 8 commits intomasterfrom
Conversation
myandrienko
commented
Dec 17, 2024
Comment on lines
368
to
378
| // 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; | ||
| } | ||
| } | ||
| } |
Comment on lines
-422
to
-425
| if (newMessages[insertionIndex - 1] && newMessage.id === newMessages[insertionIndex - 1].id) { | ||
| newMessages[insertionIndex - 1] = newMessage; | ||
| return newMessages; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
We no longer need to check this case: if a message needs to be updated, insertionIndex is guaranteed to point at it.
Contributor
|
Size Change: +670 B (+0.15%) Total Size: 462 kB
|
arnautov-anton
approved these changes
Dec 17, 2024
Contributor
arnautov-anton
left a comment
There was a problem hiding this comment.
cc: @MartinCupela
MartinCupela
approved these changes
Dec 18, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

To reproduce an existing bug:
<StrictMode />is enabled and the effect inChannelListruns 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.