All files 6.发布订阅.js

100% Statements 13/13
66.66% Branches 4/6
100% Functions 6/6
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29    3x       4x 3x   4x 4x       3x 3x   3x       1x 1x 2x     1x    
export class EventEmitter {
  constructor() {
    this.events = {};
  }
 
  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
    return this;
  }
 
  emit(eventName,...args) {
    Eif (this.events[eventName]) {
      this.events[eventName].forEach(callback => callback(...args));
    }
    return this;
  }
 
  off(eventName, callback) {
    Eif (this.events[eventName]) {
      this.events[eventName] = this.events[eventName].filter(
        cb => cb!== callback
      );
    }
    return this;
  }
}