eventemitter3
Advanced tools
Comparing version 0.1.6 to 1.0.0
77
index.js
@@ -38,11 +38,16 @@ 'use strict'; | ||
* @param {String} event The events that should be listed. | ||
* @returns {Array} | ||
* @param {Boolean} exists We only need to know if there are listeners. | ||
* @returns {Array|Boolean} | ||
* @api public | ||
*/ | ||
EventEmitter.prototype.listeners = function listeners(event) { | ||
if (!this._events || !this._events[event]) return []; | ||
if (this._events[event].fn) return [this._events[event].fn]; | ||
EventEmitter.prototype.listeners = function listeners(event, exists) { | ||
var prefix = '~'+ event | ||
, available = this._events && this._events[prefix]; | ||
for (var i = 0, l = this._events[event].length, ee = new Array(l); i < l; i++) { | ||
ee[i] = this._events[event][i].fn; | ||
if (exists) return !!available; | ||
if (!available) return []; | ||
if (this._events[prefix].fn) return [this._events[prefix].fn]; | ||
for (var i = 0, l = this._events[prefix].length, ee = new Array(l); i < l; i++) { | ||
ee[i] = this._events[prefix][i].fn; | ||
} | ||
@@ -61,5 +66,7 @@ | ||
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) { | ||
if (!this._events || !this._events[event]) return false; | ||
var prefix = '~'+ event; | ||
var listeners = this._events[event] | ||
if (!this._events || !this._events[prefix]) return false; | ||
var listeners = this._events[prefix] | ||
, len = arguments.length | ||
@@ -70,3 +77,3 @@ , args | ||
if ('function' === typeof listeners.fn) { | ||
if (listeners.once) this.removeListener(event, listeners.fn, true); | ||
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true); | ||
@@ -92,3 +99,3 @@ switch (len) { | ||
for (i = 0; i < length; i++) { | ||
if (listeners[i].once) this.removeListener(event, listeners[i].fn, true); | ||
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true); | ||
@@ -121,10 +128,11 @@ switch (len) { | ||
EventEmitter.prototype.on = function on(event, fn, context) { | ||
var listener = new EE(fn, context || this); | ||
var listener = new EE(fn, context || this) | ||
, prefix = '~'+ event; | ||
if (!this._events) this._events = {}; | ||
if (!this._events[event]) this._events[event] = listener; | ||
if (!this._events[prefix]) this._events[prefix] = listener; | ||
else { | ||
if (!this._events[event].fn) this._events[event].push(listener); | ||
else this._events[event] = [ | ||
this._events[event], listener | ||
if (!this._events[prefix].fn) this._events[prefix].push(listener); | ||
else this._events[prefix] = [ | ||
this._events[prefix], listener | ||
]; | ||
@@ -145,10 +153,11 @@ } | ||
EventEmitter.prototype.once = function once(event, fn, context) { | ||
var listener = new EE(fn, context || this, true); | ||
var listener = new EE(fn, context || this, true) | ||
, prefix = '~'+ event; | ||
if (!this._events) this._events = {}; | ||
if (!this._events[event]) this._events[event] = listener; | ||
if (!this._events[prefix]) this._events[prefix] = listener; | ||
else { | ||
if (!this._events[event].fn) this._events[event].push(listener); | ||
else this._events[event] = [ | ||
this._events[event], listener | ||
if (!this._events[prefix].fn) this._events[prefix].push(listener); | ||
else this._events[prefix] = [ | ||
this._events[prefix], listener | ||
]; | ||
@@ -165,9 +174,12 @@ } | ||
* @param {Function} fn The listener that we need to find. | ||
* @param {Mixed} context Only remove listeners matching this context. | ||
* @param {Boolean} once Only remove once listeners. | ||
* @api public | ||
*/ | ||
EventEmitter.prototype.removeListener = function removeListener(event, fn, once) { | ||
if (!this._events || !this._events[event]) return this; | ||
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) { | ||
var prefix = '~'+ event; | ||
var listeners = this._events[event] | ||
if (!this._events || !this._events[prefix]) return this; | ||
var listeners = this._events[prefix] | ||
, events = []; | ||
@@ -180,3 +192,7 @@ | ||
if (!listeners.fn) for (var i = 0, length = listeners.length; i < length; i++) { | ||
if (listeners[i].fn !== fn || (once && !listeners[i].once)) { | ||
if ( | ||
listeners[i].fn !== fn | ||
|| (once && !listeners[i].once) | ||
|| (context && listeners[i].context !== context) | ||
) { | ||
events.push(listeners[i]); | ||
@@ -191,5 +207,5 @@ } | ||
if (events.length) { | ||
this._events[event] = events.length === 1 ? events[0] : events; | ||
this._events[prefix] = events.length === 1 ? events[0] : events; | ||
} else { | ||
delete this._events[event]; | ||
delete this._events[prefix]; | ||
} | ||
@@ -209,3 +225,3 @@ | ||
if (event) delete this._events[event]; | ||
if (event) delete this._events['~'+ event]; | ||
else this._events = {}; | ||
@@ -232,9 +248,2 @@ | ||
// | ||
EventEmitter.EventEmitter = EventEmitter; | ||
EventEmitter.EventEmitter2 = EventEmitter; | ||
EventEmitter.EventEmitter3 = EventEmitter; | ||
// | ||
// Expose the module. | ||
// | ||
module.exports = EventEmitter; |
{ | ||
"name": "eventemitter3", | ||
"version": "0.1.6", | ||
"version": "1.0.0", | ||
"description": "EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --reporter spec --ui bdd test.js", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- --reporter spec --ui bdd test.js", | ||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec test.js" | ||
"test": "mocha test.js", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- test.js", | ||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- test.js" | ||
}, | ||
@@ -38,7 +38,7 @@ "repository": { | ||
"devDependencies": { | ||
"assume": "0.0.x", | ||
"assume": "1.2.x", | ||
"istanbul": "0.3.x", | ||
"mocha": "2.0.x", | ||
"pre-commit": "0.0.x" | ||
"mocha": "2.2.x", | ||
"pre-commit": "1.0.x" | ||
} | ||
} |
# EventEmitter3 | ||
[![Version npm](http://img.shields.io/npm/v/eventemitter3.svg?style=flat-square)](http://browsenpm.org/package/eventemitter3)[![Build Status](http://img.shields.io/travis/primus/EventEmitter3/master.svg?style=flat-square)](https://travis-ci.org/primus/EventEmitter3)[![Dependencies](https://img.shields.io/david/primus/EventEmitter3.svg?style=flat-square)](https://david-dm.org/primus/EventEmitter3)[![Coverage Status](http://img.shields.io/coveralls/primus/EventEmitter3/master.svg?style=flat-square)](https://coveralls.io/r/primus/EventEmitter3?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=primus) | ||
[![Version npm](https://img.shields.io/npm/v/eventemitter3.svg?style=flat-square)](http://browsenpm.org/package/eventemitter3)[![Build Status](https://img.shields.io/travis/primus/eventemitter3/master.svg?style=flat-square)](https://travis-ci.org/primus/eventemitter3)[![Dependencies](https://img.shields.io/david/primus/eventemitter3.svg?style=flat-square)](https://david-dm.org/primus/eventemitter3)[![Coverage Status](https://img.shields.io/coveralls/primus/eventemitter3/master.svg?style=flat-square)](https://coveralls.io/r/primus/eventemitter3?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=primus) | ||
EventEmitter3 is a faster alternative to EventEmitter2 and the built-in | ||
EventEmitter that ships within Node.js. It removes some features that you might | ||
not need: | ||
EventEmitter3 is a high performance EventEmitter. It has been micro-optimized | ||
for various of code paths making this one of the, if not the fastest | ||
EventEmitter available for Node.js and browsers. The module is API compatible | ||
with the EventEmitter that ships by default within Node.js but there are some | ||
slight differences: | ||
- Domain support. | ||
- Thrown errors when there are no error listeners specified. | ||
- That a `newListener` event is emitted when an event is emitted. | ||
- No silly `setMaxListeners`, the function exists, but does nothing. | ||
- No silly `listenerCount` function.. Just do `EventEmitter.listeners(event).length` | ||
- Domain support has been removed. | ||
- We do not `throw` an error when you emit an `error` event and nobody is | ||
listening. | ||
- The `newListener` event is removed as the use-cases for this functionality are | ||
really just edge cases. | ||
- No `setMaxListeners` and it's pointless memory leak warnings. If you want to | ||
add `end` listeners you should be able to do that without modules complaining. | ||
- No `listenerCount` function. Use `EE.listeners(event).length` instead. | ||
- Support for custom context for events so there is no more `fn.bind` required. | ||
- `listeners` method can now do existence checking instead returning full arrays | ||
And adds some features you want: | ||
It's a drop in replacement for existing EventEmitters, but just faster. Free | ||
performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3 | ||
so it will work in the oldest browsers and node versions that you need to | ||
support. | ||
- Emit events with a custom context without binding: `EE.on(event, fn, context)` | ||
which also works with once `EE.once(event, fn, context)` | ||
It's a drop in replacement of your existing EventEmitters, but just faster. Free | ||
performance, who wouldn't want that? | ||
The source of the EventEmitter is compatible for browser usage, no fancy pancy | ||
`Array.isArray` stuff is used, it's just plain ol JavaScript that should even | ||
work IE5 if you want to. | ||
## Installation | ||
```bash | ||
$ npm install --save eventemitter3 | ||
$ npm install --save eventemitter3 # npm | ||
$ component install primus/eventemitter3 # Component | ||
$ bower install primus/eventemitter3 # Bower | ||
``` | ||
or as a [component](http://component.io) | ||
```bash | ||
$ component install eventemitter3 | ||
``` | ||
## Usage | ||
then | ||
After installation the only thing you need to do is require the module: | ||
```js | ||
var EventEmitter = require('eventemitter3'); | ||
``` | ||
// or | ||
And you're ready to create your own EventEmitter instances. For the API | ||
documentation, please follow the official Node.js documentation: | ||
var EventEmitter = require('eventemitter3').EventEmitter; | ||
http://nodejs.org/api/events.html | ||
### Contextual emits | ||
We've upgraded the API of the `EventEmitter.on`, `EventEmitter.once` and | ||
`EventEmitter.removeListener` to accept an extra argument which is the `context` | ||
or `this` value that should be set for the emitted events. This means you no | ||
longer have the overhead of an event that required `fn.bind` in order to get a | ||
custom `this` value. | ||
```js | ||
var EE = new EventEmitter() | ||
, context = { foo: 'bar' }; | ||
function emitted() { | ||
console.log(this === context); // true | ||
} | ||
EE.once('event-name', emitted, context); | ||
EE.on('another-event', emitted, context); | ||
EE.removeListener('another-event', emitted, context); | ||
``` | ||
For API methods see the official Node.js documentation: | ||
### Existence | ||
http://nodejs.org/api/events.html | ||
To check if there is already a listener for a given event you can supply the | ||
`listeners` method with an extra boolean argument. This will transform the | ||
output from an array, to a boolean value which indicates if there are listeners | ||
in place for the given event: | ||
```js | ||
var EE = new EventEmitter(); | ||
EE.once('event-name', function () {}); | ||
EE.on('another-event', function () {}); | ||
EE.listeners('event-name', true); // returns true | ||
EE.listeners('unknown-name', true); // returns false | ||
``` | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
1
88
12083
4
202
2