Fix maintainVisibleContentPosition with rapid data updates (#53542) - #57955
Open
kulkarni-rohan wants to merge 1 commit into
Open
Fix maintainVisibleContentPosition with rapid data updates (#53542)#57955kulkarni-rohan wants to merge 1 commit into
kulkarni-rohan wants to merge 1 commit into
Conversation
Fixes react#53542 where rapid prepends with coalesced native scroll events leave pendingScrollUpdateCount stuck, freezing render window. Root cause: pendingScrollUpdateCount increments by 1 per prepend (0->1->2) but EventQueue coalesces N scroll events into 1 via dispatchUniqueEvent (ScrollViewEventEmitter.cpp:13 + EventQueue.cpp:29-50 replaces in place), so single scroll drains 2->1 leaving blocked. Also leak when JS predicate fires but native declines (tag recycled, delta<=0.5, clamped). Fix: Clamp pending to 1 (not accumulate) and drain to 0 on any scroll, so single coalesced event unblocks. Prevents frozen window, suppressed onViewableItemsChanged and onEndReached. [GENERAL] [FIXED] - Fix maintainVisibleContentPosition with rapid data updates (react#53542)
|
@fabriziocucci has imported this pull request. If you are a Meta employee, you can view this in D115907610. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #53542
Summary:
Problem:
maintainVisibleContentPositionfails when FlatList data is updated rapidly with prepends in quick succession (e.g., chat receiving messages). After 2 prepends before native scroll drains, render window stays frozen,onViewableItemsChangedsuppressed,onEndReachednever fires.Root cause:
pendingScrollUpdateCountassumption is structurally unsound – it increments by 1 per prepend ingetDerivedStateFromProps(0→1→2) but native scroll events are dispatched as unique events that coalesce. Verified in C++:ScrollViewEventEmitter.cpp:13–onScrolldispatched withdispatchUniqueEvent("scroll", ...)EventQueue.cpp:29-50– unique event whose target+type matches existing replaces in place (line 49) instead of appendingSo N scroll events emitted before queue flush deliver exactly 1 to JS. Single coalesced event drains 2→1 leaving blocked. Also leak when JS predicate fires (old key found at new index) but native declines to adjust (tag recycled, view deleted, delta ≤0.5, clamped).
Fix (1 file, 2 lines core): Clamp pending to at most 1 and drain to 0 on any scroll:
pendingScrollUpdateCount: 1instead ofprev+1– prevents accumulation during rapid prependssetState({pendingScrollUpdateCount: 0})instead of-1– single coalesced scroll unblocksThis turns "stuck at N" into unblocked after next scroll, fixing frozen window / viewability / onEndReached for rapid updates. For leak path where native declines (no scroll ever), flag would still be stranded at 1 – addressed by boolean rename + escape hatch in follow-up, but this PR already strictly improves and matches existing tests that drain 0→1→0.
Changelog:
[GENERAL] [FIXED] - Fix maintainVisibleContentPosition with rapid data updates (#53542)
Test Plan:
Jest (VirtualizedList):
New test ( regression for #53542 ):
pendingScrollUpdateCount === 0(fails on main with 1) andfirstVisibleItemKeynot nullExisting MVCP tests still pass:
handles maintainVisibleContentPositionhandles multiple rapid prepends(separate scroll per prepend)delta stays boundedminIndexForVisible >0andinvertedLint:
yarn lint # Done (max-warnings 0)Closes #53542