event.js 801 Bytes
export default {
  listeners: [],
  on(type, handle) {
    // first remove and add
    this.off(type, handle);

    if (typeof handle === 'function') {
      console.debug(`listen event ${type}`);
      this.listeners.push([type, handle]);
    }
  },
  off(type, handle) {
    let index = -1;

    if (typeof handle === 'function' &&
      this.listeners.some(([itype, ihandle]) => {
        index++;
        return itype === type && handle.name && ihandle.name === handle.name;
      })
    ) {
      this.listeners.splice(index, 1);
    }
  },
  emit(type, ...params) {
    this.listeners.forEach(([listenType, handle]) => type === listenType && handle(...params));
    console.debug(`receive event ${type}: ${JSON.stringify(params)}`);
  },
  removeAllListeners() {
    this.listeners = [];
  }
};