Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

introducing EventEmitter.on

  • Loading branch information...
commit 0b6ee5dd245e53d7f372610b6094b12bdedecbbd 1 parent 06d07c9
@majek majek authored
Showing with 19 additions and 0 deletions.
  1. +19 −0 lib/eventemitter.js
View
19 lib/eventemitter.js
@@ -9,6 +9,7 @@
var EventEmitter = function(events) {
var that = this;
that._events = events || [];
+ that._listeners = {};
};
EventEmitter.prototype.emit = function(type) {
var that = this;
@@ -19,7 +20,24 @@ EventEmitter.prototype.emit = function(type) {
if (that['on'+type]) {
that['on'+type].apply(that, args);
}
+ if (type in that._listeners) {
+ for(var i = 0; i < that._listeners[type].length; i++) {
+ that._listeners[type][i].apply(that, args);
+ }
+ }
};
+
+EventEmitter.prototype.on = function(type, callback) {
+ var that = this;
+ that._verifyType(type);
+ if (that._nuked) return;
+
+ if (!(type in that._listeners)) {
+ that._listeners[type] = [];
+ }
+ that._listeners[type].push(callback);
+};
+
EventEmitter.prototype._verifyType = function(type) {
var that = this;
if (utils.arrIndexOf(that._events, type) === -1) {
@@ -35,4 +53,5 @@ EventEmitter.prototype.nuke = function() {
for(var i=0; i<that._events.length; i++) {
delete that[that._events[i]];
}
+ that._listeners = {};
};
Please sign in to comment.
Something went wrong with that request. Please try again.