All files / src/libs event-bus.js

63.64% Statements 7/11
25% Branches 2/8
80% Functions 4/5
63.64% Lines 7/11

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    2x       2x 2x 2x 2x                         2x     2x    
export class EventBus {
  constructor() {
    this.store = new Map()
  }
 
  on(type, handler) {
    const handlers = this.store.get(type)
    const added = handlers && handlers.push(handler)
    Eif (!added) {
      this.store.set(type, [handler])
    }
  }
 
  off(type, handler) {
    const handlers = this.store.get(type)
    const index = handlers.indexOf(handler)
    if (handlers && index > -1) {
      handlers.splice(index, 1)
    }
  }
 
  emit(type, payload) {
    this.store
      .get(type)
      .slice()
      .map((handler) => handler(payload))
  }
}