@@ -731,14 +731,33 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
731731 channel,
732732 } ) ;
733733
734- /**
735- * Since we copy the current channel state all together, we need to find the greatest time among the below two and apply it as the throttling time for copying the channel state.
736- * This is done until we remove the newMessageStateUpdateThrottleInterval prop.
737- */
738- const copyChannelStateThrottlingTime =
739- newMessageStateUpdateThrottleInterval > stateUpdateThrottleInterval
740- ? newMessageStateUpdateThrottleInterval
741- : stateUpdateThrottleInterval ;
734+ const setReadThrottled = useMemo (
735+ ( ) =>
736+ throttle (
737+ ( ) => {
738+ if ( channel ) {
739+ setRead ( channel ) ;
740+ }
741+ } ,
742+ stateUpdateThrottleInterval ,
743+ throttleOptions ,
744+ ) ,
745+ [ channel , stateUpdateThrottleInterval , setRead ] ,
746+ ) ;
747+
748+ const copyMessagesStateFromChannelThrottled = useMemo (
749+ ( ) =>
750+ throttle (
751+ ( ) => {
752+ if ( channel ) {
753+ copyMessagesStateFromChannel ( channel ) ;
754+ }
755+ } ,
756+ newMessageStateUpdateThrottleInterval ,
757+ throttleOptions ,
758+ ) ,
759+ [ channel , newMessageStateUpdateThrottleInterval , copyMessagesStateFromChannel ] ,
760+ ) ;
742761
743762 const copyChannelState = useMemo (
744763 ( ) =>
@@ -749,10 +768,10 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
749768 copyMessagesStateFromChannel ( channel ) ;
750769 }
751770 } ,
752- copyChannelStateThrottlingTime ,
771+ stateUpdateThrottleInterval ,
753772 throttleOptions ,
754773 ) ,
755- [ channel , copyChannelStateThrottlingTime , copyMessagesStateFromChannel , copyStateFromChannel ] ,
774+ [ stateUpdateThrottleInterval , channel , copyStateFromChannel , copyMessagesStateFromChannel ] ,
756775 ) ;
757776
758777 const handleEvent : EventHandler = ( event ) => {
@@ -809,6 +828,16 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
809828
810829 // only update channel state if the events are not the previously subscribed useEffect's subscription events
811830 if ( channel && channel . initialized ) {
831+ if ( event . type === 'message.new' ) {
832+ copyMessagesStateFromChannelThrottled ( ) ;
833+ return ;
834+ }
835+
836+ if ( event . type === 'message.read' || event . type === 'notification.mark_read' ) {
837+ setReadThrottled ( ) ;
838+ return ;
839+ }
840+
812841 copyChannelState ( ) ;
813842 }
814843 }
@@ -819,7 +848,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
819848 const initChannel = async ( ) => {
820849 setLastRead ( new Date ( ) ) ;
821850 const unreadCount = channel . countUnread ( ) ;
822- if ( ! channel || ! shouldSyncChannel || channel . offlineMode ) {
851+ if ( ! channel || ! shouldSyncChannel ) {
823852 return ;
824853 }
825854 let errored = false ;
@@ -890,20 +919,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
890919 return unsubscribe ;
891920 } , [ channel ?. cid , client ] ) ;
892921
893- /**
894- * Subscription to the Notification mark_read event.
895- */
896- useEffect ( ( ) => {
897- const handleEvent : EventHandler = ( event ) => {
898- if ( channel . cid === event . cid ) {
899- setRead ( channel ) ;
900- }
901- } ;
902-
903- const { unsubscribe } = client . on ( 'notification.mark_read' , handleEvent ) ;
904- return unsubscribe ;
905- } , [ channel , client , setRead ] ) ;
906-
907922 const threadPropsExists = ! ! threadProps ;
908923
909924 useEffect ( ( ) => {
0 commit comments