Permalink
Browse files

Simplify scroll interaction stuff

Reviewed By: angelahess

Differential Revision: D4662787

fbshipit-source-id: 6e28f8cacd08601254e70f30ab98ee27bd08cc5a
  • Loading branch information...
sahrens committed Mar 7, 2017
1 parent ce0d641 commit c8b1c73bd45182cb37f3e97fd0aa2f368910ef2b
@@ -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;
}
@@ -415,6 +415,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
},
@@ -609,6 +610,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,

0 comments on commit c8b1c73

Please sign in to comment.