Skip to content

Commit

Permalink
refactor: migrate EmitterWrapped to ES2015
Browse files Browse the repository at this point in the history
  • Loading branch information
devoto13 committed Mar 15, 2018
1 parent 9b472c4 commit 0e6d46c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
49 changes: 28 additions & 21 deletions lib/emitter_wrapper.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
function EmitterWrapper (emitter) {
this.listeners = {}
this.emitter = emitter
}

EmitterWrapper.prototype.addListener = EmitterWrapper.prototype.on = function (event, listener) {
this.emitter.addListener(event, listener)
'use strict'

if (!this.listeners.hasOwnProperty(event)) {
this.listeners[event] = []
class EmitterWrapper {
constructor (emitter) {
this.listeners = {}
this.emitter = emitter
}

this.listeners[event].push(listener)
addListener (event, listener) {
this.emitter.addListener(event, listener)

return this
}
if (!this.listeners.hasOwnProperty(event)) {
this.listeners[event] = []
}

EmitterWrapper.prototype.removeAllListeners = function (event) {
var events = event ? [event] : Object.keys(this.listeners)
var self = this
events.forEach(function (event) {
self.listeners[event].forEach(function (listener) {
self.emitter.removeListener(event, listener)
this.listeners[event].push(listener)

return this
}

on (event, listener) {
return this.addListener(event, listener)
}

removeAllListeners (event) {
const events = event ? [event] : Object.keys(this.listeners)
events.forEach((event) => {
this.listeners[event].forEach((listener) => {
this.emitter.removeListener(event, listener)
})
delete this.listeners[event]
})
delete self.listeners[event]
})

return this
return this
}
}

module.exports = EmitterWrapper
15 changes: 8 additions & 7 deletions test/unit/emitter_wrapper.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var EventEmitter = require('events').EventEmitter
'use strict'

var EmitterWrapper = require('../../lib/emitter_wrapper')
const EventEmitter = require('events').EventEmitter
const EmitterWrapper = require('../../lib/emitter_wrapper')

describe('emitter_wrapper', () => {
var emitter
var wrapped
let emitter
let wrapped

beforeEach(() => {
emitter = new EventEmitter()
Expand All @@ -14,7 +15,7 @@ describe('emitter_wrapper', () => {
})

describe('addListener', () => {
var aListener = (e) => true
const aListener = (e) => true

it('should add a listener to the wrapped emitter', () => {
wrapped.addListener('anEvent', aListener)
Expand All @@ -27,7 +28,7 @@ describe('emitter_wrapper', () => {
})

describe('removeAllListeners', () => {
var aListener = (e) => true
const aListener = (e) => true

beforeEach(() => {
wrapped.addListener('anEvent', aListener)
Expand All @@ -44,7 +45,7 @@ describe('emitter_wrapper', () => {
})

it('should remove only matching listeners when called with an event name', () => {
var anotherListener = (e) => true
const anotherListener = (e) => true
wrapped.addListener('anotherEvent', anotherListener)
wrapped.removeAllListeners('anEvent')
expect(emitter.listeners('anEvent')).not.to.contain(aListener)
Expand Down

0 comments on commit 0e6d46c

Please sign in to comment.