Comparing version 0.3.1 to 0.4.0
28
index.js
@@ -8,6 +8,7 @@ function propagate(events, source, dest) { | ||
// events should be an array | ||
if (events && ! Array.isArray(events)) events = [events]; | ||
// events should be an array or object | ||
var eventsIsObject = typeof events === 'object' | ||
if (events && !eventsIsObject) events = [events]; | ||
if (Array.isArray(events)) { | ||
if (eventsIsObject) { | ||
return explicitPropagate(events, source, dest); | ||
@@ -37,6 +38,17 @@ } | ||
function explicitPropagate(events, source, dest) { | ||
var eventsIn; | ||
var eventsOut; | ||
if (Array.isArray(events)) { | ||
eventsIn = events; | ||
eventsOut = events; | ||
} else { | ||
eventsIn = Object.keys(events); | ||
eventsOut = eventsIn.map(function (key) { | ||
return events[key] | ||
}) | ||
} | ||
var listeners = events.map(function(event) { | ||
var listeners = eventsOut.map(function(event) { | ||
return function() { | ||
var args = Array.prototype.slice(arguments); | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(event); | ||
@@ -54,7 +66,7 @@ dest.emit.apply(dest, args); | ||
function register(listener, i) { | ||
source.on(events[i], listener); | ||
source.on(eventsIn[i], listener); | ||
} | ||
function unregister(listener, i) { | ||
source.removeListener(events[i], listener); | ||
source.removeListener(eventsIn[i], listener); | ||
} | ||
@@ -65,2 +77,2 @@ | ||
} | ||
} | ||
} |
{ | ||
"name": "propagate", | ||
"description": "Propagate events", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"author" : "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -26,2 +26,2 @@ "contributors" : [ | ||
"license": "MIT" | ||
} | ||
} |
@@ -47,4 +47,15 @@ # propagate | ||
## Propagate certain events as other events: | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var p = propagate({ | ||
'event1': 'other-event1', | ||
'event2': 'other-event2' | ||
}, ee1, ee2); | ||
``` | ||
# License | ||
MIT |
@@ -88,2 +88,33 @@ var test = require('tap').test; | ||
ee1.emit('event-1'); | ||
}); | ||
test('is able to propagate and map certain events', function(t) { | ||
t.plan(2); | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
// propagate only event-1 and event-2, leaving out | ||
var p = propagate({ | ||
'event-1': 'other-event-1', | ||
'event-2': 'other-event-2' | ||
}, ee1, ee2); | ||
ee2.on('other-event-1', function() { | ||
t.ok(true, 'event 1 received'); | ||
}); | ||
ee2.on('other-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'); | ||
p.end(); | ||
ee1.emit('event-1'); | ||
}); |
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
6708
6
151
60