const TIMER_ID_KEY = Symbol();
const throttle = (delay) => {
  return (_target, _propertyKey, descriptor) => {
    const fn = descriptor.value;
    let pendingFn;
    descriptor.value = function(...args) {
      const startTimer = () => {
        this[TIMER_ID_KEY] = window.setTimeout(() => {
          if (pendingFn) {
            pendingFn();
            pendingFn = void 0;
            startTimer();
          } else {
            this[TIMER_ID_KEY] = 0;
          }
        }, delay);
      };
      if (this[TIMER_ID_KEY]) {
        pendingFn = () => fn.apply(this, args);
      } else {
        fn.apply(this, args);
        startTimer();
      }
    };
  };
};

export { throttle };
