Permalink
Browse files

[test] updates to tests, using less nodeunit features, stability

  • Loading branch information...
1 parent 389286f commit 3eaab4620da66a70e7e9fa4e4acc7014a0bd6a9e @0x00A 0x00A committed Nov 6, 2012
View
@@ -7,7 +7,15 @@ EventEmitter2 is a an implementation of the EventEmitter found in Node.js
- Namespaces/Wildcards.
- Times To Listen (TTL), extends the `once` concept with `many`.
- Browser environment compatibility.
- - ~2x faster than Node.js event emitter for non-namespaced events.
+ - Demonstrates good performance in benchmarks
+
+```
+EventEmitterHeatUp x 3,728,965 ops/sec \302\2610.68% (60 runs sampled)
+EventEmitter x 2,822,904 ops/sec \302\2610.74% (63 runs sampled)
+EventEmitter2 x 7,251,227 ops/sec \302\2610.55% (58 runs sampled)
+EventEmitter2 (wild) x 3,220,268 ops/sec \302\2610.44% (65 runs sampled)
+Fastest is EventEmitter2
+```
## Differences (Non breaking, compatible with existing EventEmitter)
@@ -18,6 +26,7 @@ EventEmitter2 is a an implementation of the EventEmitter found in Node.js
var server = new EventEmitter2({
wildcard: true, // should the event emitter use wildcards.
delimiter: '::', // the delimiter used to segment namespaces, defaults to `.`.
+ newListener: false, // if you want to emit the newListener event set to true.
maxListeners: 20, // the max number of listeners that can be assigned to an event, defaults to 10.
});
```
@@ -72,7 +81,7 @@ If either of the above described events were passed to the `on` method, subseque
as the following would be observed...
```javascript
- emitter.emit(['foo.bazz']);
+ emitter.emit('foo.bazz');
emitter.emit(['foo', 'bar']);
```
View
@@ -6,26 +6,31 @@
var defaultMaxListeners = 10;
function init() {
- this._events = new Object;
+ this._events = {};
if (this._conf) {
configure.call(this, this._conf);
}
}
function configure(conf) {
if (conf) {
+
this._conf = conf;
+
conf.delimiter && (this.delimiter = conf.delimiter);
conf.maxListeners && (this._events.maxListeners = conf.maxListeners);
conf.wildcard && (this.wildcard = conf.wildcard);
+ conf.newListener && (this.newListener = conf.newListener);
+
if (this.wildcard) {
- this.listenerTree = new Object;
+ this.listenerTree = {};
}
}
}
function EventEmitter(conf) {
- this._events = new Object;
+ this._events = {};
+ this.newListener = false;
configure.call(this, conf);
}
@@ -159,7 +164,7 @@
while (name) {
if (!tree[name]) {
- tree[name] = new Object;
+ tree[name] = {};
}
tree = tree[name];
@@ -247,10 +252,15 @@
};
EventEmitter.prototype.emit = function() {
+
this._events || init.call(this);
var type = arguments[0];
+ if (type === 'newListener' && !this.newListener) {
+ if (!this._events.newListener) { return false; }
+ }
+
// Loop through the *_all* functions and invoke them.
if (this._all) {
var l = arguments.length;
View
@@ -76,8 +76,7 @@ process.on('exit', function() {
global.DataView!==undefined?DataView:null,
global.Uint8ClampedArray!==undefined?Uint8ClampedArray:null,
AssertionError,
- global,
- events
+ global
];
if (global.errno) {
View
@@ -1,29 +1,19 @@
-var simpleEvents = require('nodeunit').testCase;
+var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
-module.exports = simpleEvents({
-
- setUp: function (callback) {
- if(typeof require !== 'undefined') {
- this.EventEmitter2 = require(file).EventEmitter2;
- }
- else {
- this.EventEmitter2 = window.EventEmitter2;
- }
-
- this.emitter = new this.EventEmitter2({ verbose: true });
- callback();
- },
+if(typeof require !== 'undefined') {
+ EventEmitter2 = require(file).EventEmitter2;
+}
+else {
+ EventEmitter2 = window.EventEmitter2;
+}
- tearDown: function (callback) {
- //clean up?
- callback();
- },
+module.exports = simpleEvents({
'1. Add a single listener on a single event.': function (test) {
-
- var emitter = this.emitter;
+
+ var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {
test.ok(true, 'The event was raised');
@@ -36,8 +26,8 @@ module.exports = simpleEvents({
},
'2. Add two listeners on a single event.': function (test) {
-
- var emitter = this.emitter;
+
+ var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {
test.ok(true, 'The event was raised');
@@ -54,8 +44,8 @@ module.exports = simpleEvents({
},
'3. Add three listeners on a single event.': function (test) {
-
- var emitter = this.emitter;
+
+ var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {
test.ok(true, 'The event was raised');
@@ -77,7 +67,7 @@ module.exports = simpleEvents({
},
'4. Add two listeners to two different events.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {
test.ok(true, 'The event was raised');
@@ -104,7 +94,7 @@ module.exports = simpleEvents({
},
'5. Never adding any listeners should yield a listeners array with the length of 0.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {
test.ok(true, 'The event was raised');
@@ -118,7 +108,8 @@ module.exports = simpleEvents({
'6. the listener added should be the right listener.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
+
var type = 'somelistenerbar';
var f = function () {};
@@ -133,7 +124,8 @@ module.exports = simpleEvents({
'7. should be able to listen on any event' : function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
+
var f = function () {
test.ok(true, 'the event was fired');
};
@@ -153,7 +145,8 @@ module.exports = simpleEvents({
'8. should be able to listen on any event (should cause an error)' : function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
+
var f = function () {
test.ok(true, 'the event was fired');
};
@@ -167,9 +160,9 @@ module.exports = simpleEvents({
},
'9. onAny alias' : function (test) {
-
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
+
var f = function () {
test.ok(true, 'the event was fired');
};
View
@@ -1,31 +1,20 @@
-var simpleEvents = require('nodeunit').testCase;
+var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
+var EventEmitter2;
-module.exports = simpleEvents({
-
- setUp: function (callback) {
- var EventEmitter2;
+if(typeof require !== 'undefined') {
+ EventEmitter2 = require(file).EventEmitter2;
+}
+else {
+ EventEmitter2 = window.EventEmitter2;
+}
- if(typeof require !== 'undefined') {
- EventEmitter2 = require(file).EventEmitter2;
- }
- else {
- EventEmitter2 = window.EventEmitter2;
- }
-
- this.emitter = new EventEmitter2({ verbose: true });
- callback();
- },
-
- tearDown: function (callback) {
- //clean up?
- callback();
- },
+module.exports = simpleEvents({
'1. Add two listeners on a single event and emit the event.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
function functionA() { test.ok(true, 'The event was raised'); }
function functionB() { test.ok(true, 'The event was raised'); }
@@ -41,7 +30,7 @@ module.exports = simpleEvents({
},
'2. Add two listeners on a single event and emit the event twice.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
function functionA() { test.ok(true, 'The event was raised'); }
function functionB() { test.ok(true, 'The event was raised'); }
@@ -58,7 +47,7 @@ module.exports = simpleEvents({
},
'3. Add two listeners on a single event and emit the event with a parameter.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
function functionA(value1) {
test.ok(true, 'The event was raised');
@@ -80,8 +69,8 @@ module.exports = simpleEvents({
},
'4. Add two listeners on an single event and emit the event twice with a parameter.': function (test) {
-
- var emitter = this.emitter;
+
+ var emitter = new EventEmitter2({ verbose: true });
function functionA(value1) {
test.ok(true, 'The event was raised');
@@ -104,8 +93,8 @@ module.exports = simpleEvents({
},
'5. Add two listeners on an single event and emit the event twice with multiple parameters.': function (test) {
-
- var emitter = this.emitter;
+
+ var emitter = new EventEmitter2({ verbose: true });
function functionA(value1, value2, value3) {
test.ok(true, 'The event was raised');
@@ -133,7 +122,7 @@ module.exports = simpleEvents({
},
'6. Check return values of emit.': function (test) {
- var emitter = this.emitter;
+ var emitter = new EventEmitter2({ verbose: true });
function functionA() { test.ok(true, 'The event was raised'); }
View
@@ -1,28 +1,18 @@
var simpleEvents = require('nodeunit').testCase;
-
var file = '../../lib/eventemitter2';
-
var EventEmitter2;
-module.exports = simpleEvents({
-
- setUp: function (callback) {
- if(typeof require !== 'undefined') {
- EventEmitter2 = require(file).EventEmitter2;
- }
- else {
- EventEmitter2 = window.EventEmitter2;
- }
+if(typeof require !== 'undefined') {
+ EventEmitter2 = require(file).EventEmitter2;
+}
+else {
+ EventEmitter2 = window.EventEmitter2;
+}
- callback();
- },
-
- tearDown: function (callback) {
- //clean up?
- callback();
- },
+module.exports = simpleEvents({
'reconfigure1. initialize, removeAllListeners' : function (test) {
+
var emitter,
config = {
wildcard: true, // should the event emitter use wildcards.
Oops, something went wrong.

0 comments on commit 3eaab46

Please sign in to comment.