Skip to content

Commit 0ba18d3

Browse files
committed
perf: throttle message copying updates
1 parent e787038 commit 0ba18d3

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

package/expo-package/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4733,10 +4733,10 @@ [email protected], stream-buffers@~2.2.0:
47334733
resolved "https://registry.yarnpkg.com/stream-buffers/-/stream-buffers-2.2.0.tgz#91d5f5130d1cef96dcfa7f726945188741d09ee4"
47344734
integrity sha512-uyQK/mx5QjHun80FLJTfaWE7JtwfRMKBLkMne6udYOmvH0CawotVa7TfgYHzAnpphn4+TweIx1QKMnRIbipmUg==
47354735

4736-
4737-
version "6.7.1"
4738-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-6.7.1.tgz#37cdfbc5c7f8a5bac5634b954da4bbdcd2fb95f2"
4739-
integrity sha512-4ePEMt1W+iw3zUulSDRFO9Nt4HPa8kW6wJ3Qv+ZN+y886rvcUuTuH18MFrdrJtHYb+UxCU+X3oz++3qzq7Jzxw==
4736+
4737+
version "6.7.2"
4738+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-6.7.2.tgz#955e048b80c55175db084ccbb8519f52ef4bb00c"
4739+
integrity sha512-WJFOCfQ7Xpn8Lr4AE6hUh4Qhrn1eGzsoAcKmL8eSoB/etxdNllOyZ3zrwvZgyy+KIEg9bcX4y+3OWtdKW6qfsA==
47404740
dependencies:
47414741
"@gorhom/bottom-sheet" "^5.1.1"
47424742
dayjs "1.10.5"

package/native-package/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,10 +3409,10 @@ statuses@~1.5.0:
34093409
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
34103410
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==
34113411

3412-
3413-
version "6.7.1"
3414-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-6.7.1.tgz#37cdfbc5c7f8a5bac5634b954da4bbdcd2fb95f2"
3415-
integrity sha512-4ePEMt1W+iw3zUulSDRFO9Nt4HPa8kW6wJ3Qv+ZN+y886rvcUuTuH18MFrdrJtHYb+UxCU+X3oz++3qzq7Jzxw==
3412+
3413+
version "6.7.2"
3414+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-6.7.2.tgz#955e048b80c55175db084ccbb8519f52ef4bb00c"
3415+
integrity sha512-WJFOCfQ7Xpn8Lr4AE6hUh4Qhrn1eGzsoAcKmL8eSoB/etxdNllOyZ3zrwvZgyy+KIEg9bcX4y+3OWtdKW6qfsA==
34163416
dependencies:
34173417
"@gorhom/bottom-sheet" "^5.1.1"
34183418
dayjs "1.10.5"

package/src/components/Channel/Channel.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,20 @@ const ChannelWithContext = <
745745
* 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.
746746
* This is done until we remove the newMessageStateUpdateThrottleInterval prop.
747747
*/
748-
const copyChannelStateThrottlingTime =
749-
newMessageStateUpdateThrottleInterval > stateUpdateThrottleInterval
750-
? newMessageStateUpdateThrottleInterval
751-
: stateUpdateThrottleInterval;
748+
749+
const copyMsgsStateFromChannel = useMemo(
750+
() =>
751+
throttle(
752+
() => {
753+
if (channel) {
754+
copyMessagesStateFromChannel(channel);
755+
}
756+
},
757+
newMessageStateUpdateThrottleInterval,
758+
throttleOptions,
759+
),
760+
[channel, newMessageStateUpdateThrottleInterval, copyMessagesStateFromChannel],
761+
);
752762

753763
const copyChannelState = useMemo(
754764
() =>
@@ -759,10 +769,10 @@ const ChannelWithContext = <
759769
copyMessagesStateFromChannel(channel);
760770
}
761771
},
762-
copyChannelStateThrottlingTime,
772+
stateUpdateThrottleInterval,
763773
throttleOptions,
764774
),
765-
[channel, copyChannelStateThrottlingTime, copyMessagesStateFromChannel, copyStateFromChannel],
775+
[stateUpdateThrottleInterval, channel, copyStateFromChannel, copyMessagesStateFromChannel],
766776
);
767777

768778
const handleEvent: EventHandler<StreamChatGenerics> = (event) => {
@@ -819,6 +829,15 @@ const ChannelWithContext = <
819829

820830
// only update channel state if the events are not the previously subscribed useEffect's subscription events
821831
if (channel && channel.initialized) {
832+
if (event.type === 'message.new') {
833+
copyMsgsStateFromChannel();
834+
return;
835+
}
836+
837+
if (event.type === 'message.read') {
838+
return;
839+
}
840+
822841
copyChannelState();
823842
}
824843
}

0 commit comments

Comments
 (0)