Skip to content

Commit 05f2c69

Browse files
committed
address comments
1 parent 0183164 commit 05f2c69

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
176176
* pixels).
177177
*/
178178
updateBufferSize(minBufferPx: number, maxBufferPx: number) {
179+
if (maxBufferPx < minBufferPx) {
180+
throw('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
181+
}
179182
this._minBufferPx = minBufferPx;
180183
this._maxBufferPx = maxBufferPx;
181184
}

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ describe('CdkVirtualScrollViewport', () => {
4040
'should render 4 items to fill 200px space based on 50px estimate from first item');
4141
}));
4242

43+
it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {
44+
testComponent.minBufferPx = 100;
45+
testComponent.maxBufferPx = 99;
46+
expect(() => finishInit(fixture)).toThrow();
47+
}));
48+
4349
// TODO(mmalerba): Add test that it corrects the initial render if it didn't render enough,
4450
// once it actually does that.
4551
});

src/cdk/scrolling/fixed-size-virtual-scroll.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
6767
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
6868
*/
6969
updateItemAndBufferSize(itemSize: number, minBufferPx: number, maxBufferPx: number) {
70+
if (maxBufferPx < minBufferPx) {
71+
throw('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
72+
}
7073
this._itemSize = itemSize;
7174
this._minBufferPx = minBufferPx;
7275
this._maxBufferPx = maxBufferPx;
@@ -119,30 +122,31 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
119122

120123
const scrollOffset = this._viewport.measureScrollOffset();
121124
const firstVisibleIndex = scrollOffset / this._itemSize;
122-
const range = {...this._viewport.getRenderedRange()};
125+
const renderedRange = this._viewport.getRenderedRange();
126+
const newRange = {start: renderedRange.start, end: renderedRange.end};
127+
const viewportSize = this._viewport.getViewportSize();
128+
const dataLength = this._viewport.getDataLength();
123129

124-
const startBuffer = scrollOffset - range.start * this._itemSize;
125-
if (startBuffer < this._minBufferPx && range.start != 0) {
130+
const startBuffer = scrollOffset - newRange.start * this._itemSize;
131+
if (startBuffer < this._minBufferPx && newRange.start != 0) {
126132
const expandStart = Math.ceil((this._maxBufferPx - startBuffer) / this._itemSize);
127-
range.start = Math.max(0, range.start - expandStart);
128-
range.end = Math.min(this._viewport.getDataLength(),
129-
Math.ceil(firstVisibleIndex +
130-
(this._viewport.getViewportSize() + this._minBufferPx) / this._itemSize));
133+
newRange.start = Math.max(0, newRange.start - expandStart);
134+
newRange.end = Math.min(dataLength,
135+
Math.ceil(firstVisibleIndex + (viewportSize + this._minBufferPx) / this._itemSize));
131136
} else {
132-
const endBuffer =
133-
range.end * this._itemSize - (scrollOffset + this._viewport.getViewportSize());
134-
if (endBuffer < this._minBufferPx && range.end != this._viewport.getDataLength()) {
137+
const endBuffer = newRange.end * this._itemSize - (scrollOffset + viewportSize);
138+
if (endBuffer < this._minBufferPx && newRange.end != dataLength) {
135139
const expandEnd = Math.ceil((this._maxBufferPx - endBuffer) / this._itemSize);
136140
if (expandEnd > 0) {
137-
range.end = Math.min(this._viewport.getDataLength(), range.end + expandEnd);
138-
range.start = Math.max(0,
141+
newRange.end = Math.min(dataLength, newRange.end + expandEnd);
142+
newRange.start = Math.max(0,
139143
Math.floor(firstVisibleIndex - this._minBufferPx / this._itemSize));
140144
}
141145
}
142146
}
143147

144-
this._viewport.setRenderedRange(range);
145-
this._viewport.setRenderedContentOffset(this._itemSize * range.start);
148+
this._viewport.setRenderedRange(newRange);
149+
this._viewport.setRenderedContentOffset(this._itemSize * newRange.start);
146150
this._scrolledIndexChange.next(Math.floor(firstVisibleIndex));
147151
}
148152
}

src/cdk/scrolling/scrolling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ that it allows for better performance, since items do not need to be measured as
9494
</cdk-virtual-scroll-viewport>
9595
```
9696

97-
The fixed size strategy also supports setting a couple buffer parameters that determine how much
97+
The fixed size strategy also supports setting a couple of buffer parameters that determine how much
9898
extra content is rendered beyond what is visible in the viewport. The first of these parameters is
9999
`minBufferPx`. The `minBufferPx` is the minimum amount of content buffer (in pixels) that the
100100
viewport must render. If the viewport ever detects that there is less buffered content it will

src/cdk/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,13 @@ describe('CdkVirtualScrollViewport', () => {
576576
flush();
577577

578578
expect(viewport.getRenderedRange())
579-
.toEqual({start: 0, end: 8}, 'should not render 2 more buffer items');
579+
.toEqual({start: 0, end: 8}, 'should render 2 more buffer items');
580+
}));
581+
582+
it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {
583+
testComponent.minBufferPx = 100;
584+
testComponent.maxBufferPx = 99;
585+
expect(() => finishInit(fixture)).toThrow();
580586
}));
581587
});
582588
});

0 commit comments

Comments
 (0)