forward-emitter
Advanced tools
Comparing version 0.0.1 to 0.1.0
14
index.js
@@ -10,11 +10,15 @@ 'use strict'; | ||
// Listeners bound to the destination emitter should be bound to the source emitter. | ||
dest.on('newListener', function(eventName, listener) { | ||
if (filterFn(eventName)) src.on(eventName, listener); | ||
}); | ||
dest.on('newListener', newListener); | ||
function newListener(eventName, listener) { | ||
if (filterFn(eventName) && listener !== removeListener) src.on(eventName, listener); | ||
} | ||
// When a listener is removed from the destination emitter, remove it from the source emitter | ||
// (otherwise it will continue to be called). | ||
dest.on('removeListener', function(eventName, listener) { | ||
dest.on('removeListener', removeListener); | ||
function removeListener(eventName, listener) { | ||
src.removeListener(eventName, listener); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "forward-emitter", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Forward events from any Node EventEmitter to another EventEmitter.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,22 @@ 'use strict'; | ||
it('should forward in a chain', function() { | ||
var a = new EventEmitter(); | ||
var b = new EventEmitter(); | ||
var c = new EventEmitter(); | ||
forward(a, b); | ||
forward(b, c); | ||
var calls = 0; | ||
a.on('test', increment); | ||
b.on('test', increment); | ||
c.on('test', increment); | ||
function increment() { | ||
calls++; | ||
} | ||
a.emit('test', 'event payload'); | ||
calls.should.equal(3); | ||
}); | ||
it('should forward errors', function(done){ | ||
@@ -43,2 +63,14 @@ var src = new EventEmitter(); | ||
}); | ||
it('should not loop forever', function(done) { | ||
var src1 = new EventEmitter(); | ||
var src2 = new EventEmitter(); | ||
var src3 = new EventEmitter(); | ||
var dest = new EventEmitter(); | ||
forward(src1, dest); | ||
forward(src2, dest); | ||
forward(src3, dest); | ||
done(); | ||
}); | ||
}); | ||
@@ -45,0 +77,0 @@ |
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
5278
119