fix requestIdleCallback not defined in Edge (#180)

This commit is contained in:
Nolan Lawson 2018-04-19 10:35:34 -07:00 committed by GitHub
parent 682cb6f40e
commit 79b848df2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,13 +8,19 @@ import { mark, stop } from './marks'
const taskQueue = new Queue() const taskQueue = new Queue()
let runningRequestIdleCallback = false let runningRequestIdleCallback = false
function getRIC () {
// we load polyfills asynchronously, so there's a tiny chance this is not defined
return typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout
}
function runTasks (deadline) { function runTasks (deadline) {
mark('scheduleIdleTask:runTasks()') mark('scheduleIdleTask:runTasks()')
while (taskQueue.length && deadline.timeRemaining() > 0) { while (taskQueue.length && deadline.timeRemaining() > 0) {
taskQueue.shift()() taskQueue.shift()()
} }
if (taskQueue.length) { if (taskQueue.length) {
requestIdleCallback(runTasks) let rIC = getRIC()
rIC(runTasks)
} else { } else {
runningRequestIdleCallback = false runningRequestIdleCallback = false
} }
@ -25,6 +31,7 @@ export function scheduleIdleTask (task) {
taskQueue.push(task) taskQueue.push(task)
if (!runningRequestIdleCallback) { if (!runningRequestIdleCallback) {
runningRequestIdleCallback = true runningRequestIdleCallback = true
requestIdleCallback(runTasks) let rIC = getRIC()
rIC(runTasks)
} }
} }