buffer.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /** PURE_IMPORTS_START .._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { OuterSubscriber } from '../OuterSubscriber';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. /**
  12. * Buffers the source Observable values until `closingNotifier` emits.
  13. *
  14. * <span class="informal">Collects values from the past as an array, and emits
  15. * that array only when another Observable emits.</span>
  16. *
  17. * <img src="./img/buffer.png" width="100%">
  18. *
  19. * Buffers the incoming Observable values until the given `closingNotifier`
  20. * Observable emits a value, at which point it emits the buffer on the output
  21. * Observable and starts a new buffer internally, awaiting the next time
  22. * `closingNotifier` emits.
  23. *
  24. * @example <caption>On every click, emit array of most recent interval events</caption>
  25. * var clicks = Rx.Observable.fromEvent(document, 'click');
  26. * var interval = Rx.Observable.interval(1000);
  27. * var buffered = interval.buffer(clicks);
  28. * buffered.subscribe(x => console.log(x));
  29. *
  30. * @see {@link bufferCount}
  31. * @see {@link bufferTime}
  32. * @see {@link bufferToggle}
  33. * @see {@link bufferWhen}
  34. * @see {@link window}
  35. *
  36. * @param {Observable<any>} closingNotifier An Observable that signals the
  37. * buffer to be emitted on the output Observable.
  38. * @return {Observable<T[]>} An Observable of buffers, which are arrays of
  39. * values.
  40. * @method buffer
  41. * @owner Observable
  42. */
  43. export function buffer(closingNotifier) {
  44. return function bufferOperatorFunction(source) {
  45. return source.lift(new BufferOperator(closingNotifier));
  46. };
  47. }
  48. var BufferOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  49. function BufferOperator(closingNotifier) {
  50. this.closingNotifier = closingNotifier;
  51. }
  52. BufferOperator.prototype.call = function (subscriber, source) {
  53. return source.subscribe(new BufferSubscriber(subscriber, this.closingNotifier));
  54. };
  55. return BufferOperator;
  56. }());
  57. /**
  58. * We need this JSDoc comment for affecting ESDoc.
  59. * @ignore
  60. * @extends {Ignored}
  61. */
  62. var BufferSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  63. __extends(BufferSubscriber, _super);
  64. function BufferSubscriber(destination, closingNotifier) {
  65. _super.call(this, destination);
  66. this.buffer = [];
  67. this.add(subscribeToResult(this, closingNotifier));
  68. }
  69. BufferSubscriber.prototype._next = function (value) {
  70. this.buffer.push(value);
  71. };
  72. BufferSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  73. var buffer = this.buffer;
  74. this.buffer = [];
  75. this.destination.next(buffer);
  76. };
  77. return BufferSubscriber;
  78. }(OuterSubscriber));
  79. //# sourceMappingURL=buffer.js.map