Move common code from if-else

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-08-09 14:35:10 +05:30 committed by Bruno Windels
parent 168312627d
commit a02b6b68d3

View file

@ -173,7 +173,7 @@ export class LazyListView extends ListView {
this._renderRange = renderRange; this._renderRange = renderRange;
const { topCount, renderCount } = this._renderRange; const { topCount, renderCount } = this._renderRange;
const renderedItems = this._itemsFromList(topCount, topCount + renderCount); const renderedItems = this._itemsFromList({ start: topCount, end: topCount + renderCount});
this._adjustPadding(renderRange); this._adjustPadding(renderRange);
this._childInstances = []; this._childInstances = [];
const fragment = document.createDocumentFragment(); const fragment = document.createDocumentFragment();
@ -185,7 +185,7 @@ export class LazyListView extends ListView {
this._root.appendChild(fragment); this._root.appendChild(fragment);
} }
_itemsFromList(start, end) { _itemsFromList({start, end}) {
const array = []; const array = [];
let i = 0; let i = 0;
for (const item of this._list) { for (const item of this._list) {
@ -228,22 +228,18 @@ export class LazyListView extends ListView {
_renderElementsInRange(range) { _renderElementsInRange(range) {
const diff = this._renderRange.diff(range); const diff = this._renderRange.diff(range);
const {start, end} = diff.toAdd; const renderedItems = this._itemsFromList(diff.toAdd);
const renderedItems = this._itemsFromList(start, end);
this._adjustPadding(range); this._adjustPadding(range);
const {start, end} = diff.toRemove;
const normalizedStart = this._renderRange.normalize(start);
this._childInstances.splice(normalizedStart, end - start + 1).forEach(child => this._removeChild(child));
if (diff.scrollDirection === ScrollDirection.downwards) { if (diff.scrollDirection === ScrollDirection.downwards) {
const {start, end} = diff.toRemove;
this._childInstances.splice(0, end - start + 1)
.forEach(child => this._removeChild(child));
const fragment = this._renderedFragment(renderedItems, view => this._childInstances.push(view)); const fragment = this._renderedFragment(renderedItems, view => this._childInstances.push(view));
this._root.appendChild(fragment); this._root.appendChild(fragment);
} }
else { else {
const {start, end} = diff.toRemove;
const normalizedStart = this._renderRange.normalize(start);
this._childInstances.splice(normalizedStart, end - start + 1)
.forEach(child => this._removeChild(child));
const fragment = this._renderedFragment(renderedItems, view => this._childInstances.unshift(view)); const fragment = this._renderedFragment(renderedItems, view => this._childInstances.unshift(view));
this._root.insertBefore(fragment, this._root.firstChild); this._root.insertBefore(fragment, this._root.firstChild);
} }