diff --git a/ts/models/message.ts b/ts/models/message.ts index 0d7ad19b9..3c9c29aef 100644 --- a/ts/models/message.ts +++ b/ts/models/message.ts @@ -1365,6 +1365,23 @@ const throttledAllMessagesDispatch = debounce( { trailing: true, leading: true, maxWait: 1000 } ); +/** + * With `throttledAllMessagesDispatch`, we batch refresh changed messages every XXXms. + * Sometimes, a message is changed and then deleted quickly. + * This can cause an issue because if the message is deleted, but the XXXms ticks after that, + * the message will appear again in the redux store. + * This is a mistake, and was usually fixed by reloading the corresponding conversation. + * Well, this function should hopefully fix this issue. + * Anytime we delete a message, we have to call it to "cancel scheduled refreshes" + * @param messageIds the ids to cancel the dispatch of. + */ +export function cancelUpdatesToDispatch(messageIds: Array) { + for (let index = 0; index < messageIds.length; index++) { + const messageId = messageIds[index]; + updatesToDispatch.delete(messageId); + } +} + const updatesToDispatch: Map = new Map(); export class MessageCollection extends Backbone.Collection {} diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index e99b4190f..373ead341 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -26,6 +26,7 @@ import { import { AttachmentType } from '../../types/Attachment'; import { CONVERSATION_PRIORITIES, ConversationTypeEnum } from '../../models/types'; import { WithConvoId, WithMessageHash, WithMessageId } from '../../session/types/with'; +import { cancelUpdatesToDispatch } from '../../models/message'; export type MessageModelPropsWithoutConvoProps = { propsForMessage: PropsForMessageWithoutConvoProps; @@ -543,6 +544,10 @@ function handleMessageExpiredOrDeleted( const messageId = (payload as any).messageId as string | undefined; const messageHash = (payload as any).messageHash as string | undefined; + if (messageId) { + cancelUpdatesToDispatch([messageId]); + } + if (conversationId === state.selectedConversation) { // search if we find this message id. // we might have not loaded yet, so this case might not happen