Created
April 24, 2014 17:33
-
-
Save attomos/11262853 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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