Skip to content

Commit

Permalink
[lib] addresses issue with libral wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
hij1nx committed Jul 27, 2011
1 parent 8f00c1e commit 85bb262
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 47 deletions.
54 changes: 31 additions & 23 deletions lib/eventemitter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,54 @@
EventEmitter2.prototype.event = '';

var searchListenerTree = function(handlers, type, tree, i) {
if (!tree) {
return
}

var listeners;

if(i === type.length && tree && tree._listeners) {

if(typeof tree._listeners === 'function') {

if (i === type.length && tree._listeners) {
//
// If at the end of the event(s) list and the tree has listeners
// invoke those listeners.
//
if (typeof tree._listeners === 'function') {
handlers && handlers.push(tree._listeners);
return tree;
}
else {
for(var leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
} else {
for (var leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
handlers && handlers.push(tree._listeners[leaf]);
}
return tree;
}
}

if(tree && (type[i] === '*' || tree[type[i]] || tree['*'])) {
if(type[i] === '*' || tree['*']) {
for(var branch in tree) {
if(tree.hasOwnProperty(branch)) {
if (type[i] === '*' || tree[type[i]]) {
//
// If the event emitted is '*' at this part
// or there is a concrete match at this patch
//
if (type[i] === '*') {
for (var branch in tree) {
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
listeners = searchListenerTree(handlers, type, tree[branch], i+1);
}
}
return;
}

tree = tree[type[i]];
listeners = searchListenerTree(handlers, type, tree[type[i]], i+1);
}


if(tree['*']) {
i++;
for(var branch in tree) {
if(tree.hasOwnProperty(branch)) {
listeners = searchListenerTree(handlers, type, tree[branch], i+1);
}
}
}
else {
listeners = searchListenerTree(handlers, type, tree, i+1);
}
if (tree['*']) {
//
// If the listener tree will allow any match for this part,
// then recursively explore all branches of the tree
//
searchListenerTree(handlers, type, tree['*'], i+1);
}

return listeners;
};

Expand Down
69 changes: 45 additions & 24 deletions test/wildcardEvents/addListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = simpleEvents({

this.emitter = new EventEmitter2({
wildcard: true
//,delimiter: ':'
});
callback();
},
Expand All @@ -30,13 +29,13 @@ module.exports = simpleEvents({

var emitter = this.emitter;
var type = 'some.listener.bar';

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listeners(type).length, 1, 'There are three emitters');

test.expect(1);
test.done();

Expand All @@ -46,17 +45,17 @@ module.exports = simpleEvents({

var emitter = this.emitter;
var type = 'some.listener.bar';

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listeners(type).length, 2, 'There are three emitters');

test.expect(1);
test.done();

Expand All @@ -65,11 +64,11 @@ module.exports = simpleEvents({

var emitter = this.emitter;
var type = 'some.listener.bar';

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
Expand All @@ -79,7 +78,7 @@ module.exports = simpleEvents({
});

test.equal(emitter.listeners(type).length, 3, 'There are three emitters');

test.expect(1);
test.done();

Expand All @@ -88,40 +87,40 @@ module.exports = simpleEvents({

var emitter = this.emitter;
var type = 'some.listener.bar';

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

emitter.on('test2', function () {
test.ok(true, 'The event was raised');
});

emitter.on('test2', function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listeners(type).length, 2, 'There are two emitters');
test.equal(emitter.listeners('test2').length, 2, 'There are two emitters');

test.expect(2);
test.done();
},

'5. Never adding any listeners should yield a listeners array with the length of 0.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';

emitter.on(type, function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listeners('test2').length, 0, 'There are no emitters');

test.expect(1);
test.done();
},
Expand All @@ -130,26 +129,48 @@ module.exports = simpleEvents({
var emitter = this.emitter;
var type = 'some.listener.bar';
var f = function () {};

emitter.on(type, f);
test.equal(emitter.listeners(type).length, 1, 'There are is one emitters');
test.equal(emitter.listeners(type)[0], f, 'The function should be f');

test.expect(2);
test.done();

},

'7. A listener should respond to two wildcards seperated by a delimiter.': function (test) {
'7. Listeners on *, *.*, *.test with emissions from foo.test and other.emit': function (test) {
var emitter = this.emitter;
var f = function () {
test.ok(true, 'the event was fired')
};

emitter.on('*.test', f);
emitter.on('*.*', f);
emitter.emit('foo.foo');
emitter.on('*', f);

test.expect(1);
emitter.emit('other.emit');
emitter.emit('foo.test');

test.expect(3);
test.done();
},

'8. Listeners on *, *.*, foo.test with emissions from *, *.* and foo.test': function (test) {
var emitter = this.emitter;
var f = function () {
test.ok(true, 'the event was fired')
};

emitter.on('foo.test', f);
emitter.on('*.*', f);
emitter.on('*', f);

emitter.emit('*.*');
emitter.emit('foo.test');
emitter.emit('*')

test.expect(5);
test.done();
}

Expand Down
Loading

0 comments on commit 85bb262

Please sign in to comment.