@@ -717,6 +717,11 @@ const ChannelWithContext = <
717717 * Its a map of filename to AbortController
718718 */
719719 const uploadAbortControllerRef = useRef < Map < string , AbortController > > ( new Map ( ) ) ;
720+ /**
721+ * This ref keeps track of message IDs which have already been optimistically updated.
722+ * We need it to make sure we don't react on message.new/notification.message_new events
723+ * if this is indeed the case, as it's a full list update for nothing.
724+ */
720725 const optimisticallyUpdatedNewMessages = useRef < Set < string > > ( new Set ( ) ) ;
721726
722727 const channelId = channel ?. id || '' ;
@@ -839,15 +844,14 @@ const ChannelWithContext = <
839844 }
840845
841846 // only update channel state if the events are not the previously subscribed useEffect's subscription events
842- console . log ( 'EVENT: ' , event ) ;
843847 if ( channel && channel . initialized ) {
848+ // we skip the new message events if we've already done an optimistic update for the new message
844849 if ( event . type === 'message.new' || event . type === 'notification.message_new' ) {
845850 const messageId = event . message ?. id ?? '' ;
846851 if (
847852 event . user ?. id !== client . userID ||
848853 ! optimisticallyUpdatedNewMessages . current . has ( messageId )
849854 ) {
850- console . log ( 'INSIDE' , Array . from ( optimisticallyUpdatedNewMessages . current ) ) ;
851855 copyMessagesStateFromChannelThrottled ( ) ;
852856 }
853857 optimisticallyUpdatedNewMessages . current . delete ( messageId ) ;
@@ -859,7 +863,6 @@ const ChannelWithContext = <
859863 return ;
860864 }
861865
862- console . log ( 'FULL STATE UPDATE' ) ;
863866 copyChannelState ( ) ;
864867 }
865868 }
@@ -1426,6 +1429,7 @@ const ChannelWithContext = <
14261429 const updatedMessage = { ...message , cid : channel . cid } ;
14271430 updateMessage ( updatedMessage ) ;
14281431 threadInstance ?. upsertReplyLocally ?.( { message : updatedMessage } ) ;
1432+ optimisticallyUpdatedNewMessages . current . delete ( message . id ) ;
14291433
14301434 if ( enableOfflineSupport ) {
14311435 await dbApi . updateMessage ( {
0 commit comments