stream.js 3.2 KB

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