Skip to content

Commit a434c87

Browse files
authored
feat: use server defaults for querying channels (#3403)
## 🎯 Goal This PR is a reflection of [this one](GetStream/stream-chat-react#2943) from the react SDK for V8. ## πŸ›  Implementation details <!-- Provide a description of the implementation --> ## 🎨 UI Changes <!-- Add relevant screenshots --> <details> <summary>iOS</summary> <table> <thead> <tr> <td>Before</td> <td>After</td> </tr> </thead> <tbody> <tr> <td> <!--<img src="" /> --> </td> <td> <!--<img src="" /> --> </td> </tr> </tbody> </table> </details> <details> <summary>Android</summary> <table> <thead> <tr> <td>Before</td> <td>After</td> </tr> </thead> <tbody> <tr> <td> <!--<img src="" /> --> </td> <td> <!--<img src="" /> --> </td> </tr> </tbody> </table> </details> ## πŸ§ͺ Testing <!-- Explain how this change can be tested (or why it can't be tested) --> ## β˜‘οΈ Checklist - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required) - [ ] PR targets the `develop` branch - [ ] Documentation is updated - [ ] New code is tested in main example apps, including all possible scenarios - [ ] SampleApp iOS and Android - [ ] Expo iOS and Android
1 parent fce9014 commit a434c87

9 files changed

Lines changed: 64 additions & 24 deletions

File tree

β€Žexamples/SampleApp/yarn.lockβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6634,6 +6634,11 @@ lodash-es@4.17.21:
66346634
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
66356635
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
66366636

6637+
lodash-es@4.17.23:
6638+
version "4.17.23"
6639+
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.23.tgz#58c4360fd1b5d33afc6c0bbd3d1149349b1138e0"
6640+
integrity sha512-kVI48u3PZr38HdYz98UmfPnXl2DXrpdctLrFLCd3kOx1xUkOmpFPx7gCWWM5MPkL/fD8zb+Ph0QzjGFs4+hHWg==
6641+
66376642
lodash.camelcase@^4.3.0:
66386643
version "4.3.0"
66396644
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"

β€Žexamples/TypeScriptMessaging/App.tsxβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const options = {
3737
presence: true,
3838
state: true,
3939
watch: true,
40-
limit: 30,
4140
};
4241

4342
I18nManager.forceRTL(false);

β€Žpackage/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"path": "0.12.7",
8181
"react-native-markdown-package": "1.8.2",
8282
"react-native-url-polyfill": "^2.0.0",
83-
"stream-chat": "^9.27.2",
83+
"stream-chat": "^9.33.0",
8484
"use-sync-external-store": "^1.5.0"
8585
},
8686
"peerDependencies": {

β€Žpackage/src/components/Channel/Channel.tsxβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,8 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
11471147
if (channelMessagesState?.messages) {
11481148
await channel?.watch({
11491149
messages: {
1150-
limit: channelMessagesState.messages.length + 30,
1150+
// Do we want to reduce this to the default as well ?
1151+
limit: channelMessagesState.messages.length,
11511152
},
11521153
});
11531154
channel.offlineMode = false;

β€Žpackage/src/components/Channel/__tests__/useMessageListPagination.test.jsβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ describe('useMessageListPagination', () => {
161161

162162
await waitFor(() => {
163163
expect(queryFn).toHaveBeenCalledWith({
164-
messages: { id_lt: messages[0].id, limit: 20 },
165-
watchers: { limit: 20 },
164+
messages: { id_lt: messages[0].id },
165+
watchers: {},
166166
});
167167
expect(result.current.state.hasMore).toBe(true);
168168
expect(result.current.state.messages.length).toBe(40);
@@ -252,8 +252,8 @@ describe('useMessageListPagination', () => {
252252

253253
await waitFor(() => {
254254
expect(queryFn).toHaveBeenCalledWith({
255-
messages: { id_gt: messages[messages.length - 1].id, limit: 10 },
256-
watchers: { limit: 10 },
255+
messages: { id_gt: messages[messages.length - 1].id },
256+
watchers: {},
257257
});
258258
expect(result.current.state.hasMore).toBe(true);
259259
expect(result.current.state.messages.length).toBe(40);

β€Žpackage/src/components/Channel/hooks/useMessageListPagination.tsxβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const useMessageListPagination = ({ channel }: { channel: Channel }) => {
7474
/**
7575
* This function loads more messages before the first message in current channel state.
7676
*/
77-
const loadMore = useStableCallback(async (limit: number = 20) => {
77+
const loadMore = useStableCallback(async (limit?: number) => {
7878
if (!channel.state.messagePagination.hasPrev) {
7979
return;
8080
}
@@ -103,7 +103,7 @@ export const useMessageListPagination = ({ channel }: { channel: Channel }) => {
103103
/**
104104
* This function loads more messages after the most recent message in current channel state.
105105
*/
106-
const loadMoreRecent = useStableCallback(async (limit: number = 10) => {
106+
const loadMoreRecent = useStableCallback(async (limit?: number) => {
107107
if (!channel.state.messagePagination.hasNext) {
108108
return;
109109
}

β€Žpackage/src/components/ChannelList/hooks/usePaginatedChannels.tsβ€Ž

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import { useChatContext } from '../../../contexts/chatContext/ChatContext';
1313
import { useStateStore } from '../../../hooks';
1414
import { useIsMountedRef } from '../../../hooks/useIsMountedRef';
1515

16-
import { MAX_QUERY_CHANNELS_LIMIT } from '../utils';
17-
1816
type Parameters = {
1917
channelManager: ChannelManager;
2018
enableOfflineSupport: boolean;
@@ -24,10 +22,6 @@ type Parameters = {
2422
sort: ChannelSort;
2523
};
2624

27-
const DEFAULT_OPTIONS = {
28-
message_limit: 10,
29-
};
30-
3125
const RETRY_INTERVAL_IN_MS = 5000;
3226

3327
type QueryType = 'queryLocalDB' | 'reload' | 'refresh' | 'loadChannels';
@@ -46,7 +40,7 @@ export const usePaginatedChannels = ({
4640
channelManager,
4741
enableOfflineSupport,
4842
filters = {},
49-
options = DEFAULT_OPTIONS,
43+
options = {},
5044
sort = {},
5145
}: Parameters) => {
5246
const [staticChannelsActive, setStaticChannelsActive] = useState<boolean>(false);
@@ -99,7 +93,6 @@ export const usePaginatedChannels = ({
9993
setActiveQueryType(queryType);
10094

10195
const newOptions = {
102-
limit: options?.limit ?? MAX_QUERY_CHANNELS_LIMIT,
10396
offset: 0,
10497
...options,
10598
};

β€Žpackage/src/components/MessageList/MessageList.tsxβ€Ž

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
22
import {
33
FlatListProps,
44
FlatList as FlatListType,
5+
LayoutChangeEvent,
56
ScrollViewProps,
67
StyleSheet,
78
View,
89
ViewabilityConfig,
910
ViewToken,
1011
} from 'react-native';
1112

13+
import debounce from 'lodash/debounce';
1214
import type { Channel, Event, LocalMessage, MessageResponse } from 'stream-chat';
1315

1416
import { useMessageList } from './hooks/useMessageList';
@@ -327,8 +329,8 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
327329

328330
const renderItem = useCallback(
329331
({ item: message, index }: { item: LocalMessage; index: number }) => {
330-
const previousMessage = processedMessageListRef.current[index + 1];
331-
const nextMessage = processedMessageListRef.current[index - 1];
332+
const previousMessage = processedMessageList[index + 1];
333+
const nextMessage = processedMessageList[index - 1];
332334
return (
333335
<MessageWrapper
334336
message={message}
@@ -337,7 +339,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
337339
/>
338340
);
339341
},
340-
[processedMessageListRef],
342+
[processedMessageList],
341343
);
342344

343345
const messageListLengthBeforeUpdate = useRef(0);
@@ -1126,6 +1128,44 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
11261128
[additionalFlatListProps?.contentContainerStyle, contentContainer],
11271129
);
11281130

1131+
const viewportHeightRef = useRef<number>(undefined);
1132+
1133+
/**
1134+
* This debounced callback makes sure that if the current number of messages do not
1135+
* fill our screen, we load more messages continuously until we cover enough ground.
1136+
*/
1137+
const debouncedPrefillMessages = useMemo(
1138+
() =>
1139+
debounce(
1140+
(viewportHeight: number, contentHeight: number) => {
1141+
if (viewportHeight >= contentHeight) {
1142+
maybeCallOnEndReached();
1143+
}
1144+
},
1145+
500,
1146+
{
1147+
leading: false,
1148+
trailing: true,
1149+
},
1150+
),
1151+
[maybeCallOnEndReached],
1152+
);
1153+
1154+
const onContentSizeChange = useStableCallback((width: number, height: number) => {
1155+
if (additionalFlatListProps?.onContentSizeChange) {
1156+
additionalFlatListProps.onContentSizeChange(width, height);
1157+
}
1158+
1159+
debouncedPrefillMessages(viewportHeightRef.current ?? 0, height);
1160+
});
1161+
1162+
const onLayout = useStableCallback((event: LayoutChangeEvent) => {
1163+
if (additionalFlatListProps?.onLayout) {
1164+
additionalFlatListProps.onLayout(event);
1165+
}
1166+
viewportHeightRef.current = event.nativeEvent.layout.height;
1167+
});
1168+
11291169
if (!FlatList) {
11301170
return null;
11311171
}
@@ -1168,6 +1208,8 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
11681208
*/
11691209
maintainVisibleContentPosition={maintainVisibleContentPosition}
11701210
maxToRenderPerBatch={30}
1211+
onContentSizeChange={onContentSizeChange}
1212+
onLayout={onLayout}
11711213
onMomentumScrollEnd={onUserScrollEvent}
11721214
onScroll={handleScroll}
11731215
onScrollBeginDrag={onScrollBeginDrag}

β€Žpackage/yarn.lockβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8335,10 +8335,10 @@ stdin-discarder@^0.2.2:
83358335
resolved "https://registry.yarnpkg.com/stdin-discarder/-/stdin-discarder-0.2.2.tgz#390037f44c4ae1a1ae535c5fe38dc3aba8d997be"
83368336
integrity sha512-UhDfHmA92YAlNnCfhmq0VeNL5bDbiZGg7sZ2IvPsXubGkiNa9EC+tUTsjBRsYUAz87btI6/1wf4XoVvQ3uRnmQ==
83378337

8338-
stream-chat@^9.27.2:
8339-
version "9.27.2"
8340-
resolved "https://registry.yarnpkg.com/stream-chat/-/stream-chat-9.27.2.tgz#5b41173e513f3606c47c93f391693b589e663968"
8341-
integrity sha512-OdALDzg8lO8CAdl8deydJ1+O4wJ7mM9dPLeCwDppq/OQ4aFIS9X38P+IdXPcOCsgSS97UoVUuxD2/excC5PEeg==
8338+
stream-chat@^9.33.0:
8339+
version "9.33.0"
8340+
resolved "https://registry.yarnpkg.com/stream-chat/-/stream-chat-9.33.0.tgz#3da6582ade9a22a808abe7f443b08137705bf792"
8341+
integrity sha512-JdeR6Nq2QEKBIKZsW8wnGa04pTHCiWmdIOqvWUVJ4DtmLzJ9oBBeBnHvPx1Q+RKbvpZqfjwvYaCwKY5ZFq+FxQ==
83428342
dependencies:
83438343
"@types/jsonwebtoken" "^9.0.8"
83448344
"@types/ws" "^8.5.14"

0 commit comments

Comments
Β (0)