fix requestIdleCallback in edge/safari (#198)

Fixes #195
This commit is contained in:
Nolan Lawson 2018-04-21 00:33:36 -07:00 committed by GitHub
parent 286a93ecba
commit ec138f94ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,15 +8,22 @@ import { mark, stop } from './marks'
const taskQueue = new Queue() const taskQueue = new Queue()
let runningRequestIdleCallback = false let runningRequestIdleCallback = false
const liteRIC = cb => setTimeout(() => cb({timeRemaining: () => Infinity})) // eslint-disable-line
function getRIC () { function getRIC () {
// we load polyfills asynchronously, so there's a tiny chance this is not defined // we load polyfills asynchronously, so there's a tiny chance this is not defined
return typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout return typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : liteRIC
} }
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()() let task = taskQueue.shift()
try {
task()
} catch (e) {
console.error(e)
}
} }
if (taskQueue.length) { if (taskQueue.length) {
let rIC = getRIC() let rIC = getRIC()