Created
August 4, 2014 22:02
Sharing instance of event emitter
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
var emitter = require("events").EventEmitter; | |
var eInstance = new emitter(); | |
function Component() { | |
// Component's Constructor Section (can do custom stuff) | |
} | |
// Component prototype inhertits emitter prototype instance. | |
Component.prototype = Object.create(eInstance); | |
Component.prototype.constructor = Component; | |
// Now both share the same instance of event emitter and events | |
// can be thrown between them | |
var a = new Component(); | |
var b = new Component(); | |
a.on('foo', function(source) { | |
console.log('foo', source); | |
}); | |
b.on('bar', function() { | |
console.log('bar'); | |
}) | |
a.emit('foo', 'from a'); | |
a.emit('bar'); | |
b.emit('foo', 'from b'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment