Permalink
CodeDraken
Update emitter.js
11e7585
Mar 15, 2017
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up| // an event emitter | |
| function Emitter() { | |
| // observer list | |
| this.events = {}; | |
| // looks like: | |
| // { | |
| // greet: [function, function, function] | |
| // } | |
| } | |
| Emitter.prototype.on = function(type, listener) { | |
| this.events[type] = this.events[type] || []; | |
| this.events[type].push(listener); | |
| } | |
| Emitter.prototype.emit = function(type) { | |
| if(this.events[type]) { | |
| this.events[type].forEach(function(listener) { | |
| listener(); | |
| }); | |
| } | |
| } | |
| // app | |
| var emtr = new Emitter(); | |
| // observers | |
| emtr.on('greet', function() { | |
| console.log('Welcome!'); | |
| }); | |
| emtr.on('greet', function() { | |
| console.log('Greetings!'); | |
| }); | |
| emtr.emit('greet'); | |
| // Welcome! | |
| // Greetings! |