2ede18e77bbfb4d4241929a0d6664b709a58eb9a7d9df9a67fa7f6de3000841f0a871894d7cd8354607e8c47f774dc95e5848f9775e409afc1738452ab7d97 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { Subscription } from '../Subscription';
  5. import { OperatorFunction } from '../types';
  6. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  7. /**
  8. * Buffers the source Observable values, using a factory function of closing
  9. * Observables to determine when to close, emit, and reset the buffer.
  10. *
  11. * <span class="informal">Collects values from the past as an array. When it
  12. * starts collecting values, it calls a function that returns an Observable that
  13. * tells when to close the buffer and restart collecting.</span>
  14. *
  15. * ![](bufferWhen.png)
  16. *
  17. * Opens a buffer immediately, then closes the buffer when the observable
  18. * returned by calling `closingSelector` function emits a value. When it closes
  19. * the buffer, it immediately opens a new buffer and repeats the process.
  20. *
  21. * ## Example
  22. *
  23. * Emit an array of the last clicks every [1-5] random seconds
  24. *
  25. * ```ts
  26. * import { fromEvent, interval } from 'rxjs';
  27. * import { bufferWhen } from 'rxjs/operators';
  28. *
  29. * const clicks = fromEvent(document, 'click');
  30. * const buffered = clicks.pipe(bufferWhen(() =>
  31. * interval(1000 + Math.random() * 4000)
  32. * ));
  33. * buffered.subscribe(x => console.log(x));
  34. * ```
  35. *
  36. *
  37. * @see {@link buffer}
  38. * @see {@link bufferCount}
  39. * @see {@link bufferTime}
  40. * @see {@link bufferToggle}
  41. * @see {@link windowWhen}
  42. *
  43. * @param {function(): Observable} closingSelector A function that takes no
  44. * arguments and returns an Observable that signals buffer closure.
  45. * @return {Observable<T[]>} An observable of arrays of buffered values.
  46. * @method bufferWhen
  47. * @owner Observable
  48. */
  49. export function bufferWhen<T>(closingSelector: () => Observable<any>): OperatorFunction<T, T[]> {
  50. return function (source: Observable<T>) {
  51. return source.lift(new BufferWhenOperator(closingSelector));
  52. };
  53. }
  54. class BufferWhenOperator<T> implements Operator<T, T[]> {
  55. constructor(private closingSelector: () => Observable<any>) {
  56. }
  57. call(subscriber: Subscriber<T[]>, source: any): any {
  58. return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
  59. }
  60. }
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. class BufferWhenSubscriber<T> extends SimpleOuterSubscriber<T, any> {
  67. private buffer?: T[];
  68. private subscribing: boolean = false;
  69. private closingSubscription?: Subscription;
  70. constructor(destination: Subscriber<T[]>, private closingSelector: () => Observable<any>) {
  71. super(destination);
  72. this.openBuffer();
  73. }
  74. protected _next(value: T) {
  75. this.buffer!.push(value);
  76. }
  77. protected _complete() {
  78. const buffer = this.buffer;
  79. if (buffer) {
  80. this.destination.next!(buffer);
  81. }
  82. super._complete();
  83. }
  84. /** @deprecated This is an internal implementation detail, do not use. */
  85. _unsubscribe() {
  86. this.buffer = undefined;
  87. this.subscribing = false;
  88. }
  89. notifyNext(): void {
  90. this.openBuffer();
  91. }
  92. notifyComplete(): void {
  93. if (this.subscribing) {
  94. this.complete();
  95. } else {
  96. this.openBuffer();
  97. }
  98. }
  99. openBuffer() {
  100. let { closingSubscription } = this;
  101. if (closingSubscription) {
  102. this.remove(closingSubscription);
  103. closingSubscription.unsubscribe();
  104. }
  105. const buffer = this.buffer;
  106. if (this.buffer) {
  107. this.destination.next!(buffer);
  108. }
  109. this.buffer = [];
  110. let closingNotifier;
  111. try {
  112. const { closingSelector } = this;
  113. closingNotifier = closingSelector();
  114. } catch (err) {
  115. return this.error(err);
  116. }
  117. closingSubscription = new Subscription();
  118. this.closingSubscription = closingSubscription;
  119. this.add(closingSubscription);
  120. this.subscribing = true;
  121. closingSubscription.add(innerSubscribe(closingNotifier, new SimpleInnerSubscriber(this)));
  122. this.subscribing = false;
  123. }
  124. }