Skip to content

Instantly share code, notes, and snippets.

@hsubra89
Created August 4, 2014 22:02
Show Gist options
  • Save hsubra89/cbdd7fb7e8fb763f54de to your computer and use it in GitHub Desktop.
Save hsubra89/cbdd7fb7e8fb763f54de to your computer and use it in GitHub Desktop.
Sharing instance of event emitter
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