Skip to content

Instantly share code, notes, and snippets.

@oranheim
Created June 26, 2014 11:29
Show Gist options
  • Save oranheim/e879eb581c9fd87b2b1a to your computer and use it in GitHub Desktop.
Save oranheim/e879eb581c9fd87b2b1a to your computer and use it in GitHub Desktop.
'use strict';
/**
* http://book.mixu.net/node/ch9.html
*
* Created by ora on 27.03.14.
*/
var SimpleEE = function () {
this.events = {};
};
SimpleEE.prototype.on = function (eventname, callback) {
this.events[eventname] || (this.events[eventname] = []);
this.events[eventname].push(callback);
};
SimpleEE.prototype.emit = function (eventname) {
var args = Array.prototype.slice.call(arguments, 1);
if (this.events[eventname]) {
this.events[eventname].forEach(function (callback) {
callback.apply(this, args);
});
}
};
describe('Test EventEmitter', function () {
describe('Call ee', function () {
it('should pass ee callback', function () {
// Example using the event emitter
var emitter = new SimpleEE();
emitter.on('greet', function (name) {
console.log('Hello, ' + name + '!');
});
emitter.on('greet', function (name) {
console.log('World, ' + name + '!');
});
['foo', 'bar', 'baz'].forEach(function (name) {
emitter.emit('greet', name);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment