Skip to content

Commit

Permalink
Stop searching nodes if cell is already rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
masimplo committed Sep 15, 2017
1 parent 7d3b7c4 commit 21a7ce9
Showing 1 changed file with 28 additions and 35 deletions.
63 changes: 28 additions & 35 deletions src/components/virtual-scroll/virtual-util.ts
Expand Up @@ -79,9 +79,7 @@ export function processRecords(stopAtHeight: number,
if (previousCell.top + previousCell.height + data.itmHeight > stopAtHeight && processedTotal > 3) {
return;
}

}

}


Expand Down Expand Up @@ -131,7 +129,6 @@ export function populateNodeData(startCellIndex: number, endCellIndex: number, s
let node: VirtualNode;
let availableNode: VirtualNode;
let cell: VirtualCell;
let isAlreadyRendered: boolean;
let viewInsertIndex: number = null;
let totalNodes = nodes.length;
let templateRef: TemplateRef<VirtualContext>;
Expand All @@ -141,49 +138,45 @@ export function populateNodeData(startCellIndex: number, endCellIndex: number, s
for (var cellIndex = startCellIndex; cellIndex <= endCellIndex; cellIndex++) {
cell = cells[cellIndex];
availableNode = null;
isAlreadyRendered = false;

// find the first one that's available
if (!initialLoad) {
for (var i = 0; i < totalNodes; i++) {
node = nodes[i];

if (cell.tmpl !== node.tmpl || i === 0 && cellIndex !== 0) {
// the cell must use the correct template
// first node can only be used by the first cell (css :first-child reasons)
// this node is never available to be reused
continue;
}

if (node.cell === cellIndex) {
isAlreadyRendered = true;
break;
}

if (node.cell < startCellIndex || node.cell > endCellIndex) {
const existingNode = nodes.find(n => n.cell === cellIndex);
if (existingNode) {
if (existingNode.view.context.$implicit === records[existingNode.cell]) continue; // optimization: node data is the same no need to update
availableNode = existingNode; // update existing node
} else {
for (var i = 0; i < totalNodes; i++) {
node = nodes[i];

if (cell.tmpl !== node.tmpl || i === 0 && cellIndex !== 0) {
// the cell must use the correct template
// first node can only be used by the first cell (css :first-child reasons)
// this node is never available to be reused
continue;
}

if (!availableNode) {
// havent gotten an available node yet
availableNode = nodes[i];
if (node.cell < startCellIndex || node.cell > endCellIndex) {

} else if (scrollingDown) {
// scrolling down
if (node.cell < availableNode.cell) {
if (!availableNode) {
// havent gotten an available node yet
availableNode = nodes[i];
}

} else {
// scrolling up
if (node.cell > availableNode.cell) {
availableNode = nodes[i];
} else if (scrollingDown) {
// scrolling down
if (node.cell < availableNode.cell) {
availableNode = nodes[i];
}

} else {
// scrolling up
if (node.cell > availableNode.cell) {
availableNode = nodes[i];
}
}
}
}
}

if (isAlreadyRendered) {
continue;
}
}

if (!availableNode) {
Expand Down

0 comments on commit 21a7ce9

Please sign in to comment.