mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
a53512fda4
Summary: This diff is part of an overall stack, meant to fix incorrect usage of `setState()` in `VirtualizedList`, which triggers new invariant checks added in `VirtualizedList_EXPERIMENTAL`. See the stack summary below for more information on the broader change. ## Diff Summary `_createViewToken()` is called during state changes. Use explicit props passed in. ## Stack Summary `VirtualizedList`'s component state is a set of cells to render. This state is set via the `setState()` class component API. The main "tick" function `VirtualizedList._updateCellsToRender()` calculates this new state using a combination of the current component state, and instance-local state like maps, measurement caches, etc. From: https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous --- > React may batch multiple setState() calls into a single update for performance. Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state. For example, this code may fail to update the counter: ``` // Wrong this.setState({ counter: this.state.counter + this.props.increment, }); ``` > To fix it, use a second form of setState() that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument: ``` // Correct this.setState((state, props) => ({ counter: state.counter + props.increment })); ``` --- `_updateCellsToRender()` transitively calls many functions which will read directly from `this.props` or `this.state` instead of the value passed by the state updater. This intermittently fires invariant violations, when there is a mismatch. This diff migrates all usages of `props` and `state` during state update to the values provied in `setState()`. To prevent future mismatch, and to provide better clarity on when it is safe to use `this.props`, `this.state`, I overrode `setState` to fire an invariant violation if it is accessed when it is unsafe to: {F756963772} Changelog: [Internal][Fixed] - Thread props to _createViewToken() Reviewed By: genkikondo Differential Revision: D38294339 fbshipit-source-id: dc215e267f126a3789742c14c35479f509822710
361 lines
10 KiB
JavaScript
361 lines
10 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type {FrameMetricProps} from './VirtualizedListProps';
|
|
|
|
const invariant = require('invariant');
|
|
|
|
export type ViewToken = {
|
|
item: any,
|
|
key: string,
|
|
index: ?number,
|
|
isViewable: boolean,
|
|
section?: any,
|
|
...
|
|
};
|
|
|
|
export type ViewabilityConfigCallbackPair = {
|
|
viewabilityConfig: ViewabilityConfig,
|
|
onViewableItemsChanged: (info: {
|
|
viewableItems: Array<ViewToken>,
|
|
changed: Array<ViewToken>,
|
|
...
|
|
}) => void,
|
|
...
|
|
};
|
|
|
|
export type ViewabilityConfig = {|
|
|
/**
|
|
* Minimum amount of time (in milliseconds) that an item must be physically viewable before the
|
|
* viewability callback will be fired. A high number means that scrolling through content without
|
|
* stopping will not mark the content as viewable.
|
|
*/
|
|
minimumViewTime?: number,
|
|
|
|
/**
|
|
* Percent of viewport that must be covered for a partially occluded item to count as
|
|
* "viewable", 0-100. Fully visible items are always considered viewable. A value of 0 means
|
|
* that a single pixel in the viewport makes the item viewable, and a value of 100 means that
|
|
* an item must be either entirely visible or cover the entire viewport to count as viewable.
|
|
*/
|
|
viewAreaCoveragePercentThreshold?: number,
|
|
|
|
/**
|
|
* Similar to `viewAreaPercentThreshold`, but considers the percent of the item that is visible,
|
|
* rather than the fraction of the viewable area it covers.
|
|
*/
|
|
itemVisiblePercentThreshold?: number,
|
|
|
|
/**
|
|
* Nothing is considered viewable until the user scrolls or `recordInteraction` is called after
|
|
* render.
|
|
*/
|
|
waitForInteraction?: boolean,
|
|
|};
|
|
|
|
/**
|
|
* A Utility class for calculating viewable items based on current metrics like scroll position and
|
|
* layout.
|
|
*
|
|
* An item is said to be in a "viewable" state when any of the following
|
|
* is true for longer than `minimumViewTime` milliseconds (after an interaction if `waitForInteraction`
|
|
* is true):
|
|
*
|
|
* - Occupying >= `viewAreaCoveragePercentThreshold` of the view area XOR fraction of the item
|
|
* visible in the view area >= `itemVisiblePercentThreshold`.
|
|
* - Entirely visible on screen
|
|
*/
|
|
class ViewabilityHelper {
|
|
_config: ViewabilityConfig;
|
|
_hasInteracted: boolean = false;
|
|
_timers: Set<number> = new Set();
|
|
_viewableIndices: Array<number> = [];
|
|
_viewableItems: Map<string, ViewToken> = new Map();
|
|
|
|
constructor(
|
|
config: ViewabilityConfig = {viewAreaCoveragePercentThreshold: 0},
|
|
) {
|
|
this._config = config;
|
|
}
|
|
|
|
/**
|
|
* Cleanup, e.g. on unmount. Clears any pending timers.
|
|
*/
|
|
dispose() {
|
|
/* $FlowFixMe[incompatible-call] (>=0.63.0 site=react_native_fb) This
|
|
* comment suppresses an error found when Flow v0.63 was deployed. To see
|
|
* the error delete this comment and run Flow. */
|
|
this._timers.forEach(clearTimeout);
|
|
}
|
|
|
|
/**
|
|
* Determines which items are viewable based on the current metrics and config.
|
|
*/
|
|
computeViewableItems(
|
|
props: FrameMetricProps,
|
|
scrollOffset: number,
|
|
viewportHeight: number,
|
|
getFrameMetrics: (
|
|
index: number,
|
|
props: FrameMetricProps,
|
|
) => ?{
|
|
length: number,
|
|
offset: number,
|
|
...
|
|
},
|
|
// Optional optimization to reduce the scan size
|
|
renderRange?: {
|
|
first: number,
|
|
last: number,
|
|
...
|
|
},
|
|
): Array<number> {
|
|
const itemCount = props.getItemCount(props.data);
|
|
const {itemVisiblePercentThreshold, viewAreaCoveragePercentThreshold} =
|
|
this._config;
|
|
const viewAreaMode = viewAreaCoveragePercentThreshold != null;
|
|
const viewablePercentThreshold = viewAreaMode
|
|
? viewAreaCoveragePercentThreshold
|
|
: itemVisiblePercentThreshold;
|
|
invariant(
|
|
viewablePercentThreshold != null &&
|
|
(itemVisiblePercentThreshold != null) !==
|
|
(viewAreaCoveragePercentThreshold != null),
|
|
'Must set exactly one of itemVisiblePercentThreshold or viewAreaCoveragePercentThreshold',
|
|
);
|
|
const viewableIndices = [];
|
|
if (itemCount === 0) {
|
|
return viewableIndices;
|
|
}
|
|
let firstVisible = -1;
|
|
const {first, last} = renderRange || {first: 0, last: itemCount - 1};
|
|
if (last >= itemCount) {
|
|
console.warn(
|
|
'Invalid render range computing viewability ' +
|
|
JSON.stringify({renderRange, itemCount}),
|
|
);
|
|
return [];
|
|
}
|
|
for (let idx = first; idx <= last; idx++) {
|
|
const metrics = getFrameMetrics(idx, props);
|
|
if (!metrics) {
|
|
continue;
|
|
}
|
|
const top = metrics.offset - scrollOffset;
|
|
const bottom = top + metrics.length;
|
|
if (top < viewportHeight && bottom > 0) {
|
|
firstVisible = idx;
|
|
if (
|
|
_isViewable(
|
|
viewAreaMode,
|
|
viewablePercentThreshold,
|
|
top,
|
|
bottom,
|
|
viewportHeight,
|
|
metrics.length,
|
|
)
|
|
) {
|
|
viewableIndices.push(idx);
|
|
}
|
|
} else if (firstVisible >= 0) {
|
|
break;
|
|
}
|
|
}
|
|
return viewableIndices;
|
|
}
|
|
|
|
/**
|
|
* Figures out which items are viewable and how that has changed from before and calls
|
|
* `onViewableItemsChanged` as appropriate.
|
|
*/
|
|
onUpdate(
|
|
props: FrameMetricProps,
|
|
scrollOffset: number,
|
|
viewportHeight: number,
|
|
getFrameMetrics: (
|
|
index: number,
|
|
props: FrameMetricProps,
|
|
) => ?{
|
|
length: number,
|
|
offset: number,
|
|
...
|
|
},
|
|
createViewToken: (
|
|
index: number,
|
|
isViewable: boolean,
|
|
props: FrameMetricProps,
|
|
) => ViewToken,
|
|
onViewableItemsChanged: ({
|
|
viewableItems: Array<ViewToken>,
|
|
changed: Array<ViewToken>,
|
|
...
|
|
}) => void,
|
|
// Optional optimization to reduce the scan size
|
|
renderRange?: {
|
|
first: number,
|
|
last: number,
|
|
...
|
|
},
|
|
): void {
|
|
const itemCount = props.getItemCount(props.data);
|
|
if (
|
|
(this._config.waitForInteraction && !this._hasInteracted) ||
|
|
itemCount === 0 ||
|
|
!getFrameMetrics(0, props)
|
|
) {
|
|
return;
|
|
}
|
|
let viewableIndices = [];
|
|
if (itemCount) {
|
|
viewableIndices = this.computeViewableItems(
|
|
props,
|
|
scrollOffset,
|
|
viewportHeight,
|
|
getFrameMetrics,
|
|
renderRange,
|
|
);
|
|
}
|
|
if (
|
|
this._viewableIndices.length === viewableIndices.length &&
|
|
this._viewableIndices.every((v, ii) => v === viewableIndices[ii])
|
|
) {
|
|
// We might get a lot of scroll events where visibility doesn't change and we don't want to do
|
|
// extra work in those cases.
|
|
return;
|
|
}
|
|
this._viewableIndices = viewableIndices;
|
|
if (this._config.minimumViewTime) {
|
|
const handle = setTimeout(() => {
|
|
/* $FlowFixMe[incompatible-call] (>=0.63.0 site=react_native_fb) This
|
|
* 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);
|
|
this._onUpdateSync(
|
|
props,
|
|
viewableIndices,
|
|
onViewableItemsChanged,
|
|
createViewToken,
|
|
);
|
|
}, this._config.minimumViewTime);
|
|
/* $FlowFixMe[incompatible-call] (>=0.63.0 site=react_native_fb) This
|
|
* comment suppresses an error found when Flow v0.63 was deployed. To see
|
|
* the error delete this comment and run Flow. */
|
|
this._timers.add(handle);
|
|
} else {
|
|
this._onUpdateSync(
|
|
props,
|
|
viewableIndices,
|
|
onViewableItemsChanged,
|
|
createViewToken,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* clean-up cached _viewableIndices to evaluate changed items on next update
|
|
*/
|
|
resetViewableIndices() {
|
|
this._viewableIndices = [];
|
|
}
|
|
|
|
/**
|
|
* Records that an interaction has happened even if there has been no scroll.
|
|
*/
|
|
recordInteraction() {
|
|
this._hasInteracted = true;
|
|
}
|
|
|
|
_onUpdateSync(
|
|
props: FrameMetricProps,
|
|
viewableIndicesToCheck: Array<number>,
|
|
onViewableItemsChanged: ({
|
|
changed: Array<ViewToken>,
|
|
viewableItems: Array<ViewToken>,
|
|
...
|
|
}) => void,
|
|
createViewToken: (
|
|
index: number,
|
|
isViewable: boolean,
|
|
props: FrameMetricProps,
|
|
) => ViewToken,
|
|
) {
|
|
// Filter out indices that have gone out of view since this call was scheduled.
|
|
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii =>
|
|
this._viewableIndices.includes(ii),
|
|
);
|
|
const prevItems = this._viewableItems;
|
|
const nextItems = new Map(
|
|
viewableIndicesToCheck.map(ii => {
|
|
const viewable = createViewToken(ii, true, props);
|
|
return [viewable.key, viewable];
|
|
}),
|
|
);
|
|
|
|
const changed = [];
|
|
for (const [key, viewable] of nextItems) {
|
|
if (!prevItems.has(key)) {
|
|
changed.push(viewable);
|
|
}
|
|
}
|
|
for (const [key, viewable] of prevItems) {
|
|
if (!nextItems.has(key)) {
|
|
changed.push({...viewable, isViewable: false});
|
|
}
|
|
}
|
|
if (changed.length > 0) {
|
|
this._viewableItems = nextItems;
|
|
onViewableItemsChanged({
|
|
viewableItems: Array.from(nextItems.values()),
|
|
changed,
|
|
viewabilityConfig: this._config,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
function _isViewable(
|
|
viewAreaMode: boolean,
|
|
viewablePercentThreshold: number,
|
|
top: number,
|
|
bottom: number,
|
|
viewportHeight: number,
|
|
itemLength: number,
|
|
): boolean {
|
|
if (_isEntirelyVisible(top, bottom, viewportHeight)) {
|
|
return true;
|
|
} else {
|
|
const pixels = _getPixelsVisible(top, bottom, viewportHeight);
|
|
const percent =
|
|
100 * (viewAreaMode ? pixels / viewportHeight : pixels / itemLength);
|
|
return percent >= viewablePercentThreshold;
|
|
}
|
|
}
|
|
|
|
function _getPixelsVisible(
|
|
top: number,
|
|
bottom: number,
|
|
viewportHeight: number,
|
|
): number {
|
|
const visibleHeight = Math.min(bottom, viewportHeight) - Math.max(top, 0);
|
|
return Math.max(0, visibleHeight);
|
|
}
|
|
|
|
function _isEntirelyVisible(
|
|
top: number,
|
|
bottom: number,
|
|
viewportHeight: number,
|
|
): boolean {
|
|
return top >= 0 && bottom <= viewportHeight && bottom > top;
|
|
}
|
|
|
|
module.exports = ViewabilityHelper;
|