Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 8, 2013 12:21
Show Gist options
  • Save cowboy/6184130 to your computer and use it in GitHub Desktop.
Save cowboy/6184130 to your computer and use it in GitHub Desktop.
JS: EventEmitter2 "event passthrough" ugliness.
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var a = new EventEmitter2({wildcard: true});
a.onAny(function(foo, bar) {
console.log(this.event, foo, bar);
});
a.emit('a', 1, 2);
// logs a 1 2
// Is there a more elegant way of forwarding all events from one
// EventEmitter2 to another than using .apply and arguments like this?
var b = new EventEmitter2({wildcard: true});
// b -> a
b.onAny(function() {
a.emit.apply(a, [this.event].concat([].slice.call(arguments)));
});
b.emit('b', 3, 4);
// logs b 3 4
var c = new EventEmitter2({wildcard: true});
// c -> b -> a
c.onAny(function() {
b.emit.apply(b, [this.event].concat([].slice.call(arguments)));
});
c.emit('c', 5, 6);
// logs c 5 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment