resize_counter/ResizeCounter.js


/**
  Counts number of times user has resized window in a session.
  Resizing is debounded to prevent too many calls.
  @memberof module:Viki
**/
class ResizeCounter {
  /**
    @constructor
    @param {function} callback to fire on every resize
  **/
  constructor(onResizeCallback) {
    this.timeout = null;
    this.count = 0;
    // Debounce resizing
    window.addEventListener('resize', () => {
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        onResizeCallback();
        this.count++;
      }, 300);
    });
  }

  /**
    @returns {number} number of times user has resized window
  **/
  getCount() {
    return this.count;
  }
}

module.exports = ResizeCounter;