-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web-server): detach listeners after running
Remove listeners from the global process and the webserver when we're done. The EmitterWrapper can be used to wrap any EventEmitter and remembers the listeners that were via the wrappers addListener/on function. Then the wrapper's removeAllListeners function can be used to remove the wrapper's listeners, leaving the listeners that were added directly to the unwrapped emitter attached.
- Loading branch information
1 parent
f9dee46
commit 3baa8e1
Showing
3 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function EmitterWrapper(emitter) { | ||
this.listeners = {}; | ||
this.emitter = emitter; | ||
} | ||
|
||
EmitterWrapper.prototype.addListener = EmitterWrapper.prototype.on = function (event, listener) { | ||
this.emitter.addListener(event, listener); | ||
|
||
if (!this.listeners.hasOwnProperty(event)) { | ||
this.listeners[event] = []; | ||
} | ||
|
||
this.listeners[event].push(listener); | ||
|
||
return this; | ||
}; | ||
|
||
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); | ||
}); | ||
delete self.listeners[event]; | ||
}); | ||
|
||
return this; | ||
}; | ||
|
||
module.exports = EmitterWrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#============================================================================== | ||
# lib/emitter_wrapper.js module | ||
#============================================================================== | ||
describe 'emitter_wrapper', -> | ||
EmitterWrapper = require '../../lib/emitter_wrapper' | ||
events = require 'events' | ||
EventEmitter = events.EventEmitter | ||
|
||
emitter = null | ||
wrapped = null | ||
called = false | ||
|
||
beforeEach -> | ||
emitter = new EventEmitter() | ||
emitter.aMethod = (e) -> called = true | ||
emitter.on 'anEvent', emitter.aMethod | ||
wrapped = new EmitterWrapper(emitter) | ||
|
||
#=========================================================================== | ||
# wrapper.addListener | ||
#=========================================================================== | ||
describe 'addListener', -> | ||
aListener = (e) -> true | ||
|
||
it 'should add a listener to the wrapped emitter', -> | ||
wrapped.addListener 'anEvent', aListener | ||
expect(emitter.listeners('anEvent')).to.contain aListener | ||
|
||
it 'returns the wrapped emitter', -> | ||
expect(wrapped.addListener 'anEvent', aListener).to.equal wrapped | ||
|
||
#=========================================================================== | ||
# wrapper.removeAllListeners | ||
#=========================================================================== | ||
describe 'removeAllListeners', -> | ||
aListener = (e) -> true | ||
|
||
beforeEach -> | ||
wrapped.addListener 'anEvent', aListener | ||
|
||
it 'should remove listeners that were attached via the wrapper', -> | ||
wrapped.removeAllListeners() | ||
expect(emitter.listeners('anEvent')).not.to.contain aListener | ||
|
||
it 'should not remove listeners that were attached to the original emitter', -> | ||
wrapped.removeAllListeners() | ||
expect(emitter.listeners('anEvent')).to.contain emitter.aMethod | ||
|
||
it 'should remove only matching listeners when called with an event name', -> | ||
anotherListener = (e) -> true | ||
wrapped.addListener 'anotherEvent', anotherListener | ||
wrapped.removeAllListeners('anEvent') | ||
expect(emitter.listeners('anEvent')).not.to.contain aListener | ||
expect(emitter.listeners('anotherEvent')).to.contain anotherListener | ||
|
||
it 'returns the wrapped emitter', -> | ||
expect(wrapped.addListener 'anEvent', aListener).to.equal wrapped |