From 62c812c0aace419676d535a48a198fe805f9ae49 Mon Sep 17 00:00:00 2001 From: Wadim Kalmykov Date: Tue, 13 Oct 2020 23:23:16 +0700 Subject: [PATCH] move load posts into separate function --- js/src/forum/components/PostStream.js | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/js/src/forum/components/PostStream.js b/js/src/forum/components/PostStream.js index 7617bb980b..546b921544 100644 --- a/js/src/forum/components/PostStream.js +++ b/js/src/forum/components/PostStream.js @@ -142,13 +142,28 @@ export default class PostStream extends Component { } /** - * When the window is scrolled, check if either extreme of the post stream is - * in the viewport, and if so, trigger loading the next/previous page. * * @param {Integer} top */ onscroll(top = window.pageYOffset) { if (this.stream.paused) return; + this.loadPostsWhenNeeded(top); + + // Throttle calculation of our position (start/end numbers of posts in the + // viewport) to 100ms. + clearTimeout(this.calculatePositionTimeout); + this.calculatePositionTimeout = setTimeout(this.calculatePosition.bind(this, top), 100); + + this.updateScrubber(top); + } + + /** + * Check if either extreme of the post stream is in the viewport, + * and if so, trigger loading the next/previous page. + * + * @param {Integer} top + */ + loadPostsWhenNeeded(top = window.pageYOffset) { const marginTop = this.getMarginTop(); const viewportHeight = $(window).height() - marginTop; const viewportTop = top + marginTop; @@ -169,13 +184,6 @@ export default class PostStream extends Component { this.stream.loadNext(); } } - - // Throttle calculation of our position (start/end numbers of posts in the - // viewport) to 100ms. - clearTimeout(this.calculatePositionTimeout); - this.calculatePositionTimeout = setTimeout(this.calculatePosition.bind(this, top), 100); - - this.updateScrubber(top); } updateScrubber(top = window.pageYOffset) { @@ -394,8 +402,8 @@ export default class PostStream extends Component { this.calculatePosition(); this.stream.paused = false; - // Run the onscroll handler to check if we need to load more posts after scrolling. - this.onscroll(); + // Check if we need to load more posts after scrolling. + this.loadPostsWhenNeeded(); }); }