Skip to content

Instantly share code, notes, and snippets.

@omarkj
Created October 31, 2010 17:44
Show Gist options
  • Save omarkj/656894 to your computer and use it in GitHub Desktop.
Save omarkj/656894 to your computer and use it in GitHub Desktop.
Simple EventBus in JS
"use strict";
var EventBus = function() {
var events = [];
var p_on = function(eventName, callback) {
if (!events.some(function(e) {
return e.name == eventName; })) {
var new_event = {
name: eventName,
cb: callback
};
events.push(new_event);
return [eventName, callback];
}
};
var p_emit = function(eventName, args) {
var match = events.filter(function(e) {
return e.name == eventName;
});
match.forEach(function(m) {
m.cb(args);
});
};
return {
on: p_on,
emit: p_emit
};
};
@omarkj
Copy link
Author

omarkj commented Oct 31, 2010

Can't remove events atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment