eventemitter2
Advanced tools
Comparing version 0.4.14 to 1.0.0
module.exports = require('./lib/eventemitter2'); |
@@ -43,2 +43,3 @@ /*! | ||
} | ||
EventEmitter.EventEmitter2 = EventEmitter; // backwards compatibility for exporting EventEmitter property | ||
@@ -207,3 +208,5 @@ // | ||
tree._listeners.length); | ||
console.trace(); | ||
if(console.trace){ | ||
console.trace(); | ||
} | ||
} | ||
@@ -280,3 +283,3 @@ } | ||
this.event = type; | ||
this._all[i].apply(this, args); | ||
this._all[i].apply(this, [type].concat(args)); | ||
} | ||
@@ -352,2 +355,87 @@ } | ||
EventEmitter.prototype.emitAsync = function() { | ||
this._events || init.call(this); | ||
var type = arguments[0]; | ||
if (type === 'newListener' && !this.newListener) { | ||
if (!this._events.newListener) { return Promise.resolve([false]); } | ||
} | ||
var promises= []; | ||
// Loop through the *_all* functions and invoke them. | ||
if (this._all) { | ||
var l = arguments.length; | ||
var args = new Array(l - 1); | ||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i]; | ||
for (i = 0, l = this._all.length; i < l; i++) { | ||
this.event = type; | ||
promises.push(this._all[i].apply(this, args)); | ||
} | ||
} | ||
// If there is no 'error' event listener then throw. | ||
if (type === 'error') { | ||
if (!this._all && | ||
!this._events.error && | ||
!(this.wildcard && this.listenerTree.error)) { | ||
if (arguments[1] instanceof Error) { | ||
return Promise.reject(arguments[1]); // Unhandled 'error' event | ||
} else { | ||
return Promise.reject("Uncaught, unspecified 'error' event."); | ||
} | ||
} | ||
} | ||
var handler; | ||
if(this.wildcard) { | ||
handler = []; | ||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); | ||
searchListenerTree.call(this, handler, ns, this.listenerTree, 0); | ||
} | ||
else { | ||
handler = this._events[type]; | ||
} | ||
if (typeof handler === 'function') { | ||
this.event = type; | ||
if (arguments.length === 1) { | ||
promises.push(handler.call(this)); | ||
} | ||
else if (arguments.length > 1) { | ||
switch (arguments.length) { | ||
case 2: | ||
promises.push(handler.call(this, arguments[1])); | ||
break; | ||
case 3: | ||
promises.push(handler.call(this, arguments[1], arguments[2])); | ||
break; | ||
// slower | ||
default: | ||
var l = arguments.length; | ||
var args = new Array(l - 1); | ||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i]; | ||
promises.push(handler.apply(this, args)); | ||
} | ||
} | ||
} | ||
else if (handler) { | ||
var l = arguments.length; | ||
var args = new Array(l - 1); | ||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i]; | ||
var listeners = handler.slice(); | ||
for (var i = 0, l = listeners.length; i < l; i++) { | ||
this.event = type; | ||
promises.push(listeners[i].apply(this, args)); | ||
} | ||
} | ||
return Promise.all(promises); | ||
}; | ||
EventEmitter.prototype.on = function(type, listener) { | ||
@@ -402,3 +490,5 @@ | ||
this._events[type].length); | ||
console.trace(); | ||
if(console.trace){ | ||
console.trace(); | ||
} | ||
} | ||
@@ -480,2 +570,5 @@ } | ||
} | ||
this.emit("removeListener", type, listener); | ||
return this; | ||
@@ -492,5 +585,27 @@ } | ||
} | ||
this.emit("removeListener", type, listener); | ||
} | ||
} | ||
function recursivelyGarbageCollect(root) { | ||
if (root === undefined) { | ||
return; | ||
} | ||
var keys = Object.keys(root); | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
var obj = root[key]; | ||
if ((obj instanceof Function) || (typeof obj !== "object")) | ||
continue; | ||
if (Object.keys(obj).length > 0) { | ||
recursivelyGarbageCollect(root[key]); | ||
} | ||
if (Object.keys(obj).length === 0) { | ||
delete root[key]; | ||
} | ||
} | ||
} | ||
recursivelyGarbageCollect(this.listenerTree); | ||
return this; | ||
@@ -506,2 +621,3 @@ }; | ||
fns.splice(i, 1); | ||
this.emit("removeListenerAny", fn); | ||
return this; | ||
@@ -511,2 +627,5 @@ } | ||
} else { | ||
fns = this._all; | ||
for(i = 0, l = fns.length; i < l; i++) | ||
this.emit("removeListenerAny", fns[i]); | ||
this._all = []; | ||
@@ -535,3 +654,3 @@ } | ||
else { | ||
if (!this._events[type]) return this; | ||
if (!this._events || !this._events[type]) return this; | ||
this._events[type] = null; | ||
@@ -577,3 +696,3 @@ } | ||
// CommonJS | ||
exports.EventEmitter2 = EventEmitter; | ||
module.exports = EventEmitter; | ||
} | ||
@@ -580,0 +699,0 @@ else { |
{ | ||
"name": "eventemitter2", | ||
"version": "0.4.14", | ||
"version": "1.0.0", | ||
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.", | ||
@@ -5,0 +5,0 @@ "keywords": ["event", "events", "emitter", "eventemitter"], |
[![build-status](https://www.codeship.io/projects/3ad58940-4c7d-0131-15d5-5a8cd3f550f8/status)](https://www.codeship.io/projects/11259) | ||
[![NPM version](https://badge.fury.io/js/eventemitter2.png)](http://badge.fury.io/js/eventemitter2) | ||
[![Bower version](https://badge.fury.io/bo/eventemitter2.png)](http://badge.fury.io/bo/eventemitter2) | ||
# SYNOPSIS | ||
EventEmitter2 is an implementation of the EventEmitter found in Node.js | ||
EventEmitter2 is an implementation of the EventEmitter module found in Node.js. In addition to having a better benchmark performance than EventEmitter and being browser-compatible, it also extends the interface of EventEmitter with additional non-breaking features. | ||
@@ -23,5 +26,5 @@ # DESCRIPTION | ||
### Differences (Non breaking, compatible with existing EventEmitter) | ||
### Differences (Non-breaking, compatible with existing EventEmitter) | ||
- The constructor takes a configuration object. | ||
- The EventEmitter2 constructor takes an optional configuration object. | ||
@@ -33,3 +36,3 @@ ```javascript | ||
// | ||
// use wildcards. | ||
// set this to `true` to use wildcards. It defaults to `false`. | ||
// | ||
@@ -44,3 +47,3 @@ wildcard: true, | ||
// | ||
// if you want to emit the newListener event set to true. | ||
// set this to `true` if you want to emit the newListener event. The default value is `true`. | ||
// | ||
@@ -50,3 +53,3 @@ newListener: false, | ||
// | ||
// max listeners that can be assigned to an event, default 10. | ||
// the maximum amount of listeners that can be assigned to an event, default 10. | ||
// | ||
@@ -90,3 +93,5 @@ maxListeners: 20 | ||
All EventEmitters emit the event `newListener` when new listeners are | ||
added. | ||
added. EventEmitters also emit the event `removeListener` when listeners are | ||
removed, and `removeListenerAny` when listeners added through `onAny` are | ||
removed. | ||
@@ -112,3 +117,14 @@ | ||
# Multi-level Wildcards | ||
A double wildcard (the string `**`) matches any number of levels (zero or more) of events. So if for example `'foo.**'` is passed to the `on` method, the following events would be observed: | ||
````javascript | ||
emitter.emit('foo'); | ||
emitter.emit('foo.bar'); | ||
emitter.emit('foo.bar.baz'); | ||
```` | ||
On the other hand, if the single-wildcard event name was passed to the on method, the callback would only observe the second of these events. | ||
### emitter.addListener(event, listener) | ||
@@ -133,6 +149,6 @@ ### emitter.on(event, listener) | ||
Adds a listener that will be fired when any event is emitted. | ||
Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback. | ||
```javascript | ||
server.onAny(function(value) { | ||
server.onAny(function(event, value) { | ||
console.log('All events trigger this.'); | ||
@@ -180,3 +196,3 @@ }); | ||
Remove a listener from the listener array for the specified event. | ||
**Caution**: changes array indices in the listener array behind the listener. | ||
**Caution**: Calling this method changes the array indices in the listener array behind the listener. | ||
@@ -235,23 +251,34 @@ ```javascript | ||
# LICENSE | ||
### emitter.emitAsync(event, [arg1], [arg2], [...]) | ||
(The MIT License) | ||
Return the results of the listeners via [Promise.all](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). | ||
Only this method doesn't work [IE](http://caniuse.com/#search=promise). | ||
Copyright (c) 2011 hij1nx <http://www.twitter.com/hij1nx> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the 'Software'), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
```javascript | ||
emitter.on('get',function(i) { | ||
return new Promise(function(resolve){ | ||
setTimeout(function(){ | ||
resolve(i+3); | ||
},50); | ||
}); | ||
}); | ||
emitter.on('get',function(i) { | ||
return new Promise(function(resolve){ | ||
resolve(i+2) | ||
}); | ||
}); | ||
emitter.on('get',function(i) { | ||
return Promise.resolve(i+1); | ||
}); | ||
emitter.on('get',function(i) { | ||
return i+0; | ||
}); | ||
emitter.on('get',function(i) { | ||
// noop | ||
}); | ||
emitter.emitAsync('get',0) | ||
.then(function(results){ | ||
console.log(results); // [3,2,1,0,undefined] | ||
}); | ||
``` |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
31414
5
584
1
276