Skip to content

Commit

Permalink
fix(Infinite): fix Infinite recycle point
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Mar 9, 2018
1 parent fc5aea0 commit e5b8af3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/Infinite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Infinite {
this.options = Object.assign({
useRecycle: true,
threshold: 100,
horizontal: false,
}, options);
this._items = itemManger;
this.clear();
Expand Down Expand Up @@ -89,19 +90,22 @@ class Infinite {
const threshold = this.options.threshold;
const endScrollPos = scrollPos + size;
const targetItem = items.getData(isForward ? endCursor : startCursor);
const outlines = targetItem.outlines[isForward ? "end" : "start"];
const targetPos = Math[isForward ? "min" : "max"](...outlines);
// const outlines = targetItem.outlines[isForward ? "end" : "start"];
const edgeItem = targetItem.items[targetItem.outlines[isForward ? "endIndex" : "startIndex"]];
const sizeName = this.options.horizontal ? "width" : "height";
const posName = this.options.horizontal ? "left" : "top";
const edgeItemSize = edgeItem.rect[sizeName] || edgeItem.size[sizeName];
const edgePos = isForward ? edgeItem.rect[posName] : edgeItem.rect[posName] + edgeItemSize;

// Math[isForward ? "min" : "max"](...outlines);

if (isForward) {
if (endScrollPos >= targetPos - threshold) {
if (endScrollPos >= edgePos - threshold) {
append && append({cache: length > endCursor + 1 && items.getData(endCursor + 1)});
}
} else if (scrollPos <= targetPos + threshold) {
} else if (scrollPos <= edgePos + threshold) {
prepend && prepend({cache: (startCursor > 0) && items.getData(startCursor - 1)});
}
if (!isProcessing) {
this.recycle(scrollPos, isForward);
}
}
setCursor(cursor, index) {
if (!this.options.useRecycle) {
Expand Down
3 changes: 2 additions & 1 deletion src/InfiniteGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
DEFENSE_BROWSER,
IGNORE_CLASSNAME,
} from "./consts";
import {toArray, $, innerWidth, innerHeight, matchHTML} from "./utils";
import Infinite from "./Infinite";
import {toArray, $, innerWidth, innerHeight, matchHTML} from "./utils";

// IE8
// https://stackoverflow.com/questions/43216659/babel-ie8-inherit-issue-with-object-create
Expand Down Expand Up @@ -130,6 +130,7 @@ class InfiniteGrid extends Component {
});

this._infinite = new Infinite(this._items, {
horizontal: this.options.horizontal,
useRecycle: this.options.useRecycle,
threshold: this.options.threshold,
append: param => this._requestAppend(param),
Expand Down
2 changes: 1 addition & 1 deletion src/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class Watcher {
return this._containerOffset;
}
_setContainerOffset() {
this._containerOffset = this.options.isOverflowScroll ? 0 : this.container[`offset${this.options.isVertical ? "Top" : "Left"}`];
this._containerOffset = this.options.isOverflowScroll ? 0 : this.options.container[`offset${this.options.isVertical ? "Top" : "Left"}`];
}
_onResize() {
if (this._timer.resize) {
Expand Down

0 comments on commit e5b8af3

Please sign in to comment.