Skip to content

Instantly share code, notes, and snippets.

@attomos
Created April 24, 2014 17:33
Show Gist options
  • Select an option

  • Save attomos/11262853 to your computer and use it in GitHub Desktop.

Select an option

Save attomos/11262853 to your computer and use it in GitHub Desktop.
var EventEmitter = require('events').EventEmitter
var emitter = new EventEmitter()
var util = require('util')
emitter.on('newListener', newListener)
function newListener() {
console.log('new listener added')
}
emitter.on('removeListener', removeListener)
function removeListener() {
console.log('removed listener')
}
emitter.on('connection #1', function() {
console.log('got connection #1')
}).once('connection #1', function() {
// get call only once and be removed immediately
console.log('first connection #1')
})
emitter.on('connection #2', connection2)
emitter.on('connection #3', connection3)
function connection2() {
console.log('got connection #2')
}
function connection3() {
console.log('got connection #3')
}
emitter.emit('connection #1')
emitter.emit('connection #1')
emitter.emit('connection #1')
emitter.emit('connection #2')
emitter.emit('connection #3')
// emitter.removeAllListeners()
emitter.setMaxListeners(0) // unlimited
console.log(util.inspect(emitter.listeners('connection #1'))) // [ [Function] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment