@@ -7,8 +7,14 @@ function setup() {
77 let visibleIndex = 0 ;
88 let requestedOffset : number | null = null ;
99 let mounted = true ;
10+ let lineCount = 1_203 ;
11+ let unmeasuredIndex : number | null = null ;
1012 const list = {
13+ get props ( ) {
14+ return { data : { length : lineCount } } ;
15+ } ,
1116 scrollToIndex ( { index } : { index : number } ) {
17+ if ( index >= lineCount ) throw new Error ( "scrollToIndex out of range" ) ;
1218 if ( index > highestMeasuredIndex ) {
1319 // VirtualizedList requires this recovery path for unmeasured variable-height rows.
1420 if ( ! scroll . onScrollToIndexFailed ) {
@@ -17,7 +23,7 @@ function setup() {
1723 scroll . onScrollToIndexFailed ( list , { index, averageItemLength : 24 } ) ;
1824 return ;
1925 }
20- visibleIndex = index ;
26+ visibleIndex = index === unmeasuredIndex ? 100 : index ;
2127 } ,
2228 scrollToOffset ( { offset } : { offset : number } ) {
2329 requestedOffset = offset ;
@@ -36,9 +42,27 @@ function setup() {
3642 return requestedOffset ;
3743 } ,
3844 measureThrough ( index : number ) {
39- highestMeasuredIndex = index ;
45+ highestMeasuredIndex = Math . max ( highestMeasuredIndex , index ) ;
46+ if ( unmeasuredIndex !== null && index >= unmeasuredIndex ) unmeasuredIndex = null ;
4047 scroll . retry ( mounted ? list : null ) ;
4148 } ,
49+ acceptApproximationFor ( index : number ) {
50+ highestMeasuredIndex = index + 100 ;
51+ unmeasuredIndex = index ;
52+ } ,
53+ replaceDocument ( count : number ) {
54+ lineCount = count ;
55+ } ,
56+ reportViewable ( offset : number , height : number , indices : readonly number [ ] ) {
57+ visibleIndex = indices [ 0 ] ?? visibleIndex ;
58+ highestMeasuredIndex = Math . max ( highestMeasuredIndex , ...indices ) ;
59+ if ( unmeasuredIndex !== null && indices . includes ( unmeasuredIndex ) ) unmeasuredIndex = null ;
60+ scroll . onScroll ( offset , height ) ;
61+ scroll . onViewableItemsChanged (
62+ mounted ? list : null ,
63+ indices . map ( ( index ) => ( { index } ) ) ,
64+ ) ;
65+ } ,
4266 unmount ( ) {
4367 mounted = false ;
4468 scroll . cancel ( ) ;
@@ -86,3 +110,65 @@ it("does not scroll after the source list unmounts", () => {
86110 expect ( ( ) => state . measureThrough ( 950 ) ) . not . toThrow ( ) ;
87111 expect ( state . visibleIndex ) . toBe ( 0 ) ;
88112} ) ;
113+
114+ it ( "keeps an accepted approximate jump pending until the target is reached" , ( ) => {
115+ const state = setup ( ) ;
116+ state . acceptApproximationFor ( 899 ) ;
117+
118+ state . scroll . scrollToLine ( 899 ) ;
119+ expect ( state . visibleIndex ) . toBe ( 100 ) ;
120+ state . measureThrough ( 950 ) ;
121+ expect ( state . visibleIndex ) . toBe ( 899 ) ;
122+ } ) ;
123+
124+ it ( "rejects a stale jump when replacement data arrives before cleanup" , ( ) => {
125+ const state = setup ( ) ;
126+
127+ state . scroll . scrollToLine ( 899 ) ;
128+ state . replaceDocument ( 12 ) ;
129+ expect ( ( ) => state . measureThrough ( 950 ) ) . not . toThrow ( ) ;
130+ expect ( state . visibleIndex ) . toBe ( 0 ) ;
131+ } ) ;
132+
133+ it . each ( [
134+ { first : 100 , offset : 2_400 , direction : "before" } ,
135+ { first : 1_000 , offset : 24_000 , direction : "after" } ,
136+ ] ) ( "corrects an approximate jump that lands $direction the target" , ( { first, offset } ) => {
137+ const state = setup ( ) ;
138+ state . acceptApproximationFor ( 899 ) ;
139+ state . scroll . scrollToLine ( 899 ) ;
140+
141+ state . reportViewable (
142+ offset ,
143+ 240 ,
144+ Array . from ( { length : 10 } , ( _ , index ) => first + index ) ,
145+ ) ;
146+ if ( first < 899 ) expect ( state . requestedOffset ) . toBeGreaterThan ( offset ) ;
147+ else expect ( state . requestedOffset ) . toBeLessThan ( offset ) ;
148+
149+ state . measureThrough ( 950 ) ;
150+ expect ( state . visibleIndex ) . toBe ( 899 ) ;
151+ } ) ;
152+
153+ it ( "stops recovering after the target becomes visible" , ( ) => {
154+ const state = setup ( ) ;
155+ state . acceptApproximationFor ( 899 ) ;
156+ state . scroll . scrollToLine ( 899 ) ;
157+
158+ state . reportViewable ( 21_576 , 240 , [ 899 , 900 , 901 ] ) ;
159+ expect ( state . visibleIndex ) . toBe ( 899 ) ;
160+
161+ state . reportViewable ( 480 , 240 , [ 20 , 21 , 22 ] ) ;
162+ state . measureThrough ( 1_100 ) ;
163+ expect ( state . visibleIndex ) . toBe ( 20 ) ;
164+ } ) ;
165+
166+ it ( "finishes an already visible target without waiting for another viewability event" , ( ) => {
167+ const state = setup ( ) ;
168+ state . reportViewable ( 0 , 240 , [ 0 , 1 , 2 , 3 , 4 ] ) ;
169+
170+ state . scroll . scrollToLine ( 4 ) ;
171+ state . reportViewable ( 480 , 240 , [ 20 , 21 , 22 ] ) ;
172+ state . measureThrough ( 100 ) ;
173+ expect ( state . visibleIndex ) . toBe ( 20 ) ;
174+ } ) ;
0 commit comments