Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eventemitter2

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventemitter2 - npm Package Compare versions

Comparing version 0.4.10 to 0.4.11

test/simple/reconfigure.js

22

lib/eventemitter2.js

@@ -9,3 +9,6 @@ ;!function(exports, undefined) {

function init() {
this._events = new Object;
this._events = {};
if (this._conf) {
configure.call(this, this._conf);
}
}

@@ -15,7 +18,12 @@

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 = {};
}

@@ -26,3 +34,4 @@ }

function EventEmitter(conf) {
this._events = new Object;
this._events = {};
this.newListener = false;
configure.call(this, conf);

@@ -161,3 +170,3 @@ }

if (!tree[name]) {
tree[name] = new Object;
tree[name] = {};
}

@@ -217,2 +226,4 @@

this._events.maxListeners = n;
if (!this._conf) this._conf = {};
this._conf.maxListeners = n;
};

@@ -249,2 +260,3 @@

EventEmitter.prototype.emit = function() {
this._events || init.call(this);

@@ -254,3 +266,3 @@

if (type === 'newListener') {
if (type === 'newListener' && !this.newListener) {
if (!this._events.newListener) { return false; }

@@ -257,0 +269,0 @@ }

{
"name": "eventemitter2",
"version": "0.4.10",
"version": "0.4.11",
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.",

@@ -5,0 +5,0 @@ "keywords": ["event", "events", "emitter", "eventemitter"],

@@ -10,4 +10,12 @@ # EventEmitter2

- 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)

@@ -22,2 +30,3 @@

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.

@@ -77,3 +86,3 @@ });

```javascript
emitter.emit(['foo.bazz']);
emitter.emit('foo.bazz');
emitter.emit(['foo', 'bar']);

@@ -80,0 +89,0 @@ ```

@@ -79,4 +79,3 @@ // Copyright Joyent, Inc. and other Node contributors.

AssertionError,
global,
events
global
];

@@ -83,0 +82,0 @@

@@ -0,28 +1,18 @@

var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
if(typeof require !== 'undefined') {
this.EventEmitter2 = require(file).EventEmitter2;
}
else {
this.EventEmitter2 = window.EventEmitter2;
}
'1. Add a single listener on a single event.': function (test) {
this.emitter = new this.EventEmitter2({ verbose: true });
callback();
},
var emitter = new EventEmitter2({ verbose: true });
tearDown: function (callback) {
//clean up?
callback();
},
'1. Add a single listener on a single event.': function (test) {
var emitter = this.emitter;
emitter.on('test1', function () {

@@ -39,5 +29,5 @@ test.ok(true, 'The event was raised');

'2. Add two listeners on a single event.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {

@@ -58,5 +48,5 @@ test.ok(true, 'The event was raised');

'3. Add three listeners on a single event.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });
emitter.on('test1', function () {

@@ -82,3 +72,3 @@ test.ok(true, 'The event was raised');

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -110,3 +100,3 @@ emitter.on('test1', function () {

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -125,3 +115,4 @@ emitter.on('test1', function () {

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });
var type = 'somelistenerbar';

@@ -141,3 +132,4 @@ var f = function () {};

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });
var f = function () {

@@ -162,3 +154,4 @@ test.ok(true, 'the event was fired');

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });
var f = function () {

@@ -177,5 +170,5 @@ test.ok(true, 'the event was fired');

'9. onAny alias' : function (test) {
var emitter = new EventEmitter2({ verbose: true });
var emitter = this.emitter;
var f = function () {

@@ -182,0 +175,0 @@ test.ok(true, 'the event was fired');

@@ -0,29 +1,18 @@

var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
var 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();
},
'1. Add two listeners on a single event and emit the event.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -44,3 +33,3 @@ function functionA() { test.ok(true, 'The event was raised'); }

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -62,3 +51,3 @@ function functionA() { test.ok(true, 'The event was raised'); }

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -85,5 +74,5 @@ function functionA(value1) {

'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) {

@@ -110,5 +99,5 @@ test.ok(true, 'The event was raised');

'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) {

@@ -140,3 +129,3 @@ test.ok(true, 'The event was raised');

var emitter = this.emitter;
var emitter = new EventEmitter2({ verbose: true });

@@ -143,0 +132,0 @@ function functionA() { test.ok(true, 'The event was raised'); }

@@ -0,29 +1,20 @@

var simpleEvents= require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
var EventEmitter2;
'removeListener1. adding 1, removing 1' : function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
var emitter = new EventEmitter2;
this.emitter = new EventEmitter2;
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'removeListener1. adding 1, removing 1' : function (test) {
var emitter = this.emitter,
type = 'remove',
var type = 'remove',
listeners;

@@ -49,4 +40,6 @@

'removeListener2. adding 2, removing 1' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -73,4 +66,6 @@

'removeListener3. adding 3, removing 1' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -98,4 +93,5 @@

'removeListener4. should error if we don\'t pass in a function' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -121,4 +117,5 @@

'removeListener5. removing a different function, should not remove' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -147,4 +144,5 @@

'removeListener6. removing all functions' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -173,4 +171,5 @@

'removeListener7. removing different event, should not remove' : function (test) {
var emitter = this.emitter,
type = 'remove',
var emitter = new EventEmitter2;
var type = 'remove',
listeners;

@@ -202,5 +201,3 @@

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

@@ -0,28 +1,19 @@

var simpleEvents= require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
'setMaxListener1. default behavior of 10 listeners.' : function (test) {
if(typeof require !== 'undefined') {
this.EventEmitter2 = require(file).EventEmitter2;
}
else {
this.EventEmitter2 = window.EventEmitter2;
}
var emitter = new EventEmitter2;
this.emitter = new this.EventEmitter2;
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'setMaxListener1. default behavior of 10 listeners.' : function (test) {
var emitter = this.emitter;
for (var i = 0; i < 10; i++) {

@@ -42,4 +33,5 @@ emitter.on('foobar', function () {

'setMaxListener2. If we added more than 10, should not see them' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2;
for (var i = 0; i < 10 ; i++) {

@@ -54,3 +46,2 @@ emitter.on('foobar2', function () {

});
console.log('move on');

@@ -66,3 +57,4 @@ var listeners = emitter.listeners('foobar2');

'setMaxListener3. if we set maxListener to be greater before adding' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2;
var type = 'foobar3';

@@ -79,4 +71,2 @@

// require('eyes').inspect(emitter._events);
var listeners = emitter.listeners(type);

@@ -91,3 +81,4 @@ test.equal(listeners.length, 15, 'should have 15');

'setMaxListener4. should be able to change it right at 10' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2;
var type = 'foobar4';

@@ -106,4 +97,2 @@

// require('eyes').inspect(emitter._events);
var listeners = emitter.listeners(type);

@@ -118,3 +107,4 @@ test.equal(listeners.length, 11, 'should have 11');

'setMaxListener5. if we set maxListener to be 0 should add endlessly' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2;
var type = 'foobar';

@@ -139,5 +129,7 @@

'maxListeners parameter. Passing maxListeners as a parameter should override default.' : function (test) {
var emitter = new this.EventEmitter2({
var emitter = new EventEmitter2({
maxListeners: 2
});
console.log(emitter, test.equal, test.ok);

@@ -144,0 +136,0 @@ emitter.on('a', function () {});

@@ -0,30 +1,19 @@

var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
this.emitter = new EventEmitter2();
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. A listener added with `once` should only listen once and then be removed.': function (test) {
var emitter = new EventEmitter2();
var emitter = this.emitter;
emitter.once('test1', function () {

@@ -43,3 +32,3 @@ test.ok(true, 'The event was raised once');

var emitter = this.emitter;
var emitter = new EventEmitter2();

@@ -62,3 +51,3 @@ emitter.many('test1', 4, function (value1) {

var emitter = this.emitter;
var emitter = new EventEmitter2();

@@ -82,3 +71,3 @@ emitter.many('test1', 4, function (value1, value2, value3) {

var emitter = this.emitter;
var emitter = new EventEmitter2();
var count = 0;

@@ -116,4 +105,4 @@

'5. `removeListener` and `once`': function(test) {
var emitter = this.emitter;
var emitter = new EventEmitter2();
var functionA = function() { test.ok(true, 'Event was fired'); };

@@ -120,0 +109,0 @@

@@ -5,28 +5,19 @@

module.exports = simpleEvents({
var EventEmitter2;
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;
}
module.exports = simpleEvents({
this.emitter = new EventEmitter2({
'1. Add a single listener on a single event.': function (test) {
var emitter = new EventEmitter2({
wildcard: true
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. Add a single listener on a single event.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';

@@ -46,4 +37,7 @@

'1a. Add a single listener on a single event (using an array).': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = ['some', 'listener', 'bar'];

@@ -64,3 +58,6 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = 'some.listener.bar';

@@ -85,3 +82,6 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = ['some', 'listener', 'bar'];

@@ -105,4 +105,7 @@

'3. Add three listeners on a single event.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = 'some.listener.bar';

@@ -131,3 +134,6 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = 'some.listener.bar';

@@ -159,3 +165,7 @@

'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({
wildcard: true
});
var type = 'some.listener.bar';

@@ -174,3 +184,7 @@

'6. the listener added should be the right listener.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var type = 'some.listener.bar';

@@ -189,3 +203,7 @@ var f = function () {};

'7. Listeners on `*`, `*.*`, `*.test` with emissions from `foo.test` and `other.emit`': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var f = function () {

@@ -207,3 +225,7 @@ test.ok(true, 'the event was fired')

'8. Listeners on `*`, `*.*`, foo.test with emissions from `*`, `*.*` and `foo.test`': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var f = function () {

@@ -226,4 +248,7 @@ test.ok(true, 'the event was fired')

'9. Listeners on `*`. (using an array)': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var f = function () {

@@ -242,3 +267,5 @@ test.ok(true, 'the event was fired')

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});

@@ -251,7 +278,5 @@ emitter.on('foo', function() {

console.log(this.event);
});
});
emitter.emit('foo');
emitter.emit('foo');

@@ -262,3 +287,7 @@ test.done();

'11. Listeners with multi-level wildcards': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
var i = 0;

@@ -303,5 +332,7 @@ var f = function (n) {

'12. Check return values of emit for wildcard emitter.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true
});
emitter.on('foo.*', function () {

@@ -308,0 +339,0 @@ test.ok(true, 'The event was raised');

var basicEvents = require('nodeunit').testCase;
/////helper///////
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require('../../lib/eventemitter2').EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
function setHelper (emitter, test, testName){

@@ -24,13 +32,5 @@ var eventNames = [

setUp: function (callback) {
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require('../../lib/eventemitter2').EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
this.emitter = new EventEmitter2({
'1. An event can be namespaced.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,

@@ -40,14 +40,2 @@ verbose: true

callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. An event can be namespaced.': function (test) {
var emitter = this.emitter;
emitter.on('test1.ns1', function () {

@@ -64,5 +52,8 @@ test.ok(true, 'The event was raised');

'2. An event can be namespaced and accept values.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitter = this.emitter;
emitter.on('test2.ns1', function(value1) {

@@ -81,5 +72,8 @@ test.ok(true, 'The event was raised');

var emitter = this.emitter;
emitter.on('test3.ns1', function (value1, value2, value3) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
emitter.on('test3.ns1', function (value1, value2, value3) {
test.ok(true, 'The event was raised');

@@ -90,14 +84,17 @@ test.ok(arguments.length === 3, 'The event was raised with the correct number of arguments');

test.ok(value3 === 3 || value3 === 6, 'The event was raised with the value `' + value3 + '`.');
});
});
emitter.emit('test3.ns1', 1, 2, 3);
emitter.emit('test3.ns1', 4, 5, 6);
emitter.emit('test3.ns1', 1, 2, 3);
emitter.emit('test3.ns1', 4, 5, 6);
test.expect(10);
test.expect(10);
test.done();
},
'4. A listener should support wild cards.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitter = this.emitter;
emitter.on('test4.*', function () {

@@ -114,12 +111,15 @@ test.ok(true, 'The event was raised');

'5. Emitting an event should support wildcards.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitter = this.emitter;
emitter.on('test5A.test5B', function () {
test.ok(true, 'The event was raised');
});
emitter.emit('test5A.*');
test.expect(1);
emitter.on('test5A.test5B', function () {
test.ok(true, 'The event was raised');
});
emitter.emit('test5A.*');
test.expect(1);
test.done();

@@ -130,4 +130,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
emitter.on('test10.*.foo', function () {

@@ -144,5 +147,8 @@ test.ok(true, 'The event was raised');

'7. Emitting an event should support complex wildcards.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitter = this.emitter;
emitter.on('test11.ns1.foo', function () {

@@ -160,4 +166,7 @@ test.ok(true, 'The event was raised');

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
emitter.on('test12.ns1.ns2', function (value1, value2, value3) {

@@ -180,3 +189,6 @@ test.ok(true, 'The event was raised');

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});

@@ -199,5 +211,8 @@ emitter.on('test13', function (event) {

'10. should be able to listen on any event' : function (test) {
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitter = this.emitter;
var fn = function (foo, bar) {

@@ -216,604 +231,10 @@ test.equal(this.event, 'test23.ns5.ns5')

},
/*,
'11. A listener should support total wild card.': function (test) {
var emitter = this.emitter;
emitter.on('*', function () {
test.ok(true, 'The event was raised');
});
emitter.emit('test14');
emitter.emit('test14.ns1');
emitter.emit('test14.ns1.ns2');
test.expect(1);
test.done();
},
'12. A listener should support complex total wild card.': function (test) {
var emitter = this.emitter;
emitter.on('*', function () {
test.ok(true, 'The event was raised');
});
emitter.emit('test15.*');
emitter.emit('test15.*.ns2')
emitter.emit('*');
test.expect(1);
test.done();
},
'13. Should be able to fire with wildcard start.' : function (test) {
var emitter = this.emitter;
emitter.on('test16', function () {
test.ok(true, 'The event test15 was raised');
});
emitter.on('test16.ns1', function () {
test.ok(true, 'The event test15.ns1 was raised');
});
emitter.emit('*');
emitter.emit('*.ns1');
test.expect(2);
test.done();
},
'14. Should fail if delimiter is used to start or end event name.' : function (test) {
var emitter = this.emitter;
//nothing should emit, so here is a all-listener
emitter.on('*.*.*', function () {
test.ok(false, 'an event was raised!');
});
emitter.on('*.*', function () {
test.ok(false, 'an event was raised!');
});
emitter.on('*', function () {
test.ok(false, 'an event was raised!');
});
try {
emitter.on('.ns4', function () {
test.ok(false, 'The event .ns4 was raised');
});
emitter.emit('.ns4');
}
catch(ex) {
test.ok(true, 'The event .ns4 was not raised');
}
try {
emitter.on('ns4.', function () {
test.ok(false, 'The event .ns4 was raised');
});
emitter.emit('ns4.');
}
catch(ex) {
test.ok(true, 'The event .ns4 was not raised');
}
try {
emitter.on('.ns4', function () {
test.ok(false, 'The event .ns4 was raised');
});
emitter.emit('ns4.');
}
catch(ex) {
test.ok(true, 'The event .ns4 was not raised');
}
try {
emitter.on('.ns4', function () {
test.ok(false, 'The event .ns4 was raised');
});
}
catch(ex) {
test.ok(true, 'The event .ns4 was not raised');
}
try {
emitter.emit('ns4.');
}
catch(ex) {
test.ok(true, 'The event .ns4 was not raised');
}
try {
emitter.emit('some..bad');
}
catch (ex) {
test.ok(true, 'error was raised');
}
try {
emitter.on('some..bad', function () {
test.ok(false, 'a bad event was raised');
});
}
catch (ex){
test.ok(true,'error was raised');
}
test.expect(7);
test.done();
},
'15. Should provide case sensitive option.' : function (test) {
var emitter = this.emitter;
emitter.on('test18', function () {
test.ok(false, 'The event test18 was raised');
});
emitter.on('test18.ns1', function () {
test.ok(false, 'The event test18.ns1 was raised');
});
emitter.emit('Test18');
test.expect(0);
test.done();
},
'16. one emit should be able to fire on multiple namespaces.' : function (test) {
var emitter = this.emitter;
emitter.on('test19.*', function () {
test.ok(true, 'test19.* was raised');
});
emitter.on('test19.foo', function () {
test.ok(true, 'test19.foo was raised');
});
emitter.emit('test19.foo');
test.expect(2);
test.done();
},
'17. should support case insensitivty (complex).' : function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require('../lib/ee2').EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
var emitter = new EventEmitter2({ caseSensitive : false});
emitter.on('test20', function () {
test.ok(true, 'The event test20 was raised');
});
emitter.on('test20.ns1', function () {
test.ok(true, 'The event test20.ns1 was raised');
});
emitter.on('*.ns1', function () {
test.ok(true, 'The event *.ns1 was raised');
});
emitter.on('*.ns2', function () {
test.ok(false, 'The event *.ns2 was raised');
});
emitter.emit('Test20');
emitter.emit('TeSt20.nS1');
test.expect(3);
test.done();
},
'18. should be able to removeListeners' : function (test) {
var emitter = this.emitter;
var someFun = function () {
test.ok(true, 'someFunc was raised');
}
emitter.on('test21', someFun);
emitter.on('test21.*', someFun);
emitter.on('test21.ns1', someFun);
emitter.on('test21.ns1.ns2', someFun);
emitter.emit('test21'); //1
emitter.emit('test21.ns2'); //1
emitter.emit('test21.ns1'); //2
var listeners = emitter.listeners('test21');
test.ok(listeners.length === 1, 'there should be 1 listener');
emitter.removeListener('test21', someFun);
listeners = emitter.listeners('test21');
test.ok(listeners.length === 0, 'there should be 0 listener (empty array)');
// should be able to add more listeners after removing
emitter.on('test21', someFun);
emitter.on('test21', someFun);
listeners = emitter.listeners('test21');
test.ok(listeners.length === 2, 'there should be 2 listeners'); //1
emitter.emit('test21'); //2
emitter.removeListener('test21', someFun); //this removes all listeners
listeners = emitter.listeners('test21');
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
emitter.removeListener('test21', someFun); //this removes all listeners
listeners = emitter.listeners('test21');
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
emitter.emit('test21'); //0
listeners = emitter.listeners('test21.ns1');
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
emitter.removeListener('test21.ns1', someFun); // remove one
listeners = emitter.listeners('test21.ns1');
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
listeners = emitter.listeners('test21.*');
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
emitter.removeListener('test21.*', someFun); // remove one
listeners = emitter.listeners('test21.*');
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
test.expect(15);
test.done();
},
'19. should be able to remove all listeners' : function (test) {
var emitter = this.emitter,
addedEvents = setHelper(emitter, test, 'test22');
emitter.emit('test22'); //1
var listeners = emitter.listeners('test22');
test.ok(listeners.length === 1, 'there should be 1 listener'); //1
emitter.removeAllListeners('test22');
listeners = emitter.listeners('test22');
test.ok(listeners.length === 0, 'there should be 0 listener'); //1
emitter.removeAllListeners('test22.ns1');
listeners = emitter.listeners('test22.ns1');
test.ok(listeners.length === 0, 'there should be 0 listener'); //1
emitter.removeAllListeners(); //removing all possible
for (var i = 0; i < addedEvents.length; i++) {
listeners = emitter.listeners(addedEvents[i]);
test.ok(listeners.length === 0, 'there shouldn\'t be at a listener');
}
test.expect(addedEvents.length + 4 );
test.done();
},
'19. should be able to fire once and done' : function (test) {
var emitter = this.emitter,
addedEvents = setHelper(emitter,test,'test24');
emitter.once('test24once', function () {
test.ok(true, 'fired once');
});
emitter.emit('test24');
emitter.emit('test24once');
emitter.emit('test24once');
test.expect(2);
test.done();
},
'20. should be able to fire many and done' : function (test) {
var emitter = this.emitter,
addedEvents = setHelper(emitter,test,'test25');
emitter.many('test25many', 5, function () {
test.ok(true, 'test25many pewpew');
});
emitter.emit('test25'); //1
for (var i= 0; i < 5; i++) {
emitter.emit('test25many'); //1
}
emitter.emit('test25many'); //0
test.expect(6);
test.done();
},
'21. should be able to list all onAny listeners' : function (test) {
var emitter = this.emitter,
addedEvents = setHelper(emitter, test, 'test26'),
fn = function (tag) {
if (tag !== 'addListener') {
test.equals(tag, 'test26.ns5.ns5', 'emitted tag, and raised tag should match');
test.ok(true, 'something happened somewhere');
}
};
emitter.onAny(fn);
emitter.emit('test26.ns5.ns5'); //2
var listeners = emitter.listenersAny();
test.equals(listeners.length, 1, 'should be one any listeners');
emitter.offAny(fn);
listeners = emitter.listenersAny();
test.ok(listeners.length === 0, 'should be no any listeners');
emitter.onAny(fn);
emitter.onAny(fn);
listeners = emitter.listenersAny();
test.equals(listeners.length, 2, 'should be two any listeners');
emitter.offAny();
listeners = emitter.listenersAny();
test.ok(listeners.length === 0, 'should be no any listeners');
test.expect(6);
test.done();
},
'22. should not expand beyond the namespace' : function (test) {
var emitter = this.emitter,
addedEvents = setHelper(emitter,test,'test27');
emitter.emit('test27.ns2.ns3'); //1
emitter.emit('test27.ns2.ns3.ns4'); //0
test.expect(1);
test.done();
},
'23. should raise errors, if error is emitted and not caught' : function (test) {
var emitter = this.emitter,
error = new Error('Something Funny Happened');
try {
emitter.emit('error');
}
catch (ex) {
test.ok(ex instanceof Error, 'should be an Error');
}
try {
emitter.emit('error', error);
}
catch (ex) {
test.equal(error, ex, 'should have passed up the argument');
}
emitter.on('error', function (event, err) {
test.ok(true, 'error event was raised');
test.equal(err, error, 'of the error');
});
emitter.emit('error',error);
test.expect(4);
test.done();
},
'11. No warning should be raised if we set maxListener to be greater before adding' : function (test) {
'24. should raise errors on namespaces, if error is emitted and not caught' : function (test) {
var emitter = this.emitter,
error = new Error('Something Funny Happened');
emitter.on('foo.bar', function(){});
try {
emitter.emit('foo.error');
}
catch (ex) {
test.ok(ex instanceof Error, 'should be an Error');
}
try {
emitter.emit('foo.error', error);
}
catch (ex) {
test.equal(error, ex, 'should have passed up the argument');
}
emitter.on('error', function (event, err) {
test.ok(true, 'error event was raised');
test.equal(err, error, 'of the error');
});
emitter.emit('error',error);
test.expect(4);
test.done();
},
'25. should support old config for EE2' : function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require('../lib/ee2').EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
var emitter = new EventEmitter2({
caseSensitive : true,
delimiter : '?'
wildcard: true,
verbose: true
});
emitter.on('test30?a?b', function () {
test.ok(true, 'test30?a?b did emit');
});
emitter.emit('test30?a?b');
test.expect(1);
test.done();
},
'26. should reject bad wildcard inputs' : function (test) {
var emitter = this.emitter;
addedEvents = setHelper(emitter,test,'test31');
emitter.onAny(function () {
test.ok(false, 'no event should be emitted, ever');
});
// try listening on a bad
try {
emitter.on('test31*', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('test*31', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('test31.*a', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31.a*', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31.a*a', function () {
test.ok(false, 'should never registered');
});
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
//now try emittering with a bad wildcard
try {
emitter.emit('test31*')
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31');
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('test*31');
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('test31.*a');
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31.a*');
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
// bad wildcard at the front
try {
emitter.on('*test31.a*a');
}
catch (ex) {
test.ok(ex instanceof Error, 'expected an error');
}
test.expect(12);
test.done();
},
'27. Should be able to start with 0 max listeners' : function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require('../lib/ee2').EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
try {
var emitter = new EventEmitter2({
maxListeners : 0
});
emitter.on('no listeners', function () {
test.ok(false, 'no listener was raised');
});
test.ok(true, 'was able to make something');
}
catch (ex) {
test.ok(false, 'Error was raised');
}
test.expect(1);
test.done();
},
'28. should raise maxListeners when too many are registerd' : function (test) {
var emitter = this.emitter;
emitter.on('maxListeners', function () {
test.ok(true, 'maxListeners fired');
});
for (var i = 0; i < 11 ; i++){
emitter.on('test33', function () {
test.ok(false, 'event was raised');
});
}
var listeners = emitter.listeners('test33');
test.equal(listeners.length, 10, '10 listeners in total');
test.expect(2);
test.done();
} */
'29. No warning should be raised if we set maxListener to be greater before adding' : function (test) {
var emitter = this.emitter;
var type = 'test29.*';

@@ -830,4 +251,2 @@

// require('eyes').inspect(emitter._events);
var listeners = emitter.listeners(type);

@@ -834,0 +253,0 @@ test.equal(listeners.length, 15, 'should have 15');

@@ -0,32 +1,23 @@

var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
module.exports = simpleEvents({
var EventEmitter2;
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;
}
module.exports = simpleEvents({
this.emitter = new EventEmitter2({
'1. Add a single listener on a single event.': function (test) {
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. Add a single listener on a single event.': function (test) {
var emitter = this.emitter;
var type = 'some::listener::bar';

@@ -47,3 +38,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var type = 'some::listener::bar';

@@ -67,3 +62,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var type = 'some::listener::bar';

@@ -91,3 +90,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var type = 'some::listener::bar';

@@ -119,3 +122,8 @@

'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({
wildcard: true,
delimiter: '::'
});
var type = 'some::listener::bar';

@@ -134,3 +142,8 @@

'6. the listener added should be the right listener.': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var type = 'some::listener::bar';

@@ -149,3 +162,8 @@ var f = function () {};

'7. Listeners on *, *::*, *::test with emissions from foo::test and other::emit': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var f = function () {

@@ -167,3 +185,8 @@ test.ok(true, 'the event was fired')

'8. Listeners on *, *::*, foo.test with emissions from *, *::* and foo.test': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var f = function () {

@@ -186,3 +209,8 @@ test.ok(true, 'the event was fired')

'9. Listeners on **, **::*, **::test with emissions from foo::test and other::emit': function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var f = function () {

@@ -204,3 +232,9 @@ test.ok(true, 'the event was fired');

'10. Listeners on **, **::*, foo.test with emissions from **, **::* and foo.test': function (test) {
var emitter = this.emitter, i = 0;
var emitter = new EventEmitter2({
wildcard: true,
delimiter: '::'
});
var i = 0;
var f = function (n) {

@@ -207,0 +241,0 @@ return function() {

var basicEvents = require('nodeunit').testCase;
var lib = '../../lib/eventemitter2';
/////helper///////
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(lib).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
function setHelper (emitter, test, testName){

@@ -27,32 +35,12 @@ var eventNames = [

setUp: function (callback) {
var EventEmitter2;
'intialize 1. Configuration Flags Test.': function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require(lib).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
this.emitter = new EventEmitter2({
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
this.emitterDefault = new EventEmitter2({
var emitterDefault = new EventEmitter2({
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'intialize 1. Configuration Flags Test.': function (test) {
// lazy
var emitter = this.emitter,
emitterDefault = this.emitterDefault;
test.ok(!emitterDefault.wildcard, 'default .wildcard should be false');

@@ -67,5 +55,10 @@ test.ok(emitter.wildcard, '.wildcard should be true when set');

var emitter = this.emitter,
emitterDefault = this.emitterDefault;
var emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
var emitterDefault = new EventEmitter2({
});
test.ok(emitter.listenerTree, 'listenerTree should exist');

@@ -72,0 +65,0 @@ test.equal(typeof emitter.listenerTree, 'object', 'listenerTree should be an Object');

var simpleEvents= require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
module.exports = simpleEvents({
var EventEmitter2;
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;
}
module.exports = simpleEvents({
this.emitter = new EventEmitter2({
'1. add a single event and then remove the event.' : function (test) {
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. add a single event and then remove the event.' : function (test) {
var emitter = this.emitter,
type = 'remove.foo.bar',
var type = 'remove.foo.bar',
listeners;

@@ -52,4 +43,9 @@

'2. Add two events and then remove only one of those events.' : function (test) {
var emitter = this.emitter,
type = 'remove.foo.bar',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar',
listeners;

@@ -77,4 +73,9 @@

'3. Add three events and remove only one of the events that was added.' : function (test) {
var emitter = this.emitter,
type = 'remove.foo.bar',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar',
listeners;

@@ -102,4 +103,9 @@

'4. Should error if we don\'t pass a function to the emit method.' : function (test) {
var emitter = this.emitter,
type = 'remove.foo.bar',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar',
listeners;

@@ -125,4 +131,9 @@

'5. Removing one listener should not affect another listener.' : function (test) {
var emitter = this.emitter,
type = 'remove.foo.bar',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar',
listeners;

@@ -152,4 +163,8 @@

var emitter = this.emitter,
type = 'remove.foo.bar',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar',
listeners;

@@ -180,4 +195,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'remove.foo.bar';

@@ -220,4 +238,9 @@

'8. Its ok to listen on wildcard, so it is ok to remove it.' : function (test) {
var emitter = this.emitter,
type1 = '*.wild.card',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type1 = '*.wild.card',
type2 = 'just.another.event',

@@ -243,4 +266,9 @@ listeners;

'9. And (8) should not depend on order of listening.' : function (test) {
var emitter = this.emitter,
type1 = '*.wild.card',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type1 = '*.wild.card',
type2 = 'just.another.event',

@@ -266,4 +294,9 @@ listeners;

'10. Reporting many listeners on wildcard all should removed.' : function (test) {
var emitter = this.emitter,
type1 = '*.wild.card',
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type1 = '*.wild.card',
type2 = 'exact.wild.card',

@@ -270,0 +303,0 @@ listeners;

var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
module.exports = simpleEvents({
setUp: function (callback) {
var EventEmitter2;
'1. A listener added with `once` should only listen once and then be removed.': function (test) {
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
this.emitter = new EventEmitter2({
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. A listener added with `once` should only listen once and then be removed.': function (test) {
var emitter = this.emitter;
var type = 'test1.foo.bar';

@@ -47,3 +37,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -67,3 +61,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -88,3 +86,7 @@

var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -123,4 +125,8 @@ var count = 0;

'5. `removeListener` and `once`': function(test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -139,3 +145,8 @@ var functionA = function() { test.ok(true, 'Event was fired'); };

'6. Listening with a wildcard on once' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.*';

@@ -155,3 +166,8 @@ var functionA = function() { test.ok(true, 'Event was fired'); };

'7. Emitting with a wildcard targeted at once' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -170,7 +186,13 @@ var type2 = 'test1.foo.*';

'8. Emitting with a multi-level wildcard on once': function(test) {
var emitter = this.emitter, i = 0;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var i = 0;
var type = 'test1.**';
var functionA = function(n) {
return function() {
//console.log(n, this.event);
console.log(n, this.event);
test.ok(true, 'Event was fired');

@@ -181,3 +203,3 @@ };

emitter.once(type, functionA(i++));
emitter.on(type,functionA(i++));
emitter.on(type, functionA(i++));
emitter.emit(type); //2

@@ -191,3 +213,8 @@ emitter.emit(type); //1

'9. Emitting with a multi-level wildcard targeted at once' : function (test) {
var emitter = this.emitter;
var emitter = new EventEmitter2({
wildcard : true,
verbose : true
});
var type = 'test1.foo.bar';

@@ -194,0 +221,0 @@ var type2 = 'test1.**';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc