/**
 * Since events can fire at a high rate, the event handler shouldn't execute computationally expensive operations such as DOM modifications.
 */
const throttleEvent = (handleEvent: (e?: Event) => void) => {
  let ticking = false;
  return (e?: Event) => {
    if (!ticking) {
      window.requestAnimationFrame(() => {
        e ? handleEvent(e) : handleEvent();
        ticking = false;
      });
      ticking = true;
    }
  };
};

export default throttleEvent;
