Skip to content

Commit c44384a

Browse files
Fixed typecasting and added big comment about message pagination logic
1 parent aea87c7 commit c44384a

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/components/MessageList/MessageList.tsx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,29 @@ const MessageListWithContext = <
588588
return null;
589589
};
590590

591+
//
592+
// We are keeping full control on message pagination, and not relying on react-native for it.
593+
// The reasons being,
594+
// 1. FlatList doesn't support onStartReached prop
595+
// 2. `onEndReached` function prop available on react-native, gets executed
596+
// once per content length (and thats actually a nice optimization strategy).
597+
// But it also means, we always need to priotizie onEndReached above our
598+
// logic for `onStartReached`.
599+
// 3. `onEndReachedThreshold` prop decides - at which scroll position to call `onEndReached`.
600+
// Its a factor of content length (which is necessary for "real" infinite scroll). But on
601+
// the other hand, it also makes calls to `onEndReached` (and this `channel.query`) way
602+
// too early during scroll, which we don't really need. So we are going to instead
603+
// keep some fixed offset distance, to decide when to call `loadMore` or `loadMoreRecent`.
604+
//
605+
// We are still gonna keep the optimization, which react-native does - only call onEndReached
606+
// once per content length.
607+
//
608+
609+
/**
610+
* 1. Makes a call to `loadMoreRecent` function, which queries more recent messages.
611+
* 2. Ensures that we call `loadMoreRecent`, once per content length
612+
* 3. If the call to `loadMore` is in progress, we wait for it to finish to make sure scroll doesn't jump.
613+
*/
591614
const maybeCallOnStartReached = () => {
592615
// If onStartReached has already been called for given data length, then ignore.
593616
if (
@@ -610,17 +633,18 @@ const MessageListWithContext = <
610633
// If onEndReached is in progress, better to wait for it to finish for smooth UX
611634
if (onEndReachedInPromise.current) {
612635
onEndReachedInPromise.current.finally(() => {
613-
onStartReachedInPromise.current = (loadMoreRecent() as Promise<void>).then(
614-
callback,
615-
);
636+
onStartReachedInPromise.current = loadMoreRecent().then(callback);
616637
});
617638
} else {
618-
onStartReachedInPromise.current = (loadMoreRecent() as Promise<void>).then(
619-
callback,
620-
);
639+
onStartReachedInPromise.current = loadMoreRecent().then(callback);
621640
}
622641
};
623642

643+
/**
644+
* 1. Makes a call to `loadMore` function, which queries more older messages.
645+
* 2. Ensures that we call `loadMore`, once per content length
646+
* 3. If the call to `loadMoreRecent` is in progress, we wait for it to finish to make sure scroll doesn't jump.
647+
*/
624648
const maybeCallOnEndReached = () => {
625649
// If onEndReached has already been called for given messageList length, then ignore.
626650
if (
@@ -647,13 +671,11 @@ const MessageListWithContext = <
647671
onStartReachedInPromise.current.finally(() => {
648672
onEndReachedInPromise.current = (threadList
649673
? loadMoreThread()
650-
: (loadMore() as Promise<void>)
674+
: loadMore()
651675
).then(callback);
652676
});
653677
} else {
654-
onEndReachedInPromise.current = (loadMore() as Promise<void>).then(
655-
callback,
656-
);
678+
onEndReachedInPromise.current = loadMore().then(callback);
657679
}
658680
};
659681

src/contexts/messagesContext/MessagesContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export type MessagesContextValue<
136136
InlineUnreadIndicator: React.ComponentType;
137137
loadingMore: boolean;
138138
loadingMoreRecent: boolean;
139-
loadMore: () => void | Promise<void>;
140-
loadMoreRecent: () => void | Promise<void>;
139+
loadMore: () => Promise<void>;
140+
loadMoreRecent: () => Promise<void>;
141141
Message: React.ComponentType<MessageProps<At, Ch, Co, Ev, Me, Re, Us>>;
142142
/**
143143
* UI component for MessageAvatar

0 commit comments

Comments
 (0)