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.0 to 0.4.1

test/common.js

149

lib/eventemitter2.js
;!function(environment, undefined) {
;!function(exports, undefined) {
var EventEmitter2 = environment.EventEmitter2 = function(conf) {
this.init();
this.setConf(conf);
};
var isArray = Array.isArray;
var defaultMaxListeners = 10;
EventEmitter2.prototype.init = function() {
function init() {
this._events = new Object;
this.defaultMaxListeners = 10;
};
}
var isArray = Array.isArray;
function configure(conf) {
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
if (conf) {
this.wildcard = conf.wildcard;
this.delimiter = conf.delimiter || '.';
EventEmitter2.prototype.setConf = function(conf) {
this.wildcard = conf && conf.wildcard;
this.delimiter = conf && conf.delimiter || '.';
if(this.wildcard) {
this.listenerTree = new Object;
if (this.wildcard) {
this.listenerTree = new Object;
}
}
};
}
EventEmitter2.prototype.setMaxListeners = function(n) {
this._events || this.init();
this._events.maxListeners = n;
};
function EventEmitter(conf) {
this._events = new Object;
configure.call(this, conf);
}
EventEmitter2.prototype.event = '';
var searchListenerTree = function(handlers, type, tree, i) {
function searchListenerTree(handlers, type, tree, i) {
if (!tree) {
return
return;
}
var listeners;
if (i === type.length && tree._listeners) {

@@ -86,7 +76,7 @@ //

}
return listeners;
};
var growListenerTree = function(type, listener) {
function growListenerTree(type, listener) {

@@ -120,3 +110,3 @@ type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();

var m = this.defaultMaxListeners;
var m = defaultMaxListeners;

@@ -141,3 +131,17 @@ if (m > 0 && tree._listeners.length > m) {

EventEmitter2.prototype.once = function(event, fn) {
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function(n) {
this._events || init.call(this);
this._events.maxListeners = n;
};
EventEmitter.prototype.event = '';
EventEmitter.prototype.once = function(event, fn) {
this.many(event, 1, fn);

@@ -147,3 +151,3 @@ return this;

EventEmitter2.prototype.many = function(event, ttl, fn) {
EventEmitter.prototype.many = function(event, ttl, fn) {
var self = this;

@@ -157,3 +161,3 @@

if (--ttl === 0) {
self.un(event, listener);
self.off(event, listener);
}

@@ -170,9 +174,6 @@ fn.apply(null, arguments);

EventEmitter2.prototype.emit = function() {
this._events || this.init();
EventEmitter.prototype.emit = function() {
this._events || init.call(this);
var type = arguments[0];
this.event = type;
// If there is no 'error' event listener then throw.

@@ -183,3 +184,3 @@ if (type === 'newListener') {

// Loop through the *_allListenerFn* functions and invoke them.
// Loop through the *_all* functions and invoke them.
if (this._all) {

@@ -190,2 +191,3 @@ var l = arguments.length;

for (i = 0, l = this._all.length; i < l; i++) {
this.event = type;
this._all[i].apply(this, args);

@@ -195,4 +197,9 @@ }

// If there is no 'error' event listener then throw.
if (type === 'error') {
if (this._events.error && typeof this._events.error !== 'function') {
if (!this._all &&
!this._events.error &&
!(this.wildcard && this.listenerTree.error)) {
if (arguments[1] instanceof Error) {

@@ -215,6 +222,7 @@ throw arguments[1]; // Unhandled 'error' event

else {
handler = this._events[type];
handler = this._events[type];
}
if (typeof handler === 'function') {
this.event = type;
if (arguments.length === 1) {

@@ -239,3 +247,3 @@ handler.call(this);

return true;
}
}
else if (handler) {

@@ -248,2 +256,3 @@ var l = arguments.length;

for (var i = 0, l = listeners.length; i < l; i++) {
this.event = type;
listeners[i].apply(this, args);

@@ -256,4 +265,4 @@ }

EventEmitter2.prototype.on = function(type, listener) {
this._events || this.init();
EventEmitter.prototype.on = function(type, listener) {
this._events || init.call(this);

@@ -288,3 +297,3 @@ // To avoid recursion in the case that type == "newListeners"! Before

} else {
m = this.defaultMaxListeners;
m = defaultMaxListeners;
}

@@ -306,3 +315,3 @@

EventEmitter2.prototype.onAny = function(fn) {
EventEmitter.prototype.onAny = function(fn) {

@@ -319,7 +328,8 @@ if(!this._all) {

this._all.push(fn);
return this;
};
EventEmitter2.prototype.addListener = EventEmitter2.prototype.on;
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
EventEmitter2.prototype.un = function(type, listener) {
EventEmitter.prototype.off = function(type, listener) {
if (typeof listener !== 'function') {

@@ -334,3 +344,3 @@ throw new Error('removeListener only takes instances of Function');

var leaf = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
if('undefined' === typeof leaf) { return this; }

@@ -358,4 +368,4 @@ handlers = leaf._listeners;

if (position < 0) {
return this;
if (position < 0) {
return this;
}

@@ -378,3 +388,3 @@

}
}
}
else if (handlers === listener ||

@@ -394,3 +404,3 @@ (handlers.listener && handlers.listener === listener) ||

EventEmitter2.prototype.unAny = function(fn) {
EventEmitter.prototype.offAny = function(fn) {
var i = 0, l = 0, fns;

@@ -411,7 +421,7 @@ if (fn && this._all && this._all.length > 0) {

EventEmitter2.prototype.removeListener = EventEmitter2.prototype.un;
EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
EventEmitter2.prototype.removeAllListeners = function(type) {
EventEmitter.prototype.removeAllListeners = function(type) {
if (arguments.length === 0) {
this._events || this.init();
!this._events || init.call(this);
return this;

@@ -434,3 +444,3 @@ }

EventEmitter2.prototype.listeners = function(type) {
EventEmitter.prototype.listeners = function(type) {
if(this.wildcard) {

@@ -443,3 +453,3 @@ var handlers = [];

this._events || this.init();
this._events || init.call(this);

@@ -453,2 +463,15 @@ if (!this._events[type]) this._events[type] = [];

EventEmitter.prototype.listenersAny = function() {
if(this._all) {
return this._all;
}
else {
return [];
}
};
exports.EventEmitter2 = EventEmitter;
}(typeof exports === 'undefined' ? window : exports);
{
"name": "eventemitter2",
"version": "0.4.0",
"version": "0.4.1",
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.",

@@ -30,4 +30,4 @@ "keywords": ["event", "events", "emitter", "eventemitter"],

"test" : "nodeunit test/simple/* && nodeunit test/wildcardEvents/*",
"benchmark" : "node test/perf/benchmark-listener.js"
"benchmark" : "node test/perf/benchmark.js"
}
}

@@ -79,2 +79,21 @@

**Namespaces** with **Wildcards**
To use namespaces/wildcards, pass the `wildcard` option into the EventEmitter constructor.
When namespaces/wildcards are enabled, events can either be strings (`foo.bar`) separated
by a delimiter or arrays (`['foo', 'bar']`). The delimiter is also configurable as a
constructor option.
An event name passed to any event emitter method can contain a wild card (the `*` character).
If the event name is a string, a wildcard may appear as `foo.*`. If the event name is an array,
the wildcard may appear as `['foo', '*']`.
If either of the above described events were passed to the `on` method, subsequent emits such
as the following would be observed...
```javascript
emitter.emit(['foo.bazz']);
emitter.emit(['foo', 'bar']);
```
#### emitter.addListener(event, listener)

@@ -107,3 +126,3 @@ #### emitter.on(event, listener)

#### emitter.unAny(listener)
#### emitter.offAny(listener)

@@ -113,3 +132,3 @@ Removes the listener that will be fired when any event is emitted.

```javascript
server.unAny(function(value) {
server.offAny(function(value) {
console.log('This event will be listened to exactly four times.');

@@ -141,3 +160,3 @@ });

#### emitter.removeListener(event, listener)
#### emitter.un(event, listener)
#### emitter.off(event, listener)

@@ -144,0 +163,0 @@ Remove a listener from the listener array for the specified event. **Caution**: changes array indices in the listener array behind the listener.

@@ -146,3 +146,3 @@ var simpleEvents = require('nodeunit').testCase;

emitter.emit('test23.ns5.ns5', 'someData'); //1
emitter.unAny(f);
emitter.offAny(f);
emitter.emit('test21'); //0

@@ -156,5 +156,20 @@ emitter.onAny(f);

},
'8. should be able to listen on any event (should cause an error)' : function (test) {
var emitter = this.emitter;
var type = 'somelistenerbar';
var f = function () {
test.ok(true, 'the event was fired')
};
emitter.onAny(f);
emitter.emit('error');
test.expect(1);
test.done();
}
});

@@ -214,3 +214,3 @@

},
'9. Listeners on `*`. (using an array)': function (test) {

@@ -222,3 +222,3 @@

};
emitter.on(['*'], f);

@@ -231,2 +231,21 @@ emitter.emit('*')

'10. actual event name': function(test) {
var emitter = this.emitter;
emitter.on('foo', function() {
emitter.emit('bar'); // changes the current event, passes the old one in as a parameter.
});
emitter.on('*', function() {
console.log(this.event);
});
emitter.emit('foo');
test.done();
}
});

@@ -540,3 +540,3 @@ var basicEvents = require('nodeunit').testCase;

emitter.unAny(fn);
emitter.offAny(fn);
listeners = emitter.listenersAny();

@@ -550,3 +550,3 @@ test.ok(listeners.length === 0, 'should be no any listeners');

emitter.unAny();
emitter.offAny();
listeners = emitter.listenersAny();

@@ -553,0 +553,0 @@ test.ok(listeners.length === 0, 'should be no any listeners');

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