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.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Subscription } from '../Subscription';
  2. import { OuterSubscriber } from '../OuterSubscriber';
  3. import { subscribeToResult } from '../util/subscribeToResult';
  4. export function bufferWhen(closingSelector) {
  5. return function (source) {
  6. return source.lift(new BufferWhenOperator(closingSelector));
  7. };
  8. }
  9. class BufferWhenOperator {
  10. constructor(closingSelector) {
  11. this.closingSelector = closingSelector;
  12. }
  13. call(subscriber, source) {
  14. return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
  15. }
  16. }
  17. class BufferWhenSubscriber extends OuterSubscriber {
  18. constructor(destination, closingSelector) {
  19. super(destination);
  20. this.closingSelector = closingSelector;
  21. this.subscribing = false;
  22. this.openBuffer();
  23. }
  24. _next(value) {
  25. this.buffer.push(value);
  26. }
  27. _complete() {
  28. const buffer = this.buffer;
  29. if (buffer) {
  30. this.destination.next(buffer);
  31. }
  32. super._complete();
  33. }
  34. _unsubscribe() {
  35. this.buffer = null;
  36. this.subscribing = false;
  37. }
  38. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  39. this.openBuffer();
  40. }
  41. notifyComplete() {
  42. if (this.subscribing) {
  43. this.complete();
  44. }
  45. else {
  46. this.openBuffer();
  47. }
  48. }
  49. openBuffer() {
  50. let { closingSubscription } = this;
  51. if (closingSubscription) {
  52. this.remove(closingSubscription);
  53. closingSubscription.unsubscribe();
  54. }
  55. const buffer = this.buffer;
  56. if (this.buffer) {
  57. this.destination.next(buffer);
  58. }
  59. this.buffer = [];
  60. let closingNotifier;
  61. try {
  62. const { closingSelector } = this;
  63. closingNotifier = closingSelector();
  64. }
  65. catch (err) {
  66. return this.error(err);
  67. }
  68. closingSubscription = new Subscription();
  69. this.closingSubscription = closingSubscription;
  70. this.add(closingSubscription);
  71. this.subscribing = true;
  72. closingSubscription.add(subscribeToResult(this, closingNotifier));
  73. this.subscribing = false;
  74. }
  75. }
  76. //# sourceMappingURL=bufferWhen.js.map