Skip to content

Commit

Permalink
Merge pull request missive#300 from nolanlawson/nolan/simplify-rIC
Browse files Browse the repository at this point in the history
fix: improve requestIdleCallback usage
  • Loading branch information
nolanlawson committed Mar 13, 2019
2 parents 5984122 + d1978c0 commit 1e6f956
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,20 @@ function measureScrollbar() {
// Use requestIdleCallback() if available, else fall back to setTimeout().
// Throttle so as not to run too frequently.
function throttleIdleTask(func) {
const queue =
const doIdleTask =
typeof requestIdleCallback === 'function' ? requestIdleCallback : setTimeout
const clear =
typeof cancelIdleCallback === 'function' ? cancelIdleCallback : clearTimeout

let id
let running = false

return function throttled() {
if (id) {
clear(id)
if (running) {
return
}
id = queue(func)
running = true
doIdleTask(() => {
running = false
func()
})
}
}

Expand Down

0 comments on commit 1e6f956

Please sign in to comment.