Skip to content
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

Merged
merged 8 commits into from
Dec 18, 2024

Conversation

myandrienko
Copy link
Contributor

To reproduce an existing bug:

  1. Have several (3+) messages with the same creation timestamp in a channel (you can send them quickly with the server-side API)
  2. Set up a situation when channels are queried twice (e.g. <StrictMode /> is enabled and the effect in ChannelList runs twice)
  3. Observe duplicating messages

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.

src/utils.ts Outdated
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;
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍒

Comment on lines -422 to -425
if (newMessages[insertionIndex - 1] && newMessage.id === newMessages[insertionIndex - 1].id) {
newMessages[insertionIndex - 1] = newMessage;
return newMessages;
}
Copy link
Contributor Author

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.

Copy link
Contributor

github-actions bot commented Dec 17, 2024

Size Change: +670 B (+0.15%)

Total Size: 462 kB

Filename Size Change
dist/browser.es.js 101 kB +152 B (+0.15%)
dist/browser.full-bundle.min.js 56.9 kB +42 B (+0.07%)
dist/browser.js 102 kB +160 B (+0.16%)
dist/index.es.js 101 kB +154 B (+0.15%)
dist/index.js 102 kB +162 B (+0.16%)

compressed-size-action

Copy link
Contributor

@arnautov-anton arnautov-anton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/thread.ts Outdated Show resolved Hide resolved
src/thread.ts Outdated Show resolved Hide resolved
@myandrienko myandrienko merged commit b7b019a into master Dec 18, 2024
5 checks passed
@myandrienko myandrienko deleted the message-insertion branch December 18, 2024 17:20
@github-actions github-actions bot mentioned this pull request Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants