mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-22 11:16:06 +08:00
Simplify scroll interaction stuff
Reviewed By: angelahess Differential Revision: D4662787 fbshipit-source-id: 6e28f8cacd08601254e70f30ab98ee27bd08cc5a
This commit is contained in:
committed by
Facebook Github Bot
parent
28b43aa05c
commit
9325496d46
@@ -42,15 +42,6 @@ export type ViewabilityConfig = {|
|
||||
* render.
|
||||
*/
|
||||
waitForInteraction?: boolean,
|
||||
|
||||
/**
|
||||
* Criteria to filter out certain scroll events so they don't count as interactions. By default,
|
||||
* any non-zero scroll offset will be considered an interaction.
|
||||
*/
|
||||
scrollInteractionFilter?: {|
|
||||
minimumOffset?: number, // scrolls with an offset less than this are ignored.
|
||||
minimumElapsed?: number, // scrolls that happen before this are ignored.
|
||||
|},
|
||||
|};
|
||||
|
||||
/**
|
||||
@@ -74,10 +65,6 @@ class ViewabilityHelper {
|
||||
_viewableItems: Map<string, ViewToken> = new Map();
|
||||
|
||||
constructor(config: ViewabilityConfig = {viewAreaCoveragePercentThreshold: 0}) {
|
||||
invariant(
|
||||
config.scrollInteractionFilter == null || config.waitForInteraction,
|
||||
'scrollInteractionFilter only works in conjunction with waitForInteraction',
|
||||
);
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
@@ -163,17 +150,6 @@ class ViewabilityHelper {
|
||||
this._lastUpdateTime = updateTime;
|
||||
}
|
||||
const updateElapsed = this._lastUpdateTime ? updateTime - this._lastUpdateTime : 0;
|
||||
if (this._config.waitForInteraction && !this._hasInteracted && scrollOffset !== 0) {
|
||||
const filter = this._config.scrollInteractionFilter;
|
||||
if (filter) {
|
||||
if ((filter.minimumOffset == null || scrollOffset >= filter.minimumOffset) &&
|
||||
(filter.minimumElapsed == null || updateElapsed >= filter.minimumElapsed)) {
|
||||
this._hasInteracted = true;
|
||||
}
|
||||
} else {
|
||||
this._hasInteracted = true;
|
||||
}
|
||||
}
|
||||
if (this._config.waitForInteraction && !this._hasInteracted) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -407,6 +407,7 @@ class VirtualizedList extends React.PureComponent<OptionalProps, Props, State> {
|
||||
onContentSizeChange: this._onContentSizeChange,
|
||||
onLayout: this._onLayout,
|
||||
onScroll: this._onScroll,
|
||||
onScrollBeginDrag: this._onScrollBeginDrag,
|
||||
ref: this._captureScrollRef,
|
||||
scrollEventThrottle: 50, // TODO: Android support
|
||||
},
|
||||
@@ -604,6 +605,10 @@ class VirtualizedList extends React.PureComponent<OptionalProps, Props, State> {
|
||||
this._updateCellsToRenderBatcher.schedule();
|
||||
};
|
||||
|
||||
_onScrollBeginDrag = (e): void => {
|
||||
this._viewabilityHelper.recordInteraction();
|
||||
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e);
|
||||
};
|
||||
_updateCellsToRender = () => {
|
||||
const {data, disableVirtualization, getItemCount, onEndReachedThreshold} = this.props;
|
||||
this._updateViewableItems(data);
|
||||
|
||||
@@ -316,14 +316,11 @@ describe('onUpdate', function() {
|
||||
);
|
||||
|
||||
it(
|
||||
'waitForInteraction blocks callback until scroll',
|
||||
'waitForInteraction blocks callback until interaction',
|
||||
function() {
|
||||
const helper = new ViewabilityHelper({
|
||||
waitForInteraction: true,
|
||||
viewAreaCoveragePercentThreshold: 0,
|
||||
scrollInteractionFilter: {
|
||||
minimumOffset: 20,
|
||||
},
|
||||
});
|
||||
rowFrames = {
|
||||
a: {y: 0, height: 200},
|
||||
@@ -340,15 +337,9 @@ describe('onUpdate', function() {
|
||||
onViewableItemsChanged,
|
||||
);
|
||||
expect(onViewableItemsChanged).not.toBeCalled();
|
||||
helper.onUpdate(
|
||||
data.length,
|
||||
10, // not far enough to meet minimumOffset
|
||||
100,
|
||||
getFrameMetrics,
|
||||
createViewToken,
|
||||
onViewableItemsChanged,
|
||||
);
|
||||
expect(onViewableItemsChanged).not.toBeCalled();
|
||||
|
||||
helper.recordInteraction();
|
||||
|
||||
helper.onUpdate(
|
||||
data.length,
|
||||
20,
|
||||
|
||||
Reference in New Issue
Block a user