stream.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. /**
  8. * @file stream.js
  9. */
  10. /**
  11. * A lightweight readable stream implemention that handles event dispatching.
  12. *
  13. * @class Stream
  14. */
  15. var Stream = function () {
  16. function Stream() {
  17. _classCallCheck(this, Stream);
  18. this.listeners = {};
  19. }
  20. /**
  21. * Add a listener for a specified event type.
  22. *
  23. * @param {String} type the event name
  24. * @param {Function} listener the callback to be invoked when an event of
  25. * the specified type occurs
  26. */
  27. _createClass(Stream, [{
  28. key: 'on',
  29. value: function on(type, listener) {
  30. if (!this.listeners[type]) {
  31. this.listeners[type] = [];
  32. }
  33. this.listeners[type].push(listener);
  34. }
  35. /**
  36. * Remove a listener for a specified event type.
  37. *
  38. * @param {String} type the event name
  39. * @param {Function} listener a function previously registered for this
  40. * type of event through `on`
  41. * @return {Boolean} if we could turn it off or not
  42. */
  43. }, {
  44. key: 'off',
  45. value: function off(type, listener) {
  46. if (!this.listeners[type]) {
  47. return false;
  48. }
  49. var index = this.listeners[type].indexOf(listener);
  50. this.listeners[type].splice(index, 1);
  51. return index > -1;
  52. }
  53. /**
  54. * Trigger an event of the specified type on this stream. Any additional
  55. * arguments to this function are passed as parameters to event listeners.
  56. *
  57. * @param {String} type the event name
  58. */
  59. }, {
  60. key: 'trigger',
  61. value: function trigger(type) {
  62. var callbacks = this.listeners[type];
  63. var i = void 0;
  64. var length = void 0;
  65. var args = void 0;
  66. if (!callbacks) {
  67. return;
  68. }
  69. // Slicing the arguments on every invocation of this method
  70. // can add a significant amount of overhead. Avoid the
  71. // intermediate object creation for the common case of a
  72. // single callback argument
  73. if (arguments.length === 2) {
  74. length = callbacks.length;
  75. for (i = 0; i < length; ++i) {
  76. callbacks[i].call(this, arguments[1]);
  77. }
  78. } else {
  79. args = Array.prototype.slice.call(arguments, 1);
  80. length = callbacks.length;
  81. for (i = 0; i < length; ++i) {
  82. callbacks[i].apply(this, args);
  83. }
  84. }
  85. }
  86. /**
  87. * Destroys the stream and cleans up.
  88. */
  89. }, {
  90. key: 'dispose',
  91. value: function dispose() {
  92. this.listeners = {};
  93. }
  94. /**
  95. * Forwards all `data` events on this stream to the destination stream. The
  96. * destination stream should provide a method `push` to receive the data
  97. * events as they arrive.
  98. *
  99. * @param {Stream} destination the stream that will receive all `data` events
  100. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  101. */
  102. }, {
  103. key: 'pipe',
  104. value: function pipe(destination) {
  105. this.on('data', function (data) {
  106. destination.push(data);
  107. });
  108. }
  109. }]);
  110. return Stream;
  111. }();
  112. exports['default'] = Stream;