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
1 changed files with 9 additions and 2 deletions

View File

@ -8,13 +8,19 @@ import { mark, stop } from './marks'
const taskQueue = new Queue()
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) {
mark('scheduleIdleTask:runTasks()')
while (taskQueue.length && deadline.timeRemaining() > 0) {
taskQueue.shift()()
}
if (taskQueue.length) {
requestIdleCallback(runTasks)
let rIC = getRIC()
rIC(runTasks)
} else {
runningRequestIdleCallback = false
}
@ -25,6 +31,7 @@ export function scheduleIdleTask (task) {
taskQueue.push(task)
if (!runningRequestIdleCallback) {
runningRequestIdleCallback = true
requestIdleCallback(runTasks)
let rIC = getRIC()
rIC(runTasks)
}
}