buffer.js 2.9 KB

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