Ignore stale minimum-view-time viewability updates (#57923)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57923

Changelog: [General][Fixed] - Ignore stale viewability updates while enforcing minimum view time

Minimum view-time callbacks can outlive a newer viewport update. During initial layout, callbacks for intermediate viewport snapshots can fire after the current snapshot and replace the correct visible-item set. Discard callbacks whose captured visible-index set is no longer current, and cover the transition with a unit test.

Reviewed By: Abbondanzo

Differential Revision: D115740467

fbshipit-source-id: 0a308dbc57c1d1fc5b2b669d998d1d9f8f1495f1
This commit is contained in:
Riccardo Cipolleschi authored and meta-codesync[bot] committed 2026-08-13 06:59:30 -07:00
1 parent e774f0d623
commit 1c4a46f4e3
3 files changed
+62

No files matched your search

@@ -680,6 +680,12 @@ describe('<FlatList>', () => {
timers.advanceTimersByTime(200);
// The first timer represents an intermediate viewport snapshot and
// must not publish after the final visible set supersedes it.
expect(onViewableItemsChanged).not.toHaveBeenCalled();
timers.advanceTimersByTime(200);
expect(onViewableItemsChanged).toHaveBeenCalled();
} finally {
timers.uninstall();
@@ -231,6 +231,10 @@ class ViewabilityHelper {
* comment suppresses an error found when Flow v0.63 was deployed. To
* see the error delete this comment and run Flow. */
this._timers.delete(handle);
// `onUpdate` replaces the array whenever the visible set changes.
if (this._viewableIndices !== viewableIndices) {
return;
}
this._onUpdateSync(
props,
viewableIndices,
@@ -342,6 +342,58 @@ describe('onUpdate', function () {
});
});
it('minimumViewTime ignores stale viewability updates', function () {
const helper = new ViewabilityHelper({
minimumViewTime: 350,
viewAreaCoveragePercentThreshold: 0,
});
rowFrames = {
a: {y: 0, height: 200},
b: {y: 200, height: 200},
};
data = [{key: 'a'}, {key: 'b'}];
const onViewableItemsChanged = jest.fn();
helper.onUpdate(
props,
0,
200,
// $FlowFixMe[incompatible-type] - Invalid `ListMetricsAggregator`.
{getCellMetrics},
createViewToken,
onViewableItemsChanged,
);
helper.onUpdate(
props,
0,
400,
// $FlowFixMe[incompatible-type] - Invalid `ListMetricsAggregator`.
{getCellMetrics},
createViewToken,
onViewableItemsChanged,
);
jest.runAllTimers();
expect(onViewableItemsChanged.mock.calls).toEqual([
[
{
changed: [
{isViewable: true, key: 'a'},
{isViewable: true, key: 'b'},
],
viewabilityConfig: {
minimumViewTime: 350,
viewAreaCoveragePercentThreshold: 0,
},
viewableItems: [
{isViewable: true, key: 'a'},
{isViewable: true, key: 'b'},
],
},
],
]);
});
it('minimumViewTime skips briefly visible items', function () {
const helper = new ViewabilityHelper({
minimumViewTime: 350,