/**
 * Throttle the event for better performance, using RAF (default is 60 FPS)
 * https://gist.github.com/addyosmani/5434533
 * http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
 * https://developer.mozilla.org/en-US/docs/Web/Events/resize
 */
export default function throttle(callback, fps) {
  let then = window.performance ? window.performance.now() : Date.now();
  let interval = 1000 / (fps || 60);

  return function onThrottleAnimation() {
    let now = window.performance ? window.performance.now() : Date.now();
    let delta = now - then;

    if (delta > interval) {
      // now - (delta % interval) is an improvement over just
      // using then = now, which can end up lowering overall fps
      then = now - (delta % interval);
      window.requestAnimationFrame(callback);
    }
  };
}
