import { UnknownFunction } from '../Types'

/* eslint-disable prefer-rest-params */
export default function repaint(fn: UnknownFunction): UnknownFunction {
  let requestId: number | undefined

  return function (this: UnknownFunction) {
    if (requestId) {
      window.cancelAnimationFrame(requestId)
    }

    requestId = window.requestAnimationFrame(() => {
      requestId = undefined
      fn.apply(this, [...arguments])
    })
  }
}
