Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bufferWhen.ts 4.1KB

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