Skip to content

Instantly share code, notes, and snippets.

@Twipped
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twipped/d797b99378d026fc7a82 to your computer and use it in GitHub Desktop.
Save Twipped/d797b99378d026fc7a82 to your computer and use it in GitHub Desktop.
var Emitter = require('events').EventEmitter;
module.exports = function (target) {
if (!target) {target = new Emitter();}
if (target.proxyTo) {return target;}
var oldEmit = target.emit;
target.preProxies = [];
target.postProxies = [];
target.emit = function (name) {
var i;
i = this.preProxies.length;
while (i-- > 0) {
this.preProxies[i].fn.apply(this, arguments);
}
var args = [].slice.call(arguments);
args.unshift('all');
oldEmit.apply(this, args);
oldEmit.apply(this, arguments);
args[0] = 'all.post';
oldEmit.apply(this, args);
i = this.postProxies.length;
while (i-- > 0) {
this.postProxies[i].fn.apply(this, arguments);
}
};
target.proxyTo = function (destination, options) {
options = options || {};
options = {
before: options.before || false,
prefix: options.prefix || '',
suffix: options.suggix || ''
};
(options.before ? this.preProxies : this.postProxies).unshift({
destination: destination,
fn: function () {
var args = [].slice.call(arguments);
args[0] = options.prefix + args[0] + options.suffix;
destination.emit.apply(destination, args);
}
});
};
return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment