Skip to content

Commit ae7a225

Browse files
committed
fix: optionally disable raf coalescing
1 parent fa3e062 commit ae7a225

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

package/src/components/MessageList/MessageList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ type MessageListPropsWithContext = Pick<
226226
* If true, the message list will be used in a live-streaming scenario.
227227
* This flag is used to make sure that the auto scroll behaves well, if multiple messages are received.
228228
*
229-
* This flag is experimental and is subject to change.
229+
* This flag is experimental and is subject to change. Please test thoroughly before using it.
230+
*
231+
* @experimental
230232
*/
231233
isLiveStreaming?: boolean;
232234
};
@@ -1238,7 +1240,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
12381240
scrollEventThrottle={isLiveStreaming ? 16 : undefined}
12391241
showsVerticalScrollIndicator={false}
12401242
// @ts-expect-error react-native internal
1241-
strictMode={isLiveStreaming ? true : false}
1243+
strictMode={isLiveStreaming}
12421244
style={flatListStyle}
12431245
testID='message-flat-list'
12441246
viewabilityConfig={flatListViewabilityConfig}

package/src/components/MessageList/hooks/useMessageList.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ export const useMessageList = (params: UseMessageListParams) => {
112112
return newMessageList;
113113
}, [client.userID, deletedMessagesVisibilityType, messageList]);
114114

115-
const rafColeasedProcessedMessageList = useRAFCoalescedValue(processedMessageList);
116-
117-
const data = isLiveStreaming ? rafColeasedProcessedMessageList : processedMessageList;
115+
const data = useRAFCoalescedValue(processedMessageList, isLiveStreaming);
118116

119117
return useMemo(
120118
() => ({

package/src/hooks/useRAFCoalescedValue.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ import { useEffect, useRef, useState } from 'react';
3636
* - Do not cancel/reschedule on prop changes; cancellation is handled on unmount only.
3737
*
3838
* @param value The upstream value that may change multiple times within a single frame.
39+
* @param isEnabled Determines whether the hook should be run or not (useful for cases where
40+
* we want to conditionally use RAF when certain feature feature flags are enabled). If `false`,
41+
* it will simply pass the data through (maintaining the reference as well).
3942
* @returns A value that updates **at most once per frame** with the latest input.
4043
*/
41-
export const useRAFCoalescedValue = <S>(value: S): S => {
44+
export const useRAFCoalescedValue = <S>(value: S, isEnabled: boolean | undefined): S => {
4245
const [emitted, setEmitted] = useState<S>(value);
4346
const pendingRef = useRef<S>(value);
4447
const rafIdRef = useRef<number | null>(null);
4548

4649
// If `value` changes, schedule a single RAF to publish the latest one.
4750
useEffect(() => {
48-
if (value === pendingRef.current) return;
51+
if (value === pendingRef.current || !isEnabled) return;
4952
pendingRef.current = value;
5053

5154
// already scheduled the next frame, skip
@@ -57,7 +60,7 @@ export const useRAFCoalescedValue = <S>(value: S): S => {
5760
};
5861

5962
rafIdRef.current = requestAnimationFrame(run);
60-
}, [value]);
63+
}, [value, isEnabled]);
6164

6265
useEffect(() => {
6366
return () => {
@@ -69,5 +72,5 @@ export const useRAFCoalescedValue = <S>(value: S): S => {
6972
};
7073
}, []);
7174

72-
return emitted;
75+
return isEnabled ? emitted : value;
7376
};

0 commit comments

Comments
 (0)