@@ -716,6 +716,7 @@ const ChannelWithContext = <
716716 * Its a map of filename to AbortController
717717 */
718718 const uploadAbortControllerRef = useRef < Map < string , AbortController > > ( new Map ( ) ) ;
719+ const optimisticallyUpdatedNewMessages = useRef < Set < string > > ( new Set ( ) ) ;
719720
720721 const channelId = channel ?. id || '' ;
721722 const pollCreationEnabled = ! channel . disconnected && ! ! channel ?. id && channel ?. getConfig ( ) ?. polls ;
@@ -837,9 +838,18 @@ const ChannelWithContext = <
837838 }
838839
839840 // only update channel state if the events are not the previously subscribed useEffect's subscription events
841+ console . log ( 'EVENT: ' , event ) ;
840842 if ( channel && channel . initialized ) {
841- if ( event . type === 'message.new' ) {
842- copyMessagesStateFromChannelThrottled ( ) ;
843+ if ( event . type === 'message.new' || event . type === 'notification.message_new' ) {
844+ const messageId = event . message ?. id ?? '' ;
845+ if (
846+ event . user ?. id !== client . userID ||
847+ ! optimisticallyUpdatedNewMessages . current . has ( messageId )
848+ ) {
849+ console . log ( 'INSIDE' , Array . from ( optimisticallyUpdatedNewMessages . current ) ) ;
850+ copyMessagesStateFromChannelThrottled ( ) ;
851+ }
852+ optimisticallyUpdatedNewMessages . current . delete ( messageId ) ;
843853 return ;
844854 }
845855
@@ -848,6 +858,7 @@ const ChannelWithContext = <
848858 return ;
849859 }
850860
861+ console . log ( 'FULL STATE UPDATE' ) ;
851862 copyChannelState ( ) ;
852863 }
853864 }
@@ -1180,13 +1191,17 @@ const ChannelWithContext = <
11801191 * MESSAGE METHODS
11811192 */
11821193 const updateMessage : MessagesContextValue < StreamChatGenerics > [ 'updateMessage' ] =
1183- useStableCallback ( ( updatedMessage , extraState = { } ) => {
1194+ useStableCallback ( ( updatedMessage , extraState = { } , throttled = false ) => {
11841195 if ( ! channel ) {
11851196 return ;
11861197 }
11871198
11881199 channel . state . addMessageSorted ( updatedMessage , true ) ;
1189- copyMessagesStateFromChannelThrottled ( ) ;
1200+ if ( throttled ) {
1201+ copyMessagesStateFromChannelThrottled ( ) ;
1202+ } else {
1203+ copyMessagesStateFromChannel ( channel ) ;
1204+ }
11901205
11911206 if ( thread && updatedMessage . parent_id ) {
11921207 extraState . threadMessages = channel . state . threads [ updatedMessage . parent_id ] || [ ] ;
@@ -1401,7 +1416,7 @@ const ChannelWithContext = <
14011416 if ( retrying ) {
14021417 replaceMessage ( message , messageResponse . message ) ;
14031418 } else {
1404- updateMessage ( messageResponse . message ) ;
1419+ updateMessage ( messageResponse . message , { } , true ) ;
14051420 }
14061421 }
14071422 } catch ( err ) {
@@ -1436,6 +1451,7 @@ const ChannelWithContext = <
14361451 messageInput : '' ,
14371452 } ) ;
14381453 threadInstance ?. upsertReplyLocally ?.( { message : messagePreview } ) ;
1454+ optimisticallyUpdatedNewMessages . current . add ( messagePreview . id ) ;
14391455
14401456 if ( enableOfflineSupport ) {
14411457 // While sending a message, we add the message to local db with failed status, so that
0 commit comments