fix very long processing time in scroll handler
This commit is contained in:
@@ -185,14 +185,20 @@ class MessagesView extends React.Component {
|
||||
|
||||
getVisibleMessageRange(holderRect) {
|
||||
let min = Number.MAX_SAFE_INTEGER, max = -1;
|
||||
this.chatMessageRefs.forEach((ref, seq) => {
|
||||
let visibilityStatus = false;
|
||||
this.chatMessageRefs.every((ref, seq) => {
|
||||
if (ref.current) {
|
||||
const { top, bottom, height } = ref.current.getBoundingClientRect();
|
||||
const visible = top <= holderRect.top ? holderRect.top - top <= height : bottom - holderRect.bottom <= height;
|
||||
if (visible) {
|
||||
visibilityStatus = true;
|
||||
min = Math.min(min, seq);
|
||||
max = Math.max(max, seq);
|
||||
} else if (visibilityStatus) {
|
||||
// The remaining elements are no longer visible, no need to iterate them.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return max >= min ? {min: min, max: max} : {min: 0, max: 0};
|
||||
@@ -577,13 +583,14 @@ class MessagesView extends React.Component {
|
||||
showGoToLastButton: (pos > SHOW_GO_TO_LAST_DIST) && (pos < this.state.scrollPosition),
|
||||
});
|
||||
|
||||
if (this.state.fetchingMessages) {
|
||||
if (this.state.fetchingMessages || this.processingScrollEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.target.scrollTop <= FETCH_PAGE_TRIGGER) {
|
||||
const topic = this.props.tinode.getTopic(this.state.topic);
|
||||
if (topic && topic.isSubscribed()) {
|
||||
this.processingScrollEvent = true;
|
||||
const {min, max} = this.getVisibleMessageRange(event.target.getBoundingClientRect());
|
||||
const gaps = topic.msgHasMoreMessages(min, max, false);
|
||||
if (gaps.length > 0) {
|
||||
@@ -595,6 +602,7 @@ class MessagesView extends React.Component {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.processingScrollEvent = false;
|
||||
}
|
||||
|
||||
/* Mount drag and drop events */
|
||||
|
||||
Reference in New Issue
Block a user