Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
amk221 committed Mar 8, 2024
1 parent 0afb6e2 commit 4fb4788
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions addon/components/infinite-scroller.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class InfiniteScrollerComponent extends Component {
@tracked scrollState = {};
@tracked lastScrollState = {};

debug;
debug = true;
scroller;
debounceId;

Expand All @@ -32,6 +32,10 @@ export default class InfiniteScrollerComponent extends Component {
return this.args.percent ?? 100;
}

get shouldLoadMore() {
return this.scrollState.reachedBottom && !this.isLoading;
}

get normalisedScrollerElement() {
if (this.scroller instanceof Document) {
return this.scroller.documentElement;
Expand Down Expand Up @@ -78,16 +82,12 @@ export default class InfiniteScrollerComponent extends Component {
}

_checkShouldLoadMore() {
this.lastScrollState = this.scrollState;
this.scrollState = this._getScrollState();

const shouldLoadMore = this.scrollState.reachedBottom && !this.isLoading;

this._debug({
...this.scrollState,
shouldLoadMore
});
this._debug();

if (shouldLoadMore) {
if (this.shouldLoadMore) {
this.loadMore();
}
}
Expand All @@ -99,14 +99,19 @@ export default class InfiniteScrollerComponent extends Component {

this.scrollState = this._getScrollState();

this._debug(this.scrollState);
this._debug();
}

_debug(state) {
_debug() {
if (!this.debug) {
return;
}

const state = {
...this.scrollState,
shouldLoadMore: this.shouldLoadMore
};

console.table([state]); // eslint-disable-line
}

Expand All @@ -120,6 +125,8 @@ export default class InfiniteScrollerComponent extends Component {
const percent = this.percent;
const percentScrolled = round((scrollTop / bottom) * 100);
const reachedBottom = percentScrolled >= percent;
const scrollingDown = element.scrollTop > this.lastScrollState.scrollTop;
const direction = scrollingDown ? 'DOWN' : 'UP';

return {
isScrollable,
Expand All @@ -129,7 +136,8 @@ export default class InfiniteScrollerComponent extends Component {
bottom,
percent,
percentScrolled,
reachedBottom
reachedBottom,
direction
};
}

Expand Down

0 comments on commit 4fb4788

Please sign in to comment.