@@ -70,7 +70,7 @@ export class VirtualRepeat extends AbstractRepeater {
7070 * @internal
7171 * First view index, for proper follow up calculations
7272 */
73- _first = 0 ;
73+ _first : number = 0 ;
7474
7575 /**
7676 * @internal
@@ -376,6 +376,8 @@ export class VirtualRepeat extends AbstractRepeater {
376376 * - handle scroll as if scroll event happened
377377 */
378378 itemsChanged ( ) : void {
379+ // the current collection subscription may be irrelevant
380+ // unsubscribe and resubscribe later
379381 this . _unsubscribeCollection ( ) ;
380382 // still bound? and still attached?
381383 if ( ! this . scope || ! this . _isAttached ) {
@@ -385,11 +387,11 @@ export class VirtualRepeat extends AbstractRepeater {
385387 let previousLastViewIndex = this . _getIndexOfLastView ( ) ;
386388
387389 const items = this . items ;
388- const currentItemCount = items . length ;
389390 const shouldCalculateSize = ! ! items ;
390391 const strategy = this . strategy = this . strategyLocator . getStrategy ( items ) ;
391-
392+
392393 if ( shouldCalculateSize ) {
394+ const currentItemCount = items . length ;
393395 if ( currentItemCount > 0 && this . viewCount ( ) === 0 ) {
394396 strategy . createFirstItem ( this ) ;
395397 }
@@ -412,6 +414,7 @@ export class VirtualRepeat extends AbstractRepeater {
412414 strategy . instanceChanged ( this , items , this . _first ) ;
413415
414416 if ( shouldCalculateSize ) {
417+ const currentItemCount = items . length ;
415418 // Reset rebinding
416419 this . _lastRebind = this . _first ;
417420
@@ -534,31 +537,31 @@ export class VirtualRepeat extends AbstractRepeater {
534537 this . _skipNextScrollHandle = false ;
535538 return ;
536539 }
537- if ( ! this . items ) {
540+ const items = this . items ;
541+ if ( ! items ) {
538542 return ;
539543 }
540- let itemHeight = this . itemHeight ;
541- let scrollTop = this . _fixedHeightContainer
544+ const itemHeight = this . itemHeight ;
545+ const scrollTop = this . _fixedHeightContainer
542546 ? this . scrollContainer . scrollTop
543547 : ( pageYOffset - this . distanceToTop ) ;
544548
545549 // Calculate the index of first view
546550 // Using Math floor to ensure it has correct space for both small and large calculation
547- let firstViewIndex = itemHeight > 0 ? Math . floor ( scrollTop / itemHeight ) : 0 ;
548- this . _first = firstViewIndex < 0 ? 0 : firstViewIndex ;
551+ let firstIndex = itemHeight > 0 ? Math . floor ( scrollTop / itemHeight ) : 0 ;
552+ this . _first = firstIndex < 0 ? 0 : firstIndex ;
549553 // if first index starts somewhere after the last view
550554 // then readjust based on the delta
551- if ( this . _first > this . items . length - this . elementsInView ) {
552- firstViewIndex = this . items . length - this . elementsInView ;
553- this . _first = firstViewIndex < 0 ? 0 : firstViewIndex ;
555+ if ( firstIndex > items . length - this . elementsInView ) {
556+ this . _first = Math . max ( 0 , items . length - this . elementsInView ) ;
554557 }
555558
556559 // Check scrolling states and adjust flags
557560 this . _checkScrolling ( ) ;
558561
559562 // store buffers' heights into local variables
560- let currentTopBufferHeight = this . _topBufferHeight ;
561- let currentBottomBufferHeight = this . _bottomBufferHeight ;
563+ const currentTopBufferHeight = this . _topBufferHeight ;
564+ const currentBottomBufferHeight = this . _bottomBufferHeight ;
562565
563566 // TODO if and else paths do almost same thing, refactor?
564567 if ( this . _scrollingDown ) {
0 commit comments