Load comments when approaching viewport

Seeing the spinner too much can be annoying.

- This approach works, but currently, when the list is very long, something is taking up resources and the handler couldn't be processed, so the effect is lost (still seeing the spinner). See 6473.

- Since we are now prefetching, bumped the debounceMs a bit.
This commit is contained in:
infinite-persistence 2021-07-14 09:45:18 +08:00
parent 2f92ce8bb4
commit 3d63ce4666
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0

View file

@ -14,7 +14,15 @@ import { ENABLE_COMMENT_REACTIONS } from 'config';
import Empty from 'component/common/empty'; import Empty from 'component/common/empty';
import debounce from 'util/debounce'; import debounce from 'util/debounce';
const DEBOUNCE_SCROLL_HANDLER_MS = 50; const DEBOUNCE_SCROLL_HANDLER_MS = 200;
function scaleToDevicePixelRatio(value) {
const devicePixelRatio = window.devicePixelRatio || 1.0;
if (devicePixelRatio < 1.0) {
return Math.ceil(value / devicePixelRatio);
}
return Math.ceil(value * devicePixelRatio);
}
type Props = { type Props = {
allCommentIds: any, allCommentIds: any,
@ -156,16 +164,25 @@ function CommentList(props: Props) {
const handleCommentScroll = debounce(() => { const handleCommentScroll = debounce(() => {
// $FlowFixMe // $FlowFixMe
const rect = spinnerRef.current.getBoundingClientRect(); const rect = spinnerRef.current.getBoundingClientRect();
// $FlowFixMe
const windowH = window.innerHeight || document.documentElement.clientHeight;
// $FlowFixMe
const windowW = window.innerWidth || document.documentElement.clientWidth;
const Y_OFFSET_PX = 1000;
const isApproachingViewport = Y_OFFSET_PX && rect.top < windowH + scaleToDevicePixelRatio(Y_OFFSET_PX);
const isInViewport = const isInViewport =
rect.width > 0 &&
rect.height > 0 &&
rect.bottom >= 0 && rect.bottom >= 0 &&
rect.right >= 0 && rect.right >= 0 &&
// $FlowFixMe // $FlowFixMe
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && rect.top <= windowH &&
// $FlowFixMe // $FlowFixMe
rect.left <= (window.innerWidth || document.documentElement.clientWidth); rect.left <= windowW;
if (isInViewport && page < topLevelTotalPages) { if ((isInViewport || isApproachingViewport) && page < topLevelTotalPages) {
setPage(page + 1); setPage(page + 1);
} }
}, DEBOUNCE_SCROLL_HANDLER_MS); }, DEBOUNCE_SCROLL_HANDLER_MS);