You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventEmitterForwarder.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Implements utility to forward events from one eventEmitter to another.
  3. * @param src {object} instance of EventEmitter or another class that implements
  4. * addListener method which will register listener to EventEmitter instance.
  5. * @param dest {object} instance of EventEmitter or another class that
  6. * implements emit method which will emit an event.
  7. */
  8. function EventEmitterForwarder (src, dest) {
  9. if (!src || !dest || typeof src.addListener !== "function" ||
  10. typeof dest.emit !== "function") {
  11. throw new Error("Invalid arguments passed to EventEmitterForwarder");
  12. }
  13. this.src = src;
  14. this.dest = dest;
  15. }
  16. /**
  17. * Adds event to be forwarded from src to dest.
  18. * @param srcEvent {string} the event that EventEmitterForwarder is listening
  19. * for.
  20. * @param dstEvent {string} the event that will be fired from dest.
  21. * @param arguments all other passed arguments are going to be fired with
  22. * dstEvent.
  23. */
  24. EventEmitterForwarder.prototype.forward = function () {
  25. // This line is only for fixing jshint errors.
  26. var args = arguments;
  27. var srcEvent = args[0];
  28. //This will be the "this" value for emit function.
  29. args[0] = this.dest;
  30. //Using bind.apply to pass the arguments as Array-like object ("arguments")
  31. this.src.addListener(srcEvent,
  32. Function.prototype.bind.apply(this.dest.emit, args));
  33. };
  34. module.exports = EventEmitterForwarder;