Skip to content

Commit a62fc73

Browse files
authored
fix: do not load whole child collection with virtual scroll (CirclonGroup#463) (CirclonGroup#868)
1 parent 68e9e75 commit a62fc73

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

projects/angular-tree-component/src/lib/models/tree-virtual-scroll.model.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ export class TreeVirtualScroll {
129129

130130
if (!this.viewportHeight || !visibleNodes.length) return [];
131131

132+
// When loading children async this method is called before their height and position is calculated.
133+
// In that case firstIndex === 0 and lastIndex === visibleNodes.length - 1 (e.g. 1000),
134+
// which means that it loops through every visibleNodes item and push them into viewportNodes array.
135+
// We can prevent nodes from being pushed to the array and wait for the appropriate calculations to take place
136+
const lastVisibleNode = visibleNodes.slice(-1)[0]
137+
if (!lastVisibleNode.height && lastVisibleNode.position === 0) return [];
138+
132139
// Search for first node in the viewport using binary search
133140
// Look for first node that starts after the beginning of the viewport (with buffer)
134141
// Or that ends after the beginning of the viewport
@@ -145,19 +152,6 @@ export class TreeVirtualScroll {
145152

146153
const viewportNodes = [];
147154

148-
// Loading async top nodes' children is too long.
149-
// It happens when first node is visible withing viewport range (including Y_OFFSET).
150-
// In that case firstIndex == 0 and lastIndex == visibleNodes.length - 1 (e.g. 1000),
151-
// which means that it loops through every visibleNodes item and push them into viewportNodes array.
152-
// lastIndex should not equal visibleNodes.length - 1, but something around 50-100 (depending on the viewport)
153-
const nodeHeight = visibleNodes[0].treeModel.options.options.nodeHeight;
154-
const renderedNodesMaxLength = (Y_OFFSET * 2 + this.viewportHeight) / nodeHeight;
155-
156-
// Something is probably wrong, prevent nodes from being pushed to an array.
157-
if (lastIndex - firstIndex > renderedNodesMaxLength) {
158-
return [];
159-
}
160-
161155
for (let i = firstIndex; i <= lastIndex; i++) {
162156
viewportNodes.push(visibleNodes[i]);
163157
}

0 commit comments

Comments
 (0)