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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
sortedArray: replies,
sortDirection: 'ascending',
selectValueToCompare: (reply) => reply.created_at.getTime(),
selectKey: (reply) => reply.id,
});

const actualIndex =
myandrienko marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
37 changes: 24 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export function formatMessage<StreamChatGenerics extends ExtendableGenerics = De
export const findIndexInSortedArray = <T, L>({
needle,
sortedArray,
selectKey,
selectValueToCompare = (e) => e,
sortDirection = 'ascending',
}: {
Expand All @@ -325,6 +326,7 @@ export const findIndexInSortedArray = <T, L>({
* selectValueToCompare: (message) => message.created_at.getTime()
* ```
*/
selectKey?: (arrayElement: T) => string;
myandrienko marked this conversation as resolved.
Show resolved Hide resolved
selectValueToCompare?: (arrayElement: T) => L | T;
/**
* @default ascending
Expand Down Expand Up @@ -353,18 +355,28 @@ export const findIndexInSortedArray = <T, L>({

const comparableMiddle = selectValueToCompare(sortedArray[middle]);

// if (comparableNeedle === comparableMiddle) return middle;

if (
(sortDirection === 'ascending' && comparableNeedle < comparableMiddle) ||
(sortDirection === 'descending' && comparableNeedle > comparableMiddle)
(sortDirection === 'descending' && comparableNeedle >= comparableMiddle)
) {
right = middle - 1;
} else {
left = middle + 1;
}
}

// 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.

🍒


return left;
};

Expand Down Expand Up @@ -410,19 +422,18 @@ export function addToMessageList<T extends FormatMessageResponse>(
sortDirection: 'ascending',
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectValueToCompare: (m) => m[sortBy]!.getTime(),
selectKey: (m) => m.id,
});

// message already exists and not filtered with timestampChanged, update and return
if (!timestampChanged && newMessage.id) {
if (newMessages[insertionIndex] && newMessage.id === newMessages[insertionIndex].id) {
newMessages[insertionIndex] = newMessage;
return newMessages;
}

if (newMessages[insertionIndex - 1] && newMessage.id === newMessages[insertionIndex - 1].id) {
newMessages[insertionIndex - 1] = newMessage;
return newMessages;
}
Comment on lines -422 to -425
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.

if (
!timestampChanged &&
newMessage.id &&
newMessages[insertionIndex] &&
newMessage.id === newMessages[insertionIndex].id
) {
newMessages[insertionIndex] = newMessage;
return newMessages;
}

// do not add updated or deleted messages to the list if they already exist or come with a timestamp change
Expand Down
Loading