Simplifying how to determine requestAnimationFrame vs. requestIdleCallback
MODIFY
addon/scheduler.js
addon/scheduler.js
@@ -13,7 +13,7 @@ let _requestAnimationFrameEnabled;
let _requestIdleCallbackEnabled;
let _activeScheduledTasks = 0;
-export const REQUEST_IDLE_CALLBACK = 'requestIdleCallback';
+export const USE_REQUEST_IDLE_CALLBACK = true;
export const SIMPLE_CALLBACK = callback => callback();
reset();
@@ -102,12 +102,8 @@ export function _useRequestIdleCallback(
_requestIdleCallbackEnabled = rICEnabled;
}
-_useRequestAnimationFrame();
-_useRequestIdleCallback();
-_whenRoutePaintedScheduleFn = _getScheduleFn();
-_whenRouteIdleScheduleFn = _getScheduleFn(REQUEST_IDLE_CALLBACK);
-export function _getScheduleFn(scheduleType) {
- if (scheduleType === REQUEST_IDLE_CALLBACK && _requestIdleCallbackEnabled) {
+export function _getScheduleFn(useRequestIdleCallback = false) {
+ if (useRequestIdleCallback && _requestIdleCallbackEnabled) {
return requestIdleCallback;
} else if (_requestAnimationFrameEnabled) {
return requestAnimationFrame;
@@ -116,6 +112,11 @@ export function _getScheduleFn(scheduleType) {
}
}
+_useRequestAnimationFrame();
+_useRequestIdleCallback();
+_whenRoutePaintedScheduleFn = _getScheduleFn();
+_whenRouteIdleScheduleFn = _getScheduleFn(USE_REQUEST_IDLE_CALLBACK);
+
function _afterNextPaint(scheduleFn) {
let promise = new RSVP.Promise(resolve => {
if (DEBUG) {

MODIFY
tests/unit/scheduler-test.js
tests/unit/scheduler-test.js
@@ -10,7 +10,7 @@ import {
_useRequestAnimationFrame,
_useRequestIdleCallback,
_getScheduleFn,
- REQUEST_IDLE_CALLBACK,
+ USE_REQUEST_IDLE_CALLBACK,
SIMPLE_CALLBACK,
} from 'ember-app-scheduler/scheduler';
@@ -82,13 +82,19 @@ module('Unit | Scheduler', function(hooks) {
});
test('_getScheduleFn returns correct scheduleFn for requestIdleCallback', function(assert) {
- assert.equal(_getScheduleFn(REQUEST_IDLE_CALLBACK), requestIdleCallback);
+ assert.equal(
+ _getScheduleFn(USE_REQUEST_IDLE_CALLBACK),
+ requestIdleCallback
+ );
});
test('_getScheduleFn falls back to requestAnimationFrame if requestIdleCallback not available', function(assert) {
_useRequestIdleCallback(false);
- assert.equal(_getScheduleFn(REQUEST_IDLE_CALLBACK), requestAnimationFrame);
+ assert.equal(
+ _getScheduleFn(USE_REQUEST_IDLE_CALLBACK),
+ requestAnimationFrame
+ );
});
test('_getScheduleFn returns simple callback if requestAnimationFrame not available', function(assert) {

Simplifying how to determine requestAnimationFrame vs. requestIdleCallback