Comparing version 0.1.1 to 0.2.0
17
index.js
@@ -1,6 +0,17 @@ | ||
function propagate(source, dest) { | ||
function propagate(events, source, dest) { | ||
if (arguments.length < 3) { | ||
dest = source; | ||
source = events; | ||
events = undefined; | ||
} | ||
// events should be an array | ||
if (events && ! Array.isArray(events)) events = [events]; | ||
var oldEmit = source.emit; | ||
source.emit = function() { | ||
dest.emit.apply(dest, arguments); | ||
source.emit = function(eventType) { | ||
if (! events || ~events.indexOf(eventType)) { | ||
dest.emit.apply(dest, arguments); | ||
} | ||
oldEmit.apply(source, arguments); | ||
@@ -7,0 +18,0 @@ } |
{ | ||
"name": "propagate", | ||
"description": "Propagate events", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author" : "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors" : [ |
@@ -11,3 +11,3 @@ # propagate | ||
## User | ||
## Propagate | ||
@@ -39,1 +39,9 @@ ```javascript | ||
``` | ||
## Just propagate some events: | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var p = propagate(['event1', event2'], ee1, ee2); | ||
``` |
@@ -60,2 +60,26 @@ var test = require('tap').test; | ||
ee1.emit('event'); | ||
}); | ||
test('is able to propagate only certain events', function(t) { | ||
t.plan(2); | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
// propagate only event-1 and event-2, leaving out | ||
propagate(['event-1', 'event-2'], ee1, ee2); | ||
ee2.on('event-1', function() { | ||
t.ok(true, 'event 1 received'); | ||
}); | ||
ee2.on('event-2', function(a, b, c) { | ||
t.ok(true, 'event 2 received'); | ||
}); | ||
ee2.on('event-3', function(a, b, c) { | ||
t.ok(false, 'event 3 should not have been received'); | ||
}); | ||
ee1.emit('event-1'); | ||
ee1.emit('event-2'); | ||
ee1.emit('event-3'); | ||
}); |
3708
87
46