stream.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file stream.js
  3. */
  4. /**
  5. * A lightweight readable stream implemention that handles event dispatching.
  6. *
  7. * @class Stream
  8. */
  9. 'use strict';
  10. Object.defineProperty(exports, '__esModule', {
  11. value: true
  12. });
  13. 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; }; })();
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  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. var index = undefined;
  47. if (!this.listeners[type]) {
  48. return false;
  49. }
  50. index = this.listeners[type].indexOf(listener);
  51. this.listeners[type].splice(index, 1);
  52. return index > -1;
  53. }
  54. /**
  55. * Trigger an event of the specified type on this stream. Any additional
  56. * arguments to this function are passed as parameters to event listeners.
  57. *
  58. * @param {String} type the event name
  59. */
  60. }, {
  61. key: 'trigger',
  62. value: function trigger(type) {
  63. var callbacks = undefined;
  64. var i = undefined;
  65. var length = undefined;
  66. var args = undefined;
  67. callbacks = this.listeners[type];
  68. if (!callbacks) {
  69. return;
  70. }
  71. // Slicing the arguments on every invocation of this method
  72. // can add a significant amount of overhead. Avoid the
  73. // intermediate object creation for the common case of a
  74. // single callback argument
  75. if (arguments.length === 2) {
  76. length = callbacks.length;
  77. for (i = 0; i < length; ++i) {
  78. callbacks[i].call(this, arguments[1]);
  79. }
  80. } else {
  81. args = Array.prototype.slice.call(arguments, 1);
  82. length = callbacks.length;
  83. for (i = 0; i < length; ++i) {
  84. callbacks[i].apply(this, args);
  85. }
  86. }
  87. }
  88. /**
  89. * Destroys the stream and cleans up.
  90. */
  91. }, {
  92. key: 'dispose',
  93. value: function dispose() {
  94. this.listeners = {};
  95. }
  96. /**
  97. * Forwards all `data` events on this stream to the destination stream. The
  98. * destination stream should provide a method `push` to receive the data
  99. * events as they arrive.
  100. *
  101. * @param {Stream} destination the stream that will receive all `data` events
  102. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  103. */
  104. }, {
  105. key: 'pipe',
  106. value: function pipe(destination) {
  107. this.on('data', function (data) {
  108. destination.push(data);
  109. });
  110. }
  111. }]);
  112. return Stream;
  113. })();
  114. exports['default'] = Stream;
  115. module.exports = exports['default'];