eventemitter3
Advanced tools
Comparing version 0.1.5 to 0.1.6
79
index.js
@@ -43,5 +43,6 @@ 'use strict'; | ||
if (!this._events || !this._events[event]) return []; | ||
if (this._events[event].fn) return [this._events[event].fn]; | ||
for (var i = 0, l = this._events[event].length, ee = []; i < l; i++) { | ||
ee.push(this._events[event][i].fn); | ||
for (var i = 0, l = this._events[event].length, ee = new Array(l); i < l; i++) { | ||
ee[i] = this._events[event][i].fn; | ||
} | ||
@@ -63,18 +64,16 @@ | ||
var listeners = this._events[event] | ||
, length = listeners.length | ||
, len = arguments.length | ||
, ee = listeners[0] | ||
, args | ||
, i, j; | ||
, i; | ||
if (1 === length) { | ||
if (ee.once) this.removeListener(event, ee.fn, true); | ||
if ('function' === typeof listeners.fn) { | ||
if (listeners.once) this.removeListener(event, listeners.fn, true); | ||
switch (len) { | ||
case 1: return ee.fn.call(ee.context), true; | ||
case 2: return ee.fn.call(ee.context, a1), true; | ||
case 3: return ee.fn.call(ee.context, a1, a2), true; | ||
case 4: return ee.fn.call(ee.context, a1, a2, a3), true; | ||
case 5: return ee.fn.call(ee.context, a1, a2, a3, a4), true; | ||
case 6: return ee.fn.call(ee.context, a1, a2, a3, a4, a5), true; | ||
case 1: return listeners.fn.call(listeners.context), true; | ||
case 2: return listeners.fn.call(listeners.context, a1), true; | ||
case 3: return listeners.fn.call(listeners.context, a1, a2), true; | ||
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true; | ||
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true; | ||
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true; | ||
} | ||
@@ -86,4 +85,7 @@ | ||
ee.fn.apply(ee.context, args); | ||
listeners.fn.apply(listeners.context, args); | ||
} else { | ||
var length = listeners.length | ||
, j; | ||
for (i = 0; i < length; i++) { | ||
@@ -118,5 +120,12 @@ if (listeners[i].once) this.removeListener(event, listeners[i].fn, true); | ||
EventEmitter.prototype.on = function on(event, fn, context) { | ||
var listener = new EE(fn, context || this); | ||
if (!this._events) this._events = {}; | ||
if (!this._events[event]) this._events[event] = []; | ||
this._events[event].push(new EE( fn, context || this )); | ||
if (!this._events[event]) this._events[event] = listener; | ||
else { | ||
if (!this._events[event].fn) this._events[event].push(listener); | ||
else this._events[event] = [ | ||
this._events[event], listener | ||
]; | ||
} | ||
@@ -135,5 +144,12 @@ return this; | ||
EventEmitter.prototype.once = function once(event, fn, context) { | ||
var listener = new EE(fn, context || this, true); | ||
if (!this._events) this._events = {}; | ||
if (!this._events[event]) this._events[event] = []; | ||
this._events[event].push(new EE(fn, context || this, true )); | ||
if (!this._events[event]) this._events[event] = listener; | ||
else { | ||
if (!this._events[event].fn) this._events[event].push(listener); | ||
else this._events[event] = [ | ||
this._events[event], listener | ||
]; | ||
} | ||
@@ -157,6 +173,11 @@ return this; | ||
if (fn) for (var i = 0, length = listeners.length; i < length; i++) { | ||
if (listeners[i].fn !== fn && listeners[i].once !== once) { | ||
events.push(listeners[i]); | ||
if (fn) { | ||
if (listeners.fn && (listeners.fn !== fn || (once && !listeners.once))) { | ||
events.push(listeners); | ||
} | ||
if (!listeners.fn) for (var i = 0, length = listeners.length; i < length; i++) { | ||
if (listeners[i].fn !== fn || (once && !listeners[i].once)) { | ||
events.push(listeners[i]); | ||
} | ||
} | ||
} | ||
@@ -167,4 +188,7 @@ | ||
// | ||
if (events.length) this._events[event] = events; | ||
else this._events[event] = null; | ||
if (events.length) { | ||
this._events[event] = events.length === 1 ? events[0] : events; | ||
} else { | ||
delete this._events[event]; | ||
} | ||
@@ -183,3 +207,3 @@ return this; | ||
if (event) this._events[event] = null; | ||
if (event) delete this._events[event]; | ||
else this._events = {}; | ||
@@ -210,4 +234,5 @@ | ||
if ('object' === typeof module && module.exports) { | ||
module.exports = EventEmitter; | ||
} | ||
// | ||
// Expose the module. | ||
// | ||
module.exports = EventEmitter; |
{ | ||
"name": "eventemitter3", | ||
"version": "0.1.5", | ||
"description": "EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface. This the source of the same EventEmitter that is used in Primus.", | ||
"version": "0.1.6", | ||
"description": "EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=testing ./node_modules/.bin/mocha $(find test -name '*.test.js')" | ||
"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" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/3rd-Eden/EventEmitter3.git" | ||
"url": "git://github.com/primus/EventEmitter3.git" | ||
}, | ||
@@ -33,9 +35,10 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/3rd-Eden/EventEmitter3/issues" | ||
"url": "https://github.com/primus/EventEmitter3/issues" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.18.x", | ||
"pre-commit": "0.0.x", | ||
"chai": "1.9.x" | ||
"assume": "0.0.x", | ||
"istanbul": "0.3.x", | ||
"mocha": "2.0.x", | ||
"pre-commit": "0.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) | ||
EventEmitter3 is a faster alternative to EventEmitter2 and the built-in | ||
@@ -10,3 +12,3 @@ EventEmitter that ships within Node.js. It removes some features that you might | ||
- That a `newListener` event is emitted when an event is emitted. | ||
- No silly `setMaxListeners`. | ||
- No silly `setMaxListeners`, the function exists, but does nothing. | ||
- No silly `listenerCount` function.. Just do `EventEmitter.listeners(event).length` | ||
@@ -24,4 +26,3 @@ | ||
`Array.isArray` stuff is used, it's just plain ol JavaScript that should even | ||
work IE5 if you want to. This module currently serves its use in | ||
[Primus](http://github.com/primus/primus)'s client file. | ||
work IE5 if you want to. | ||
@@ -49,4 +50,4 @@ ## Installation | ||
For API methods see the official Node.js documentation: | ||
For API methods see the official Node.js documentation: | ||
http://nodejs.org/api/events.html |
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
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
51
21828
4
8
512
1