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

Dont batch reads and writes in same rAF #219

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Changes from all 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
22 changes: 14 additions & 8 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { setupRouter, reset, whenRouteIdle, whenRoutePainted } from 'ember-app-s
import { gte } from 'ember-compatibility-helpers';
import { getScrollBarWidth } from './utils/scrollbar-width';

let scrollBarWidth = getScrollBarWidth();
const body = document.body;
const html = document.documentElement;
let ATTEMPTS = 0;
const MAX_ATTEMPTS = 100; // rAF runs every 16ms ideally, so 60x a second
let requestId;
let scrollBarWidth = 0;

/**
* By default, we start checking to see if the document height is >= the last known `y` position
Expand All @@ -25,14 +25,17 @@ let requestId;
* @void
*/
function tryScrollRecursively(fn, scrollHash) {
requestId = window.requestAnimationFrame(() => {
const documentWidth = Math.max(body.scrollWidth, body.offsetWidth,
html.clientWidth, html.scrollWidth, html.offsetWidth);
const documentHeight = Math.max(body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight);
// read DOM outside of rAF
const documentWidth = Math.max(body.scrollWidth, body.offsetWidth,
html.clientWidth, html.scrollWidth, html.offsetWidth);
const documentHeight = Math.max(body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight);
const { innerHeight, innerWidth } = window;

if (documentWidth + scrollBarWidth - window.innerWidth >= scrollHash.x
&& documentHeight + scrollBarWidth - window.innerHeight >= scrollHash.y
requestId = window.requestAnimationFrame(() => {
// write DOM (scrollTo causes reflow)
if (documentWidth + scrollBarWidth - innerWidth >= scrollHash.x
&& documentHeight + scrollBarWidth - innerHeight >= scrollHash.y
|| ATTEMPTS >= MAX_ATTEMPTS) {
ATTEMPTS = 0;
fn.call(null, scrollHash.x, scrollHash.y);
Expand Down Expand Up @@ -65,6 +68,9 @@ let RouterScrollMixin = Mixin.create({
this._routeDidChange(transition);
});
}
if (!get(this, 'isFastBoot')) {
scrollBarWidth = getScrollBarWidth();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

}
},

destroy() {
Expand Down