Fixing nasty bug that doesn't detect rAF correctly. Changed getScheduleFn to run eagerly
MODIFY
addon/scheduler.js
addon/scheduler.js
@@ -6,10 +6,13 @@ import { registerWaiter } from '@ember/test';
const APP_SCHEDULER_HAS_SETUP = '__APP_SCHEDULER_HAS_SETUP__';
let _didTransition;
let _whenRoutePainted;
+let _whenRoutePaintedScheduleFn;
let _whenRouteIdle;
+let _whenRouteIdleScheduleFn;
let _requestAnimationFrameEnabled;
let _requestIdleCallbackEnabled;
let _activeScheduledTasks = 0;
+
export const REQUEST_IDLE_CALLBACK = 'requestIdleCallback';
export const SIMPLE_CALLBACK = callback => callback();
@@ -19,10 +22,10 @@ export function beginTransition() {
if (_didTransition.isResolved) {
_didTransition = _defer();
_whenRoutePainted = _didTransition.promise.then(() =>
- _afterNextPaint(getScheduleFn())
+ _afterNextPaint(_whenRoutePaintedScheduleFn)
);
_whenRouteIdle = _whenRoutePainted.then(() =>
- _afterNextPaint(getScheduleFn(REQUEST_IDLE_CALLBACK))
+ _afterNextPaint(_whenRouteIdleScheduleFn)
);
}
}
@@ -87,20 +90,32 @@ export function routeSettled() {
return _whenRouteIdle;
}
-useRequestAnimationFrame();
-export function useRequestAnimationFrame(
+_useRequestAnimationFrame();
+export function _useRequestAnimationFrame(
rAFEnabled = typeof requestAnimationFrame === 'function'
) {
_requestAnimationFrameEnabled = rAFEnabled;
}
-_requestIdleCallbackEnabled = useRequestIdleCallback();
-export function useRequestIdleCallback(
+_useRequestIdleCallback();
+export function _useRequestIdleCallback(
rICEnabled = typeof requestIdleCallback === 'function'
) {
_requestIdleCallbackEnabled = rICEnabled;
}
+_whenRoutePaintedScheduleFn = _getScheduleFn();
+_whenRouteIdleScheduleFn = _getScheduleFn(REQUEST_IDLE_CALLBACK);
+export function _getScheduleFn(scheduleType) {
+ if (scheduleType === REQUEST_IDLE_CALLBACK && _requestIdleCallbackEnabled) {
+ return requestIdleCallback;
+ } else if (_requestAnimationFrameEnabled) {
+ return requestAnimationFrame;
+ } else {
+ return SIMPLE_CALLBACK;
+ }
+}
+
function _afterNextPaint(scheduleFn) {
let promise = new RSVP.Promise(resolve => {
if (DEBUG) {
@@ -126,16 +141,6 @@ if (DEBUG) {
registerWaiter(() => _activeScheduledTasks === 0);
}
-export function getScheduleFn(scheduleType) {
- if (scheduleType == REQUEST_IDLE_CALLBACK && _requestIdleCallbackEnabled) {
- return requestIdleCallback;
- } else if (_requestAnimationFrameEnabled) {
- return requestAnimationFrame;
- } else {
- return SIMPLE_CALLBACK;
- }
-}
-
function _defer(label) {
let deferred = { resolve: undefined, reject: undefined };

MODIFY
tests/unit/scheduler-test.js
tests/unit/scheduler-test.js
@@ -7,9 +7,9 @@ import {
endTransition,
} from 'ember-app-scheduler';
import {
- useRequestAnimationFrame,
- useRequestIdleCallback,
- getScheduleFn,
+ _useRequestAnimationFrame,
+ _useRequestIdleCallback,
+ _getScheduleFn,
REQUEST_IDLE_CALLBACK,
SIMPLE_CALLBACK,
} from 'ember-app-scheduler/scheduler';
@@ -19,8 +19,8 @@ const REQUEST_ANIMATION_FRAME = requestAnimationFrame;
module('Unit | Scheduler', function(hooks) {
hooks.afterEach(function() {
reset();
- useRequestAnimationFrame();
- useRequestIdleCallback();
+ _useRequestAnimationFrame();
+ _useRequestIdleCallback();
window.requestAnimationFrame = REQUEST_ANIMATION_FRAME;
});
@@ -43,7 +43,7 @@ module('Unit | Scheduler', function(hooks) {
test('whenRouteIdle resolves when transition ended when requestAnimationFrame not available', async function(assert) {
assert.expect(1);
- useRequestAnimationFrame(false);
+ _useRequestAnimationFrame(false);
window.requestAnimationFrame = () =>
assert.ok(false, 'requestAnimationFrame was used');
beginTransition();
@@ -81,20 +81,20 @@ module('Unit | Scheduler', function(hooks) {
assert.verifySteps(['first whenRouteIdle', 'second whenRouteIdle']);
});
- test('getScheduleFn returns correct scheduleFn for requestIdleCallback', function(assert) {
- assert.equal(getScheduleFn(REQUEST_IDLE_CALLBACK), requestIdleCallback);
+ test('_getScheduleFn returns correct scheduleFn for requestIdleCallback', function(assert) {
+ assert.equal(_getScheduleFn(REQUEST_IDLE_CALLBACK), requestIdleCallback);
});
- test('getScheduleFn falls back to requestAnimationFrame if requestIdleCallback not available', function(assert) {
- useRequestIdleCallback(false);
+ test('_getScheduleFn falls back to requestAnimationFrame if requestIdleCallback not available', function(assert) {
+ _useRequestIdleCallback(false);
- assert.equal(getScheduleFn(REQUEST_IDLE_CALLBACK), requestAnimationFrame);
+ assert.equal(_getScheduleFn(REQUEST_IDLE_CALLBACK), requestAnimationFrame);
});
- test('getScheduleFn returns simple callback if requestAnimationFrame not available', function(assert) {
- useRequestIdleCallback(false);
- useRequestAnimationFrame(false);
+ test('_getScheduleFn returns simple callback if requestAnimationFrame not available', function(assert) {
+ _useRequestIdleCallback(false);
+ _useRequestAnimationFrame(false);
- assert.equal(getScheduleFn(), SIMPLE_CALLBACK);
+ assert.equal(_getScheduleFn(), SIMPLE_CALLBACK);
});
});

Fixing nasty bug that doesn't detect rAF correctly. Changed getScheduleFn to run eagerly