vscroll
Version:
Virtual scroll engine
123 lines • 2.9 kB
JavaScript
import { Direction } from '../../inputs/index';
class Positions {
constructor() {
this.reset();
}
reset() {
this.startDelta = 0;
this.before = 0;
}
}
class First {
constructor() {
this.reset();
}
reset() {
this.index = NaN;
this.indexBuffer = NaN;
this.position = NaN;
}
}
class Last {
constructor() {
this.reset();
}
reset() {
this.index = NaN;
this.indexBuffer = NaN;
}
}
class FirstVisible {
constructor() {
this.reset();
}
reset() {
this.index = NaN;
this.delta = 0;
}
}
export class FetchModel {
constructor(directionPriority) {
this.directionPriority = directionPriority;
this.callCount = 0;
this.positions = new Positions();
this.first = new First();
this.last = new Last();
this.firstVisible = new FirstVisible();
this.reset();
}
reset() {
this._newItemsData = null;
this.items = [];
this.positions.reset();
this.first.reset();
this.last.reset();
this.firstVisible.reset();
this.hasAnotherPack = false;
this.direction = null;
this.cancel = null;
this.simulate = false;
this.isCheck = false;
this.doRemove = false;
}
get newItemsData() {
return this._newItemsData;
}
set newItemsData(items) {
this._newItemsData = items;
if (items && items.length) {
this.callCount++;
}
}
get shouldFetch() {
return !!this.count;
}
get hasNewItems() {
return !!((this._newItemsData && this._newItemsData.length));
}
get index() {
return this.first.index;
}
get count() {
return !isNaN(this.first.index) && !isNaN(this.last.index) ? this.last.index - this.first.index + 1 : 0;
}
shouldCheckPreSizeExpectation(lastBufferedIndex) {
if (this.directionPriority === Direction.backward) {
return false;
}
const lastFetched = this.items[this.items.length - 1];
return lastFetched && lastFetched.$index < lastBufferedIndex;
}
startSimulate(items) {
this.simulate = true;
this._newItemsData = items.map(item => item.data);
this.items = items;
this.hasAnotherPack = false;
}
stopSimulate() {
this.simulate = false;
this.isCheck = false;
this.doRemove = false;
}
fill(items, start) {
this.startSimulate(items);
this.first.index = items[0].$index;
this.last.index = items[items.length - 1].$index;
this.direction = Direction.forward;
this.firstVisible.index = start;
this.firstVisible.delta = 0;
}
check(items) {
this.startSimulate(items);
this.last.index = items[0].$index;
this.first.index = items[items.length - 1].$index;
this.isCheck = true;
}
update(index, delta, items, itemsToRemove) {
this.startSimulate(items);
this.firstVisible.index = index;
this.firstVisible.delta = delta;
this.doRemove = itemsToRemove.length > 0;
}
}
//# sourceMappingURL=fetch.js.map