pipe.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var aFrom = require('es5-ext/array/from')
  3. , remove = require('es5-ext/array/#/remove')
  4. , value = require('es5-ext/object/valid-object')
  5. , d = require('d')
  6. , emit = require('./').methods.emit
  7. , defineProperty = Object.defineProperty
  8. , hasOwnProperty = Object.prototype.hasOwnProperty
  9. , getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  10. module.exports = function (e1, e2/*, name*/) {
  11. var pipes, pipe, desc, name;
  12. (value(e1) && value(e2));
  13. name = arguments[2];
  14. if (name === undefined) name = 'emit';
  15. pipe = {
  16. close: function () { remove.call(pipes, e2); }
  17. };
  18. if (hasOwnProperty.call(e1, '__eePipes__')) {
  19. (pipes = e1.__eePipes__).push(e2);
  20. return pipe;
  21. }
  22. defineProperty(e1, '__eePipes__', d('c', pipes = [e2]));
  23. desc = getOwnPropertyDescriptor(e1, name);
  24. if (!desc) {
  25. desc = d('c', undefined);
  26. } else {
  27. delete desc.get;
  28. delete desc.set;
  29. }
  30. desc.value = function () {
  31. var i, emitter, data = aFrom(pipes);
  32. emit.apply(this, arguments);
  33. for (i = 0; (emitter = data[i]); ++i) emit.apply(emitter, arguments);
  34. };
  35. defineProperty(e1, name, desc);
  36. return pipe;
  37. };