Skip to content

Commit aa353f1

Browse files
committed
fix: throttle logic for the copy message state
1 parent df28d8e commit aa353f1

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ export type ChannelPropsWithContext<
437437
* Load the channel at a specified message instead of the most recent message.
438438
*/
439439
messageId?: string;
440+
/**
441+
* @deprecated
442+
* The time interval for throttling while updating the message state
443+
*/
440444
newMessageStateUpdateThrottleInterval?: number;
441445
overrideOwnCapabilities?: Partial<OwnCapabilitiesContextValue>;
442446
stateUpdateThrottleInterval?: number;
@@ -594,7 +598,7 @@ const ChannelWithContext = <
594598
myMessageTheme,
595599
NetworkDownIndicator = NetworkDownIndicatorDefault,
596600
// TODO: Think about this one
597-
// newMessageStateUpdateThrottleInterval = defaultThrottleInterval,
601+
newMessageStateUpdateThrottleInterval = defaultThrottleInterval,
598602
numberOfLines = 5,
599603
onChangeText,
600604
onLongPressMessage,
@@ -684,6 +688,15 @@ const ChannelWithContext = <
684688
channel,
685689
});
686690

691+
/**
692+
* 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.
693+
* This is done until we remove the newMessageStateUpdateThrottleInterval prop.
694+
*/
695+
const copyChannelStateThrottlingTime =
696+
newMessageStateUpdateThrottleInterval > stateUpdateThrottleInterval
697+
? newMessageStateUpdateThrottleInterval
698+
: stateUpdateThrottleInterval;
699+
687700
const copyChannelState = useRef(
688701
throttle(
689702
() => {
@@ -692,7 +705,7 @@ const ChannelWithContext = <
692705
copyMessagesStateFromChannel(channel);
693706
}
694707
},
695-
stateUpdateThrottleInterval,
708+
copyChannelStateThrottlingTime,
696709
throttleOptions,
697710
),
698711
).current;
@@ -728,6 +741,7 @@ const ChannelWithContext = <
728741
};
729742

730743
useEffect(() => {
744+
let listener: ReturnType<typeof channel.on>;
731745
const initChannel = async () => {
732746
if (!channel || !shouldSyncChannel || channel.offlineMode) return;
733747
let errored = false;
@@ -755,15 +769,15 @@ const ChannelWithContext = <
755769
) {
756770
await loadChannelAtFirstUnreadMessage({ setTargetedMessage });
757771
}
758-
channel.on(handleEvent);
772+
listener = channel.on(handleEvent);
759773
};
760774

761775
initChannel();
762776

763777
return () => {
764778
copyChannelState.cancel();
765779
loadMoreThreadFinished.cancel();
766-
channel.off(handleEvent);
780+
listener.unsubscribe();
767781
};
768782
// eslint-disable-next-line react-hooks/exhaustive-deps
769783
}, [channel.cid, messageId, shouldSyncChannel]);

0 commit comments

Comments
 (0)