Skip to content

Commit 0b6ee5d

Browse files
committed
introducing EventEmitter.on
1 parent 06d07c9 commit 0b6ee5d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/eventemitter.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
var EventEmitter = function(events) {
1010
var that = this;
1111
that._events = events || [];
12+
that._listeners = {};
1213
};
1314
EventEmitter.prototype.emit = function(type) {
1415
var that = this;
@@ -19,7 +20,24 @@ EventEmitter.prototype.emit = function(type) {
1920
if (that['on'+type]) {
2021
that['on'+type].apply(that, args);
2122
}
23+
if (type in that._listeners) {
24+
for(var i = 0; i < that._listeners[type].length; i++) {
25+
that._listeners[type][i].apply(that, args);
26+
}
27+
}
2228
};
29+
30+
EventEmitter.prototype.on = function(type, callback) {
31+
var that = this;
32+
that._verifyType(type);
33+
if (that._nuked) return;
34+
35+
if (!(type in that._listeners)) {
36+
that._listeners[type] = [];
37+
}
38+
that._listeners[type].push(callback);
39+
};
40+
2341
EventEmitter.prototype._verifyType = function(type) {
2442
var that = this;
2543
if (utils.arrIndexOf(that._events, type) === -1) {
@@ -35,4 +53,5 @@ EventEmitter.prototype.nuke = function() {
3553
for(var i=0; i<that._events.length; i++) {
3654
delete that[that._events[i]];
3755
}
56+
that._listeners = {};
3857
};

0 commit comments

Comments
 (0)