Skip to content

Instantly share code, notes, and snippets.

@cvan
Created October 3, 2014 07:38
Show Gist options
  • Select an option

  • Save cvan/b0b4fa1961a950e96e16 to your computer and use it in GitHub Desktop.

Select an option

Save cvan/b0b4fa1961a950e96e16 to your computer and use it in GitHub Desktop.
happy times with events using EventEmitter with browserify/node
var Emitter = require('events').EventEmitter;
function E(emitter, options) {
this.emitter = emitter;
this.options = options;
}
E.create = function (options) {
var emitter = new Emitter();
return new E(emitter, options);
};
E.prototype.on = function () {
this.emitter.on.apply(this.emitter, arguments);
return this;
};
E.prototype.removeListener = function () {
this.emitter.removeListener.apply(this.emitter, arguments);
return this;
};
var e = E.create();
e.on('yolo', function () {
console.log('yolo fired');
});
setTimeout(function () {
e.emitter.emit('swag');
}, 100);
setTimeout(function () {
e.emitter.emit('yolo');
}, 300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment