class CEvent {
  constructor () {
    this.events = {}
  }

  // 消息订阅
  on (type, fn) {
    const fns = this.events[type]
    if (fns) {
      fns.push(fn)
    } else {
      this.events[type] = []
      this.events[type].push(fn)
    }
    return this
  }

  // 消息发布
  notify (type, data) {
    const fns = this.events[type]
    if (fns) {
      fns.forEach(fn => {
        fn(data)
      })
    }
  }

  // 移除订阅
  remove (type, fn) {
    if (this.events[type] && this.events[type].length) {
      this.events[type] = this.events[type].filter(item => {
        return item !== fn
      })
    }
  }
}

export const cEvent = new CEvent()

export default CEvent
