eventemitter-ex
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -16,3 +16,3 @@ (function () { | ||
EventEmitterEx.prototype.onAllExcept = function onAllExcept (f /* arguments */) { | ||
assertFunction(f); | ||
assertIsFunction(f); | ||
@@ -71,2 +71,3 @@ var except = Array.prototype.slice.call(arguments, 1); | ||
} | ||
return this; | ||
}; | ||
@@ -78,11 +79,19 @@ | ||
mapArgs.forEach(assertFunction); | ||
mapArgs.forEach(assertIsFunction); | ||
eex.pipeExcept(this, 'end'); | ||
this.on('end', function (/* arguments */) { | ||
var endArgs = arguments; | ||
var result = mapArgs.map(function (f) { | ||
return f.apply(null, endArgs); | ||
}); | ||
result.unshift('end'); | ||
try { | ||
var endArgs = arguments; | ||
var result = mapArgs.map(function (f) { | ||
var res = f.apply(eex, endArgs); | ||
return Array.isArray(res) ? res : [res]; | ||
}); | ||
// flatten the array | ||
result = [].concat.apply([], result); | ||
result.unshift('end'); | ||
} catch (err) { | ||
eex.emit('error', err); | ||
return; | ||
} | ||
eex.emit.apply(eex, result); | ||
@@ -95,12 +104,12 @@ }); | ||
EventEmitterEx.prototype.flatMap = function flatMap (f) { | ||
assertFunction(f); | ||
assertIsFunction(f); | ||
var res = new EventEmitterEx(); | ||
var eex = new EventEmitterEx(); | ||
res.pipeExcept(this, 'end'); | ||
eex.pipeExcept(this, 'end'); | ||
this.on('end', function (/* arguments */) { | ||
res.pipeExcept(f.apply(null, arguments)); | ||
eex.pipeExcept(f.apply(eex, arguments)); | ||
}); | ||
return res; | ||
return eex; | ||
}; | ||
@@ -123,17 +132,16 @@ | ||
EventEmitterEx.listenerCount = function listenerCount (eex, type) { | ||
return (typeof eex.listenerCountOnAll === 'function' ? eex.listenerCountOnAll(type) : 0) + EE.listenerCount(eex, type); | ||
return (typeof eex.listenerCountOnAll === 'function' ? eex.listenerCountOnAll(type) : 0) + | ||
EE.listenerCount(eex, type); | ||
}; | ||
// Should NOT emit exceptions from function as errors. | ||
// Code should not catch exceptions, thrown by listeners for 'end' event because | ||
// emitting 'error' in that case is wrong - it is the callback that is failed, not the original | ||
// operation. Also calling more than one 'final' callback is wrong. | ||
EventEmitterEx.startAsync = function startAsync (f) { | ||
assertFunction(f); | ||
assertIsFunction(f); | ||
var r = new EventEmitterEx(); | ||
setImmediate(function () { | ||
try { | ||
f(r); | ||
} catch (err) { | ||
r.emit('error', err); | ||
} | ||
}); | ||
setImmediate(f.bind(null, r)); | ||
@@ -143,3 +151,3 @@ return r; | ||
function assertFunction (f) { | ||
function assertIsFunction (f) { | ||
if (typeof f !== 'function') | ||
@@ -146,0 +154,0 @@ throw new TypeError('Argument must be a function. Got ' + typeof f); |
{ | ||
"name": "eventemitter-ex", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "EventEmitter extensions", | ||
@@ -5,0 +5,0 @@ "main": "EventEmitterEx.js", |
@@ -31,13 +31,2 @@ (function () { | ||
it('should emit exceptions from function as errors', function (done) { | ||
var e = new Error('234'); | ||
var eexResult = EEX.startAsync(function () { | ||
throw e; | ||
}); | ||
eexResult.on('error', function (err) { | ||
err.should.be.equal(e); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -56,2 +45,6 @@ | ||
it('should return self #' + i, function () { | ||
emitter.pipeExcept(source).should.be.equal(emitter); | ||
}); | ||
it('should call corresponding callbacks on original emitter #' + i, function () { | ||
@@ -237,2 +230,40 @@ var spyA = sinon.spy(), | ||
it('should set this to emitter', function (done) { | ||
var mapped = emitter.map(function () { return this; }); | ||
mapped | ||
.on('end', function (result) { | ||
result.should.be.equal(mapped); | ||
done(); | ||
}) | ||
.on('error', done); | ||
emitter.emit('end'); | ||
}); | ||
it('should support returning multiple values as array', function (done) { | ||
var A = 1, B = 2; | ||
var mapped = emitter.map(function () { return [A, B]; }); | ||
mapped | ||
.on('end', function (a, b) { | ||
a.should.be.equal(A); | ||
b.should.be.equal(B); | ||
done(); | ||
}) | ||
.on('error', done); | ||
emitter.emit('end'); | ||
}); | ||
it('should emit exceptions as error', function (done) { | ||
var err = new Error('234'); | ||
emitter | ||
.map(function () { throw err; }) | ||
.on('end', function () { | ||
done(new Error('Expecting error')); | ||
}) | ||
.on('error', function (error) { | ||
error.should.be.equal(err); | ||
done(); | ||
}); | ||
emitter.emit('end'); | ||
}); | ||
it('should throw exception on non-function arguments', function () { | ||
@@ -265,2 +296,18 @@ expect(function () { | ||
it('should set this to emitter', function (done) { | ||
var mapped = emitter | ||
.flatMap(function () { | ||
var self = this; | ||
return EEX.startAsync(function (eex) { | ||
eex.emit('end', self); | ||
}); | ||
}) | ||
.on('end', function (result) { | ||
result.should.be.equal(mapped); | ||
done(); | ||
}) | ||
.on('error', done); | ||
emitter.emit('end'); | ||
}); | ||
it('should throw exception on non-function arguments', function () { | ||
@@ -267,0 +314,0 @@ expect(function () { |
34311
11
453