Comparing version 0.0.4 to 0.0.5
@@ -0,1 +1,10 @@ | ||
0.0.5 / 2012-03-07 | ||
================== | ||
* Removed the 3 arguments limitation | ||
* Listeners scope set to the emitting object | ||
=> EV is now _100%_ compatible with nodejs' native EventEmitter! | ||
* Added `version` property to the constructor | ||
* Added warning on undefined event emission | ||
0.0.4 / 2012-02-26 | ||
@@ -2,0 +11,0 @@ ================== |
@@ -0,1 +1,2 @@ | ||
var maxListeners = 10 | ||
var maxLength = 3 | ||
@@ -12,2 +13,14 @@ | ||
function sliceArguments (args, index) { | ||
if (args.length === 0) return [] | ||
for ( | ||
var i = index, n = args.length, a = new Array(n - index) | ||
; i < n | ||
; i++ | ||
) | ||
a[i - index] = args[i] | ||
return a | ||
} | ||
/** | ||
@@ -20,7 +33,5 @@ events: { name: argumentsLength, ... } | ||
-> 22k vs 5k req/ms with 2 listeners | ||
- almost 100% node EventEmitter compatible (listeners scope is not set) | ||
cons: | ||
- `emit()` limited to a max of 3 arguments | ||
- about 20% slower than classical emitters when the number of listeners > 22 | ||
- adding/removing listeners is relatively slow | ||
- about 20% slower than classical emitters when the number of listeners > 20 | ||
- adding/removing listeners is slow | ||
**/ | ||
@@ -51,3 +62,2 @@ function EventEmitter (events) { | ||
// setMaxListeners() forces the default value | ||
this._ev_maxListeners = 10 | ||
this.setMaxListeners() | ||
@@ -71,2 +81,7 @@ // Avoid duplicated listeners | ||
this._ev_length[ev] = maxLength | ||
console.error( | ||
'Warning (EventEmitter): undefined event [%s]. ' + | ||
'Define at EventEmitter instanciation for better performance.' | ||
, ev | ||
) | ||
} | ||
@@ -113,2 +128,4 @@ | ||
} | ||
var self = this | ||
// Additional listener, register it according to its # of arguments | ||
@@ -118,4 +135,4 @@ switch ( this._ev_length[ev] ) { | ||
this._ev_emit[ev] = this[key] = function () { | ||
fn() | ||
handler() | ||
fn.call(self) | ||
handler.call(self) | ||
} | ||
@@ -125,4 +142,4 @@ break | ||
this._ev_emit[ev] = this[key] = function () { | ||
fn(arguments[0]) | ||
handler(arguments[0]) | ||
fn.call(self, arguments[0]) | ||
handler.call(self, arguments[0]) | ||
} | ||
@@ -132,4 +149,4 @@ break | ||
this._ev_emit[ev] = this[key] = function () { | ||
fn(arguments[0], arguments[1]) | ||
handler(arguments[0], arguments[1]) | ||
fn.call(self, arguments[0], arguments[1]) | ||
handler.call(self, arguments[0], arguments[1]) | ||
} | ||
@@ -139,9 +156,11 @@ break | ||
this._ev_emit[ev] = this[key] = function () { | ||
fn(arguments[0], arguments[1], arguments[2]) | ||
handler(arguments[0], arguments[1], arguments[2]) | ||
fn.call(self, arguments[0], arguments[1], arguments[2]) | ||
handler.call(self, arguments[0], arguments[1], arguments[2]) | ||
} | ||
break | ||
default: | ||
// Should never get here unless the max args has been altered | ||
throw new Error('Invalid number of arguments:' + this._ev_length[ev] + ' for event: ' + ev) | ||
this._ev_emit[ev] = this[key] = function () { | ||
fn.apply(self, arguments) | ||
handler.apply(self, arguments) | ||
} | ||
} | ||
@@ -200,19 +219,23 @@ return this | ||
case 1: | ||
handler() | ||
handler.call(this) | ||
break | ||
case 2: | ||
handler(arguments[1]) | ||
handler.call(this, arguments[1]) | ||
break | ||
case 3: | ||
handler(arguments[1], arguments[2]) | ||
handler.call(this, arguments[1], arguments[2]) | ||
break | ||
case 4: | ||
handler(arguments[1], arguments[2], arguments[3]) | ||
handler.call(this, arguments[1], arguments[2], arguments[3]) | ||
break | ||
default: | ||
throw new Error('Too many arguments for event ' + arguments[0]) | ||
handler.apply(this, sliceArguments(arguments, 1)) | ||
} | ||
return true | ||
} | ||
EventEmitter.prototype.once = function (ev, handler) { | ||
if (typeof handler !== 'function') | ||
throw new Error('Handler not a function: ' + handler) | ||
var self = this, _handler | ||
@@ -224,3 +247,3 @@ | ||
self.off(ev, _handler) | ||
handler() | ||
handler.call(self) | ||
} | ||
@@ -231,3 +254,3 @@ break | ||
self.off(ev, _handler) | ||
handler(arguments[0]) | ||
handler.call(self, arguments[0]) | ||
} | ||
@@ -238,3 +261,3 @@ break | ||
self.off(ev, _handler) | ||
handler(arguments[0], arguments[1]) | ||
handler.call(self, arguments[0], arguments[1]) | ||
} | ||
@@ -245,7 +268,10 @@ break | ||
self.off(ev, _handler) | ||
handler(arguments[0], arguments[1], arguments[2]) | ||
handler.call(self, arguments[0], arguments[1], arguments[2]) | ||
} | ||
break | ||
default: | ||
throw new Error('Invalid number of arguments:' + this._ev_length[ev] + ' for event: ' + ev) | ||
_handler = function () { | ||
self.off(ev, _handler) | ||
handler.apply(self, arguments) | ||
} | ||
} | ||
@@ -258,3 +284,3 @@ _handler.handler = handler | ||
EventEmitter.prototype.setMaxListeners = function (max) { | ||
this._ev_maxListeners = typeof max !== 'number' ? 10 : max | ||
this._ev_maxListeners = typeof max !== 'number' ? maxListeners : max | ||
return this | ||
@@ -273,2 +299,6 @@ } | ||
// Expose the version | ||
EventEmitter.version = require('../package.json').version | ||
EventEmitter.prototype.version = EventEmitter.version | ||
module.exports = EventEmitter |
@@ -6,3 +6,3 @@ { | ||
, "keywords": ["event","emitter","listener"] | ||
, "version": "0.0.4" | ||
, "version": "0.0.5" | ||
, "homepage": "http://github.com/pierrec/node-ev" | ||
@@ -9,0 +9,0 @@ , "repository": { |
@@ -5,3 +5,3 @@ # 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 fully compatible (as of version 0.0.5) with the nodejs' _EventEmitter_ API, with some additions (cf. the Differences section below). The main purpose of node-ev is to provide very fast event emission when dealing with a relatively low number of listeners: below 20 listeners, it is faster than EventEmitter, after that, both average out. | ||
@@ -16,4 +16,4 @@ ## Differences with node's _EventEmitter_ | ||
* `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 | ||
## API | ||
@@ -23,6 +23,8 @@ | ||
var EV = require('ev') | ||
var ev = new EV({ match: 2 }) | ||
var ev = new EV({ match: 1 }) | ||
function test (a) { | ||
console.log('received', a) | ||
} | ||
ev.on('match', test) | ||
@@ -33,2 +35,3 @@ ev.emit('match', 'standard emit') // received standard emit | ||
### Constructor | ||
@@ -52,16 +55,19 @@ | ||
### Members | ||
* `ev_dedupListener`: will not add a listener more than once to the same event if true | ||
* `version` (_String_): EV version | ||
* `ev_dedupListener` (_Boolean_): will not add a listener more than once to the same event if true | ||
### Methods | ||
* `on(event, listener)`: add a listener for [event] | ||
* `once(event, listener)`: add a listener for [event] and remove it once triggered | ||
* `off(event, listener)`: remove the listener for [event] | ||
* `off(event)`: remove all listeners for [event] | ||
* `removeAllListeners(event)`: remove all listeners for [event] | ||
* `on(event, listener)` (_String_, _Function_): add a listener for [event] | ||
* `once(event, listener)` (_String_, _Function_): add a listener for [event] and remove it once triggered | ||
* `off(event, listener)` (_String_, _Function_): remove the listener for [event] | ||
* `off(event)` (_String_): remove all listeners for [event] | ||
* `removeAllListeners(event)` (_String_): remove all listeners for [event] | ||
* `removeAllListeners()`: remove all listeners for all events | ||
* `emit(event[, arguments])`: emit [event] with a list of arguments | ||
* `setMaxListeners(max)`: set the maximum number of listeners after which a warning is issued, but the listeners are still added | ||
* `listeners([event])`: get the list of listeners for [event] or all listeners for all events | ||
* `emit(event[, arguments])` (_String_[, _Any_]): emit [event] with a list of arguments | ||
* `setMaxListeners(max)` (_Integer_): set the maximum number of listeners after which a warning is issued, but the listeners are still added | ||
* `listeners([event])` (_String_): get the list of listeners for [event] or all listeners for all events |
@@ -20,5 +20,5 @@ /* | ||
describe('emit("match")', function () { | ||
var ev = new EV(options) | ||
it('should trigger the handler with its arguments', function (done) { | ||
var ev = new EV(options) | ||
function handler (a, b) { | ||
@@ -32,8 +32,22 @@ assert.equal(a, 'a') | ||
}) | ||
it('should trigger the handler with its arguments', function (done) { | ||
var ev = new EV(options) | ||
function handler (a, b, c, d) { | ||
assert.equal(a, 'a') | ||
assert.equal(b, 'b') | ||
assert.equal(c, 'c') | ||
assert.equal(d, 'd') | ||
done() | ||
} | ||
ev.on('match', handler) | ||
ev.emit('match', 'a', 'b', 'c', 'd') | ||
}) | ||
}) | ||
describe('emit_match', function () { | ||
var ev = new EV(options) | ||
it('should emit trigger the handler with its arguments', function (done) { | ||
var ev = new EV(options) | ||
it('should emit trigger the handler with its arguments', function (done) { | ||
function handler (a, b) { | ||
@@ -47,2 +61,16 @@ assert.equal(a, 'a') | ||
}) | ||
it('should emit trigger the handler with its arguments', function (done) { | ||
var ev = new EV(options) | ||
function handler (a, b, c, d) { | ||
assert.equal(a, 'a') | ||
assert.equal(b, 'b') | ||
assert.equal(c, 'c') | ||
assert.equal(d, 'd') | ||
done() | ||
} | ||
ev.on('match', handler) | ||
ev.emit_match('a', 'b', 'c', 'd') | ||
}) | ||
}) | ||
@@ -49,0 +77,0 @@ |
# TODO | ||
* `once([event1, event2...], listener)`: mutually exclusive listeners: any event will remove the listener to the other ones | ||
* Use linked objects for handlers: | ||
this.fn | ||
this.prev | ||
this.protottype.exec = this.fn(args...); this.prev.exec(args...) |
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
38430
22
1200
69