@@ -67,6 +67,9 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
67
67
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
68
68
*/
69
69
updateItemAndBufferSize ( itemSize : number , minBufferPx : number , maxBufferPx : number ) {
70
+ if ( maxBufferPx < minBufferPx ) {
71
+ throw Error ( 'CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx' ) ;
72
+ }
70
73
this . _itemSize = itemSize ;
71
74
this . _minBufferPx = minBufferPx ;
72
75
this . _maxBufferPx = maxBufferPx ;
@@ -119,30 +122,31 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
119
122
120
123
const scrollOffset = this . _viewport . measureScrollOffset ( ) ;
121
124
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 ( ) ;
123
129
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 ) {
126
132
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 ) ) ;
131
136
} 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 ) {
135
139
const expandEnd = Math . ceil ( ( this . _maxBufferPx - endBuffer ) / this . _itemSize ) ;
136
140
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 ,
139
143
Math . floor ( firstVisibleIndex - this . _minBufferPx / this . _itemSize ) ) ;
140
144
}
141
145
}
142
146
}
143
147
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 ) ;
146
150
this . _scrolledIndexChange . next ( Math . floor ( firstVisibleIndex ) ) ;
147
151
}
148
152
}
0 commit comments