Skip to content

Commit 94bd50f

Browse files
authored
feat: cache messages received via web sockets (#2766)
1 parent 9becafa commit 94bd50f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

packages/mgt-chat/src/statefulClient/Caching/MessageCache.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,24 @@ export class MessageCache {
7373
}
7474

7575
private addMessageToCacheData(message: ChatMessage, cachedData: CachedMessageData) {
76-
// this message hasn't been modified, it should not be in the cache already
77-
// NB this assumption may prove to be an issue later, in which case, I'm sorry future developer.
78-
if (message.lastModifiedDateTime === message.createdDateTime) {
79-
cachedData.value.push(message);
76+
const spliceIndex = cachedData.value.findIndex(m => m.id === message.id);
77+
if (spliceIndex !== -1) {
78+
cachedData.value.splice(spliceIndex, 1, message);
8079
} else {
81-
const spliceIndex = cachedData.value.findIndex(m => m.id === message.id);
82-
if (spliceIndex !== -1) {
83-
cachedData.value.splice(spliceIndex, 1, message);
84-
} else {
85-
cachedData.value.push(message);
86-
}
80+
cachedData.value.push(message);
8781
}
8882
// coerce potential nullish values to an empty string to allow comparison
8983
if (message.lastModifiedDateTime && message.lastModifiedDateTime > (cachedData.lastModifiedDateTime ?? ''))
9084
cachedData.lastModifiedDateTime = message.lastModifiedDateTime;
9185
}
86+
87+
public async deleteMessage(chatId: string, message: ChatMessage) {
88+
const cachedData = await this.cache.getValue(chatId);
89+
// for now we're ignoring the case where we didn't find anything in the cache for the given chatId as there's nothing to delete.
90+
if (cachedData) {
91+
const spliceIndex = cachedData.value.findIndex(m => m.id === message.id);
92+
cachedData.value.splice(spliceIndex, 1);
93+
await this.cache.putValue(chatId, cachedData);
94+
}
95+
}
9296
}

packages/mgt-chat/src/statefulClient/StatefulGraphChatClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ detail: ${JSON.stringify(eventDetail)}`);
772772
* Event handler to be called when a new message is received by the notification service
773773
*/
774774
private readonly onMessageReceived = async (message: ChatMessage) => {
775+
await this._cache.cacheMessage(this._chatId, message);
775776
const messageConversion = this.convertChatMessage(message);
776777
const acsMessage = messageConversion.currentValue;
777778
this.updateMessages(acsMessage);
@@ -786,8 +787,10 @@ detail: ${JSON.stringify(eventDetail)}`);
786787
* Event handler to be called when a message deletion is received by the notification service
787788
*/
788789
private readonly onMessageDeleted = (message: ChatMessage) => {
790+
void this._cache.deleteMessage(this.chatId, message);
789791
this.notifyStateChange((draft: GraphChatClient) => {
790792
const draftMessage = draft.messages.find(m => m.messageId === message.id) as AcsChatMessage;
793+
// TODO: confirm if we should show the deleted content message in all cases or only when the message was deleted by the current user
791794
if (draftMessage) this.setDeletedContent(draftMessage);
792795
});
793796
};
@@ -796,6 +799,7 @@ detail: ${JSON.stringify(eventDetail)}`);
796799
* Event handler to be called when a message edit is received by the notification service
797800
*/
798801
private readonly onMessageEdited = async (message: ChatMessage) => {
802+
await this._cache.cacheMessage(this._chatId, message);
799803
const messageConversion = this.convertChatMessage(message);
800804
this.updateMessages(messageConversion.currentValue);
801805
if (messageConversion.futureValue) {

0 commit comments

Comments
 (0)