Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving detection of scroll ended #742

Merged
merged 13 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions source/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export default class Grid extends PureComponent {
_rowStopIndex: number;

_disablePointerEventsTimeoutId: ?number;
_scrollDebounceStart: number;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -1124,21 +1125,33 @@ export default class Grid extends PureComponent {
}
}

/**
* Check if the difference between current time and the last scroll ended event is greater.
* than the scrollingResetTimeInterval prop, else schedule this function to execute again.
*/
_delayScrollEnded = () => {
const { scrollingResetTimeInterval } = this.props;
if (Date.now() - this._scrollDebounceStart >= scrollingResetTimeInterval) {
this._debounceScrollEndedCallback();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: I realize we weren't doing this before, and it's no big deal, but we might as well clear this._disablePointerEventsTimeoutId here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree that the pointer must be cleared, but this hasn't been doing on _debounceScrollEndedCallback function?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right 😄 Sorry, I was just skimming the diff and that callback wasn't visible. Sorry~ 👍

} else {
this._disablePointerEventsTimeoutId = window.requestAnimationFrame(
this._delayScrollEnded
);
}
};

/**
* Sets an :isScrolling flag for a small window of time.
* This flag is used to disable pointer events on the scrollable portion of the Grid.
* This prevents jerky/stuttery mouse-wheel scrolling.
*/
_debounceScrollEnded() {
const { scrollingResetTimeInterval } = this.props;

if (this._disablePointerEventsTimeoutId) {
clearTimeout(this._disablePointerEventsTimeoutId);
window.cancelAnimationFrame(this._disablePointerEventsTimeoutId);
}

this._disablePointerEventsTimeoutId = setTimeout(
this._debounceScrollEndedCallback,
scrollingResetTimeInterval
this._scrollDebounceStart = Date.now();
this._disablePointerEventsTimeoutId = window.requestAnimationFrame(
this._delayScrollEnded
);
}

Expand Down
6 changes: 3 additions & 3 deletions source/Masonry/Masonry.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ describe("Masonry", () => {
});

it("should be reset after a small debounce when scrolling stops", () => {
jest.useFakeTimers();
const cellMeasurerCache = createCellMeasurerCache();
const renderCallback = jest.fn().mockImplementation(index => index);
const cellRenderer = createCellRenderer(
Expand All @@ -300,8 +299,9 @@ describe("Masonry", () => {
);
simulateScroll(rendered, 51);
renderCallback.mockClear();
jest.runOnlyPendingTimers();
expect(renderCallback.mock.calls[0][1].isScrolling).toEqual(false);
setTimeout(() => {
expect(renderCallback.mock.calls[0][1].isScrolling).toEqual(false);
}, 0);
});
});

Expand Down
25 changes: 19 additions & 6 deletions source/Masonry/Masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default class Masonry extends PureComponent {
);
this._setScrollingContainerRef = this._setScrollingContainerRef.bind(this);
this._onScroll = this._onScroll.bind(this);
this._delayScrollEnded = this._delayScrollEnded.bind(this);
}

clearCellPositions() {
Expand Down Expand Up @@ -290,16 +291,28 @@ export default class Masonry extends PureComponent {
}
}

_debounceResetIsScrolling() {
/**
* Check if the difference between current time and the last scroll ended event is greater.
* than the scrollingResetTimeInterval prop, else schedule this function to execute again.
*/
_delayScrollEnded() {
const { scrollingResetTimeInterval } = this.props;
if (Date.now() - this._scrollDebounceStart >= scrollingResetTimeInterval) {
this._debounceScrollEndedCallback();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto as for Grid

} else {
this._disablePointerEventsTimeoutId = window.requestAnimationFrame(
this._delayScrollEnded
);
}
}

_debounceResetIsScrolling() {
if (this._debounceResetIsScrollingId) {
clearTimeout(this._debounceResetIsScrollingId);
window.cancelAnimationFrame(this._debounceResetIsScrollingId);
}

this._debounceResetIsScrollingId = setTimeout(
this._debounceResetIsScrollingCallback,
scrollingResetTimeInterval
this._scrollDebounceStart = Date.now();
this._debounceResetIsScrollingId = window.requestAnimationFrame(
this._delayScrollEnded
);
}

Expand Down
2 changes: 1 addition & 1 deletion source/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ jest.mock("dom-helpers/util/scrollbarSize", () => {
});

// Polyfill requestAnimationFrame() for ReactDOMFrameScheduling
global.requestAnimationFrame = require("raf");
require("raf").polyfill();