Skip to content

Commit

Permalink
move load posts into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
w-4 authored and askvortsov1 committed Oct 15, 2020
1 parent 3d5feef commit 62c812c
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
});
}

Expand Down

0 comments on commit 62c812c

Please sign in to comment.