Comparing version 0.0.1 to 0.0.2
@@ -28,2 +28,4 @@ var maxLength = 3 | ||
// Handler removed, emit an event | ||
events.oldListener = 2 | ||
// New handler added, emit an event | ||
@@ -47,4 +49,7 @@ events.newListener = 2 | ||
// setMaxListeners() forces the default value | ||
this._ev_maxListeners = 10 | ||
this.setMaxListeners() | ||
// Avoid duplicated listeners | ||
this.ev_dedupListener = false | ||
@@ -70,8 +75,18 @@ var evs = Object.keys(events), max | ||
// Do not add the listener if already there and flag activated | ||
var cache = this._ev_cache[ev] | ||
var n = cache.push(handler) - 1 | ||
if (this.ev_dedupListener) { | ||
for (var i = 0; i < n; i++) { | ||
if (cache[i] === handler || cache[i].handler === handler) break | ||
} | ||
// Handler found | ||
if (i < n) return this | ||
} | ||
// Emit the newListener event | ||
this.emit_newListener(ev, handler) | ||
var n = this._ev_cache[ev].push(handler) | ||
if ( this._ev_maxListeners > 0 && n > this._ev_maxListeners && !this._ev_maxWarning[ev] ) { | ||
if ( this._ev_maxListeners > 0 && n >= this._ev_maxListeners && !this._ev_maxWarning[ev] ) { | ||
// Straight from node's EventEmitter | ||
@@ -91,3 +106,3 @@ console.error( | ||
// This is the first listener! | ||
if (n === 1) { | ||
if (n === 0) { | ||
this._ev_emit[ev] = this[key] = handler | ||
@@ -139,4 +154,6 @@ return this | ||
} | ||
// Handler not found | ||
if (i === n) return this | ||
// Remove the handler | ||
this.emit_oldListener(ev, handler) | ||
cache.splice(i, 1) | ||
@@ -197,3 +214,3 @@ // Rebuild the listeners | ||
function _handler () { | ||
self.off(ev, _handler, handler) | ||
self.off(ev, _handler) | ||
handler() | ||
@@ -218,4 +235,5 @@ } | ||
EventEmitter.prototype.on = EventEmitter.prototype.addListener | ||
EventEmitter.prototype.addEventListener = EventEmitter.prototype.addListener | ||
EventEmitter.prototype.off = EventEmitter.prototype.removeListener | ||
module.exports = EventEmitter |
{ | ||
"author": "Pierre Curto" | ||
, "name": "ev" | ||
, "description": "Fast event emitter" | ||
, "description": "Fast event emitter - compatible with node's EventEmitter but faster!" | ||
, "keywords": ["event","emitter","listener"] | ||
, "version": "0.0.1" | ||
, "version": "0.0.2" | ||
, "homepage": "http://github.com/pierrec/node-ev" | ||
@@ -8,0 +8,0 @@ , "repository": { |
@@ -5,10 +5,13 @@ # README | ||
This is yet another event emitter implementation for [node.js](http://nodejs.org). It is nearly fully compatible with the node EventEmitter API (see the differences section). The main purpose for node-ev is to provide very fast event emission when dealing with a relatively low number of listeners. Run the provided benchmarks to see if this is for you. | ||
This is yet another event emitter implementation for [node.js](http://nodejs.org). It is nearly fully compatible with the node _EventEmitter_ API (see the differences section). The main purpose for node-ev is to provide very fast event emission when dealing with a relatively low number of listeners. Run the provided benchmarks to see if this is for you. | ||
## Differences with node's EventEmitter | ||
## Differences with node's _EventEmitter_ | ||
* New methods | ||
* `.off(event[, listener])`: alias to `removeListener()` | ||
* `.emit_myevent()`: alias for `emit('myevent')` | ||
* Listeners are triggered without any context whereas EventEmitter applies the emitter's | ||
* `off(event[, listener])`: alias to `removeListener()` | ||
* `addEventListener(event, listener)`: alias to `addListener()` | ||
* `emit_myevent([ arg1... ])`: alias for `emit('myevent'[, arg1... ])` | ||
* New member | ||
* `ev_dedupListener`: does not add a listener if already defined for a given event | ||
* Listeners are triggered without any context whereas _EventEmitter_ applies the emitter's | ||
@@ -28,2 +31,24 @@ ## API | ||
### Constructor | ||
The constructor takes an options object listing the possible events to be emitted and their corresponding number of arguments. Note that EV *will* emit events even though they may not have been set by the constructor but performance will be affected in highly demanding situations. | ||
```javascript | ||
{ | ||
data: 3 | ||
, end: 1 | ||
, drain: 0 | ||
} | ||
``` | ||
There are 3 events defined by default: | ||
* `error(Error)`: if emitted while no listener attached to it, it will throw as per _EventEmitter_'s behavior | ||
* `newListener(event, listener)`: emitted when a listener is attached to an event | ||
* `oldListener(event, listener)`: emitted when a listener is removed from an event | ||
### Members | ||
* `ev_dedupListener`: will not add a listener more than once to the same event if true | ||
### Methods | ||
@@ -30,0 +55,0 @@ |
@@ -12,3 +12,3 @@ /* | ||
describe('alias', function () { | ||
describe('aliases', function () { | ||
it('addListener === on', function (done) { | ||
@@ -18,2 +18,6 @@ assert.deepEqual(EV.addListener, EV.on) | ||
}) | ||
it('addListener === addEventListener', function (done) { | ||
assert.deepEqual(EV.addListener, EV.addEventListener) | ||
done() | ||
}) | ||
}) | ||
@@ -20,0 +24,0 @@ |
@@ -53,2 +53,16 @@ /* | ||
}) | ||
describe('removing an existing listener', function () { | ||
var ev = new EV(options) | ||
it('should emit oldListener', function (done) { | ||
ev.on('oldListener', function (ev, listener) { | ||
assert.equal(ev, 'match') | ||
assert.deepEqual(listener, handler1) | ||
done() | ||
}) | ||
ev.on('match', handler1) | ||
ev.off('match', handler1) | ||
}) | ||
}) | ||
}) |
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
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
26174
17
807
63