Skip to content

Commit 4b24551

Browse files
committed
perf: handle setting read state separately
1 parent bab5d36 commit 4b24551

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,19 @@ const ChannelWithContext = <
741741
channel,
742742
});
743743

744-
/**
745-
* 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.
746-
* This is done until we remove the newMessageStateUpdateThrottleInterval prop.
747-
*/
744+
const setReadThrottled = useMemo(
745+
() =>
746+
throttle(
747+
() => {
748+
if (channel) {
749+
setRead(channel);
750+
}
751+
},
752+
stateUpdateThrottleInterval,
753+
throttleOptions,
754+
),
755+
[channel, stateUpdateThrottleInterval, setRead],
756+
);
748757

749758
const copyMessagesStateFromChannelThrottled = useMemo(
750759
() =>
@@ -776,7 +785,6 @@ const ChannelWithContext = <
776785
);
777786

778787
const handleEvent: EventHandler<StreamChatGenerics> = (event) => {
779-
console.log('SHOULD SYNC: ', event.type);
780788
if (shouldSyncChannel) {
781789
/**
782790
* Ignore user.watching.start and user.watching.stop as we should not copy the entire state when
@@ -836,6 +844,7 @@ const ChannelWithContext = <
836844
}
837845

838846
if (event.type === 'message.read' || event.type === 'notification.mark_read') {
847+
setReadThrottled();
839848
return;
840849
}
841850

@@ -920,20 +929,6 @@ const ChannelWithContext = <
920929
return unsubscribe;
921930
}, [channel?.cid, client]);
922931

923-
/**
924-
* Subscription to the Notification mark_read event.
925-
*/
926-
useEffect(() => {
927-
const handleEvent: EventHandler<StreamChatGenerics> = (event) => {
928-
if (channel.cid === event.cid) {
929-
setRead(channel);
930-
}
931-
};
932-
933-
const { unsubscribe } = client.on('notification.mark_read', handleEvent);
934-
return unsubscribe;
935-
}, [channel, client, setRead]);
936-
937932
const threadPropsExists = !!threadProps;
938933

939934
useEffect(() => {
@@ -1191,7 +1186,7 @@ const ChannelWithContext = <
11911186
}
11921187

11931188
channel.state.addMessageSorted(updatedMessage, true);
1194-
copyMessagesStateFromChannelThrottled();
1189+
copyMessagesStateFromChannel(channel);
11951190

11961191
if (thread && updatedMessage.parent_id) {
11971192
extraState.threadMessages = channel.state.threads[updatedMessage.parent_id] || [];
@@ -1206,7 +1201,7 @@ const ChannelWithContext = <
12061201
if (channel) {
12071202
channel.state.removeMessage(oldMessage);
12081203
channel.state.addMessageSorted(newMessage, true);
1209-
copyMessagesStateFromChannelThrottled();
1204+
copyMessagesStateFromChannel(channel);
12101205

12111206
if (thread && newMessage.parent_id) {
12121207
const threadMessages = channel.state.threads[newMessage.parent_id] || [];
@@ -1506,7 +1501,7 @@ const ChannelWithContext = <
15061501
) => {
15071502
if (channel) {
15081503
channel.state.removeMessage(message);
1509-
copyMessagesStateFromChannelThrottled();
1504+
copyMessagesStateFromChannel(channel);
15101505

15111506
if (thread) {
15121507
setThreadMessages(channel.state.threads[thread.id] || []);
@@ -1546,7 +1541,7 @@ const ChannelWithContext = <
15461541
user: client.user,
15471542
});
15481543

1549-
copyMessagesStateFromChannelThrottled();
1544+
copyMessagesStateFromChannel(channel);
15501545

15511546
const sendReactionResponse = await DBSyncManager.queueTask<StreamChatGenerics>({
15521547
client,
@@ -1632,7 +1627,7 @@ const ChannelWithContext = <
16321627
user: client.user,
16331628
});
16341629

1635-
copyMessagesStateFromChannelThrottled();
1630+
copyMessagesStateFromChannel(channel);
16361631

16371632
await DBSyncManager.queueTask<StreamChatGenerics>({
16381633
client,

0 commit comments

Comments
 (0)