@@ -25,6 +25,26 @@ export function createSourceLineScroll() {
2525 list ?. scrollToIndex ( { index, animated : false , viewPosition : 0.3 } ) ;
2626 if ( visibleItems . some ( ( item ) => item . index === index ) ) pendingIndex = null ;
2727 } ;
28+ const recoverFromViewport = ( list : SourceLineList | null ) => {
29+ const index = target ( list ) ;
30+ if ( index === null ) return ;
31+ if ( visibleItems . some ( ( item ) => item . index === index ) ) {
32+ pendingIndex = null ;
33+ list ?. scrollToIndex ( { index, animated : false , viewPosition : 0.3 } ) ;
34+ return ;
35+ }
36+ const first = visibleItems [ 0 ] ?. index ;
37+ const last = visibleItems . at ( - 1 ) ?. index ;
38+ if ( first == null || last == null || last < first ) return ;
39+ // An accepted scroll can still use an estimate for an unmeasured row.
40+ // Correct from the actual viewport rather than repeating that same estimate.
41+ const rowHeight = viewportHeight > 0 ? viewportHeight / ( last - first + 1 ) : averageItemLength ;
42+ if ( rowHeight <= 0 ) return ;
43+ list ?. scrollToOffset ( {
44+ offset : Math . max ( 0 , scrollOffset + ( index - first ) * rowHeight ) ,
45+ animated : false ,
46+ } ) ;
47+ } ;
2848
2949 return {
3050 scrollToLine,
@@ -45,34 +65,22 @@ export function createSourceLineScroll() {
4565 const index = target ( list ) ;
4666 if ( index !== null ) scrollToLine ( list , index ) ;
4767 } ,
48- onScroll ( offset : number , height : number ) {
68+ onScroll ( offset : number , height : number , list : SourceLineList | null ) {
69+ const moved = offset !== scrollOffset ;
4970 scrollOffset = offset ;
5071 viewportHeight = height ;
72+ if ( moved && pendingIndex !== null ) {
73+ // RN updates viewability after our scroll handler, and skips its callback
74+ // while the viewport stays inside one tall row. Use the updated rows.
75+ queueMicrotask ( ( ) => recoverFromViewport ( list ) ) ;
76+ }
5177 } ,
5278 onViewableItemsChanged (
5379 list : SourceLineList | null ,
5480 items : ReadonlyArray < { readonly index ?: number | null } > ,
5581 ) {
5682 visibleItems = items ;
57- const index = target ( list ) ;
58- if ( index === null ) return ;
59- if ( items . some ( ( item ) => item . index === index ) ) {
60- pendingIndex = null ;
61- list ?. scrollToIndex ( { index, animated : false , viewPosition : 0.3 } ) ;
62- return ;
63- }
64- const first = items [ 0 ] ?. index ;
65- const last = items . at ( - 1 ) ?. index ;
66- if ( first == null || last == null || last < first ) return ;
67- // An accepted scroll can still use an estimate for an unmeasured row.
68- // Correct from the actual viewport rather than repeating that same estimate.
69- const rowHeight =
70- viewportHeight > 0 ? viewportHeight / ( last - first + 1 ) : averageItemLength ;
71- if ( rowHeight <= 0 ) return ;
72- list ?. scrollToOffset ( {
73- offset : Math . max ( 0 , scrollOffset + ( index - first ) * rowHeight ) ,
74- animated : false ,
75- } ) ;
83+ recoverFromViewport ( list ) ;
7684 } ,
7785 cancel ( ) {
7886 pendingIndex = null ;
0 commit comments