Navigation Menu

Skip to content

Commit

Permalink
[lib] merge-in eventemitter2
Browse files Browse the repository at this point in the history
  • Loading branch information
hij1nx committed Jun 11, 2011
1 parent 8177211 commit 8f5dea9
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/EventVat.js
Expand Up @@ -47,7 +47,14 @@
return this;
};

EventVat.prototype.addListener = function(event, listener) {
EventVat.prototype.addListener = function(event, key, listener) {

if(typeof key === 'function') {
listener = key;
}
else {
event = event + this._delimiter + key;
}

var name, ns = this._events;

Expand Down Expand Up @@ -84,15 +91,15 @@

EventVat.prototype.on = EventVat.prototype.addListener;

EventVat.prototype.once = function(event, listener) {
this.many(event, listener, 1);
EventVat.prototype.once = function(event, key, listener) {
this.many(event, key, listener, 1);
};

EventVat.prototype.many = function(event, listener, ttl) {
EventVat.prototype.many = function(event, key, listener, ttl) {

var self = this;

this.addListener(event, function() {
this.addListener(event, key, function() {
if(ttl-- == 0) {
self.removeListener(event, listener);
}
Expand Down Expand Up @@ -202,7 +209,18 @@
return true;
};

EventVat.prototype.removeListener = function(event) { this._events[event] = {} };
EventVat.prototype.removeListener = function(event, listener) {
if(listener && this._events[event]._listeners) {
for(var i = 0, l = this._events[event]._listeners.length; i < l; i++) {
if(listener === this._events[event]._listeners[i]) {
this._events[event]._listeners.slice(i, 1);
}
}
}
else {
this._events[event] = {};
}
};

EventVat.prototype.removeAllListeners = function(){ this._events = {}; };

Expand Down Expand Up @@ -454,7 +472,17 @@
};

EventVat.prototype.dump = function(stringify) {
var dump = stringify ? JSON.stringify(this.store) : this.store;

var dump = {};

for(var key in this.store) {
if(this.store.hasOwnProperty(key)) {
dump[key] = this.store[key].value;
}
}

dump = stringify ? JSON.stringify(dump) : dump;

this.emit('dump', null, dump);
return dump;
};
Expand Down

0 comments on commit 8f5dea9

Please sign in to comment.