Skip to content

Commit

Permalink
fix: cached message state mixup (#3278)
Browse files Browse the repository at this point in the history
* fix cached message state duplication

* ensure only messages in chat available

* check for duplicates in delta messages
  • Loading branch information
Mnickii authored Sep 5, 2024
1 parent c42aa19 commit 8627a91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/mgt-chat/src/statefulClient/Caching/MessageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export class MessageCache {
return data;
} else {
// need to iterate through the messages and either push or splice them into the array.
messages.forEach(m => this.addMessageToCacheData(m, cachedData));
messages.forEach(m => {
// ensure that only new messages (those not already in the cache) are added to the cache for the specified chat
if (m.chatId === chatId && !cachedData.value.some(cachedMessage => cachedMessage.id === m.id)) {
this.addMessageToCacheData(m, cachedData);
}
});
if (updateNextLink) {
cachedData.nextLink = nextLink;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,13 @@ class StatefulGraphChatClient extends BaseStatefulClient<GraphChatClient> {
const deltaMessages = await this.loadDeltaData(this._chatId, cachedMessages.lastModifiedDateTime);
// add delta messages to cache
const updatedState = await this._cache.cacheMessages(this._chatId, deltaMessages);
// Filters the delta messages based on whether their IDs exist in the updated state.
const filteredDeltaMessages = deltaMessages.filter(
deltaMessage =>
!updatedState.value.some(message => message.id === deltaMessage.id && message.chatId === deltaMessage.chatId)
);
// writeMessagesToState concats with existing state, need to be careful not to create duplicate messages
updatedState.value = deltaMessages;
updatedState.value = filteredDeltaMessages;
// update state
await this.writeMessagesToState(updatedState);
} else {
Expand Down

0 comments on commit 8627a91

Please sign in to comment.