@@ -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}
0 commit comments