Skip to content

Commit ddbbfcc

Browse files
authored
Dont batch reads and writes in same rAF (#219)
1 parent 9c3bfe1 commit ddbbfcc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

addon/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { setupRouter, reset, whenRouteIdle, whenRoutePainted } from 'ember-app-s
77
import { gte } from 'ember-compatibility-helpers';
88
import { getScrollBarWidth } from './utils/scrollbar-width';
99

10-
let scrollBarWidth = getScrollBarWidth();
1110
const body = document.body;
1211
const html = document.documentElement;
1312
let ATTEMPTS = 0;
1413
const MAX_ATTEMPTS = 100; // rAF runs every 16ms ideally, so 60x a second
1514
let requestId;
15+
let scrollBarWidth = 0;
1616

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

34-
if (documentWidth + scrollBarWidth - window.innerWidth >= scrollHash.x
35-
&& documentHeight + scrollBarWidth - window.innerHeight >= scrollHash.y
35+
requestId = window.requestAnimationFrame(() => {
36+
// write DOM (scrollTo causes reflow)
37+
if (documentWidth + scrollBarWidth - innerWidth >= scrollHash.x
38+
&& documentHeight + scrollBarWidth - innerHeight >= scrollHash.y
3639
|| ATTEMPTS >= MAX_ATTEMPTS) {
3740
ATTEMPTS = 0;
3841
fn.call(null, scrollHash.x, scrollHash.y);
@@ -65,6 +68,9 @@ let RouterScrollMixin = Mixin.create({
6568
this._routeDidChange(transition);
6669
});
6770
}
71+
if (!get(this, 'isFastBoot')) {
72+
scrollBarWidth = getScrollBarWidth();
73+
}
6874
},
6975

7076
destroy() {

0 commit comments

Comments
 (0)