All files rafThrottle.js

100% Statements 14/14
100% Branches 2/2
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 7x       9x 7x 7x     7x 14x 14x 9x       7x 2x 2x     7x        
const rafThrottle = callback => {
  let requestId = null
 
  let lastArgs
 
  const later = (context) => () => {
    requestId = null
    callback.apply(context, lastArgs)
  }
 
  const throttled = function(...args) {
    lastArgs = args;
    if (requestId === null) {
      requestId = requestAnimationFrame(later(this))
    }
  }
 
  throttled.cancel = () => {
    cancelAnimationFrame(requestId)
    requestId = null
  }
 
  return throttled
}
 
export default rafThrottle