mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 22:17:40 +08:00
perf(virtual-scroll): fast path for removing
This commit is contained in:

committed by
Manuel Mtz-Almeida

parent
88e5642f60
commit
8baa6474a4
@ -44,18 +44,19 @@ export class E2EPage {
|
||||
|
||||
addRandomItem() {
|
||||
const index = Math.floor(Math.random() * this.items.length);
|
||||
console.log('Adding to index: ', index);
|
||||
this.items.splice( index, 0, {
|
||||
value: Math.floor(Math.random() * 10000),
|
||||
someMethod: function() {
|
||||
return '!!';
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
changeItem() {
|
||||
const index = Math.floor(Math.random() * this.items.length);
|
||||
this.items[index].value = Math.floor(Math.random() * 10000);
|
||||
console.log('Change to index: ', index);
|
||||
this.items[index] = { value: Math.floor(Math.random() * 10000), someMethod: () => '!!' };
|
||||
}
|
||||
|
||||
trackByFn(index: number, item: any) {
|
||||
|
@ -233,7 +233,8 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
|
||||
scrollTop: 0,
|
||||
};
|
||||
_queue: number = SCROLL_QUEUE_NO_CHANGES;
|
||||
_recordSize: number = 0;
|
||||
|
||||
|
||||
_virtualTrackBy: TrackByFn;
|
||||
|
||||
@ContentChild(VirtualItem) _itmTmp: VirtualItem;
|
||||
@ -416,10 +417,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
|
||||
*/
|
||||
firstRecord(): number {
|
||||
const cells = this._cells;
|
||||
if (cells.length > 0) {
|
||||
return cells[0].record;
|
||||
}
|
||||
return 0;
|
||||
return (cells.length > 0) ? cells[0].record : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -427,10 +425,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
|
||||
*/
|
||||
lastRecord(): number {
|
||||
const cells = this._cells;
|
||||
if (cells.length > 0) {
|
||||
return cells[cells.length - 1].record;
|
||||
}
|
||||
return 0;
|
||||
return (cells.length > 0) ? cells[cells.length - 1].record : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -451,15 +446,25 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
|
||||
let needClean = false;
|
||||
if (changes) {
|
||||
var lastRecord = this.lastRecord() + 1;
|
||||
changes.forEachOperation((item, _, cindex) => {
|
||||
if (item.previousIndex != null || (cindex < lastRecord)) {
|
||||
|
||||
changes.forEachOperation((_, pindex, cindex) => {
|
||||
|
||||
// add new record after current position
|
||||
if (pindex === null && (cindex < lastRecord)) {
|
||||
console.debug('adding record before current position, slow path');
|
||||
needClean = true;
|
||||
return;
|
||||
}
|
||||
// remove record after current position
|
||||
if (pindex < lastRecord && cindex === null) {
|
||||
console.debug('removing record before current position, slow path');
|
||||
needClean = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
needClean = true;
|
||||
}
|
||||
this._recordSize = this._records.length;
|
||||
|
||||
this.readUpdate(needClean);
|
||||
this.writeUpdate(needClean);
|
||||
@ -555,7 +560,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
|
||||
this._itmTmp.viewContainer,
|
||||
this._itmTmp.templateRef,
|
||||
this._hdrTmp && this._hdrTmp.templateRef,
|
||||
this._ftrTmp && this._ftrTmp.templateRef, needClean,
|
||||
this._ftrTmp && this._ftrTmp.templateRef, needClean
|
||||
);
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user