stream.js 3.1 KB

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