eventemitter3
Advanced tools
Comparing version 0.0.1 to 0.1.0
34
index.js
@@ -37,4 +37,4 @@ 'use strict'; | ||
, length = listeners.length | ||
, handler = listeners[0] | ||
, len = arguments.length | ||
, fn = listeners[0] | ||
, args | ||
@@ -46,18 +46,18 @@ , i; | ||
case 1: | ||
handler.call(this); | ||
fn.call(fn.context || this); | ||
break; | ||
case 2: | ||
handler.call(this, a1); | ||
fn.call(fn.context || this, a1); | ||
break; | ||
case 3: | ||
handler.call(this, a1, a2); | ||
fn.call(fn.context || this, a1, a2); | ||
break; | ||
case 4: | ||
handler.call(this, a1, a2, a3); | ||
fn.call(fn.context || this, a1, a2, a3); | ||
break; | ||
case 5: | ||
handler.call(this, a1, a2, a3, a4); | ||
fn.call(fn.context || this, a1, a2, a3, a4); | ||
break; | ||
case 6: | ||
handler.call(this, a1, a2, a3, a4, a5); | ||
fn.call(fn.context || this, a1, a2, a3, a4, a5); | ||
break; | ||
@@ -70,6 +70,6 @@ | ||
handler.apply(this, args); | ||
fn.apply(fn.context || this, args); | ||
} | ||
if (handler.once) this.removeListener(event, handler); | ||
if (fn.once) this.removeListener(event, fn); | ||
} else { | ||
@@ -80,5 +80,5 @@ for (i = 1, args = new Array(len -1); i < len; i++) { | ||
for (i = 0; i < length; i++) { | ||
listeners[i].apply(this, args); | ||
if (listeners[i].once) this.removeListener(event, handler[i]); | ||
for (i = 0; i < length; fn = listeners[++i]) { | ||
fn.apply(fn.context || this, args); | ||
if (fn.once) this.removeListener(event, fn); | ||
} | ||
@@ -95,7 +95,10 @@ } | ||
* @param {Functon} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @api public | ||
*/ | ||
EventEmitter.prototype.on = function on(event, fn) { | ||
EventEmitter.prototype.on = function on(event, fn, context) { | ||
if (!this._events) this._events = {}; | ||
if (!this._events[event]) this._events[event] = []; | ||
fn.context = context; | ||
this._events[event].push(fn); | ||
@@ -111,7 +114,8 @@ | ||
* @param {Function} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @api public | ||
*/ | ||
EventEmitter.prototype.once = function once(event, fn) { | ||
EventEmitter.prototype.once = function once(event, fn, context) { | ||
fn.once = true; | ||
return this.on(event, fn); | ||
return this.on(event, fn, context); | ||
}; | ||
@@ -118,0 +122,0 @@ |
{ | ||
"name": "eventemitter3", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"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.", | ||
@@ -29,6 +29,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.13.0", | ||
"pre-commit": "0.0.4", | ||
"chai": "~1.8.0" | ||
"mocha": "1.13.x", | ||
"pre-commit": "0.0.x", | ||
"chai": "1.8.x" | ||
} | ||
} |
@@ -13,2 +13,7 @@ # EventEmitter3 | ||
And adds some features you want: | ||
- 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 | ||
@@ -25,5 +30,12 @@ performance, who wouldn't want that. | ||
```bash | ||
npm install --save eventemitter3 | ||
$ npm install --save eventemitter3 | ||
``` | ||
or as a [component](http://component.io) | ||
```bash | ||
$ component install eventemitter3 | ||
``` | ||
then | ||
```js | ||
@@ -30,0 +42,0 @@ var EventEmitter = require('eventemitter3'); |
@@ -18,2 +18,14 @@ 'use strict'; | ||
it('emits with context', function (done) { | ||
var e = new EventEmitter() | ||
, context = 'bar'; | ||
e.on('foo', function (bar) { | ||
expect(bar).to.equal('bar'); | ||
expect(this).to.equal(context); | ||
done(); | ||
}, context).emit('foo', 'bar'); | ||
}); | ||
it('should return true when there are events to emit', function (done) { | ||
@@ -113,2 +125,44 @@ var e = new EventEmitter(); | ||
}); | ||
it('only emits once for multiple events', function () { | ||
var e = new EventEmitter() | ||
, multi = 0 | ||
, foo = 0 | ||
, bar = 0; | ||
e.once('foo', function () { | ||
foo++; | ||
}); | ||
e.once('foo', function () { | ||
bar++; | ||
}); | ||
e.on('foo', function () { | ||
multi++; | ||
}); | ||
e.emit('foo'); | ||
e.emit('foo'); | ||
e.emit('foo'); | ||
e.emit('foo'); | ||
e.emit('foo'); | ||
expect(e.listeners('foo').length).to.equal(1); | ||
expect(multi).to.equal(5); | ||
expect(foo).to.equal(1); | ||
expect(bar).to.equal(1); | ||
}); | ||
it('only emits once with context', function (done) { | ||
var e = new EventEmitter() | ||
, context = 'foo'; | ||
e.once('foo', function (bar) { | ||
expect(this).to.equal(context); | ||
expect(bar).to.equal('bar'); | ||
done(); | ||
}, context).emit('foo', 'bar'); | ||
}); | ||
}); | ||
@@ -115,0 +169,0 @@ |
Sorry, the diff of this file is not supported yet
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
Native code
Supply chain riskContains native code which could be a vector to obscure malicious code, and generally decrease the likelihood of reproducible or reliable installs.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
50
1
1
29984
19
804