Created
October 3, 2014 07:38
-
-
Save cvan/b0b4fa1961a950e96e16 to your computer and use it in GitHub Desktop.
happy times with events using EventEmitter with browserify/node
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 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