
import { throttle } from 'lodash'

class WinResetEvent {
  eventStack: any
  funcId: number
  constructor(){
    this.eventStack = {};
    this.funcId = 0;
    this.reset()
  }

  addEvent(fn: Function, context: any=null, delay: number=200){
    this.funcId++;
    this.eventStack[this.funcId] = throttle(fn.bind(context), delay)
    return this.funcId
  }

  removeEvent(funcId: number){
    delete this.eventStack[funcId]
  }
  
  reset(){
    window.addEventListener('resize', () => {
      Object.values(this.eventStack).forEach(cb=>{
        typeof cb === 'function' && cb()
      })
    })
  }
}

export default new WinResetEvent();
