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.

mergeScan.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { subscribeToResult } from '../util/subscribeToResult';
  2. import { OuterSubscriber } from '../OuterSubscriber';
  3. import { InnerSubscriber } from '../InnerSubscriber';
  4. export function mergeScan(accumulator, seed, concurrent = Number.POSITIVE_INFINITY) {
  5. return (source) => source.lift(new MergeScanOperator(accumulator, seed, concurrent));
  6. }
  7. export class MergeScanOperator {
  8. constructor(accumulator, seed, concurrent) {
  9. this.accumulator = accumulator;
  10. this.seed = seed;
  11. this.concurrent = concurrent;
  12. }
  13. call(subscriber, source) {
  14. return source.subscribe(new MergeScanSubscriber(subscriber, this.accumulator, this.seed, this.concurrent));
  15. }
  16. }
  17. export class MergeScanSubscriber extends OuterSubscriber {
  18. constructor(destination, accumulator, acc, concurrent) {
  19. super(destination);
  20. this.accumulator = accumulator;
  21. this.acc = acc;
  22. this.concurrent = concurrent;
  23. this.hasValue = false;
  24. this.hasCompleted = false;
  25. this.buffer = [];
  26. this.active = 0;
  27. this.index = 0;
  28. }
  29. _next(value) {
  30. if (this.active < this.concurrent) {
  31. const index = this.index++;
  32. const destination = this.destination;
  33. let ish;
  34. try {
  35. const { accumulator } = this;
  36. ish = accumulator(this.acc, value, index);
  37. }
  38. catch (e) {
  39. return destination.error(e);
  40. }
  41. this.active++;
  42. this._innerSub(ish, value, index);
  43. }
  44. else {
  45. this.buffer.push(value);
  46. }
  47. }
  48. _innerSub(ish, value, index) {
  49. const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
  50. const destination = this.destination;
  51. destination.add(innerSubscriber);
  52. subscribeToResult(this, ish, value, index, innerSubscriber);
  53. }
  54. _complete() {
  55. this.hasCompleted = true;
  56. if (this.active === 0 && this.buffer.length === 0) {
  57. if (this.hasValue === false) {
  58. this.destination.next(this.acc);
  59. }
  60. this.destination.complete();
  61. }
  62. this.unsubscribe();
  63. }
  64. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  65. const { destination } = this;
  66. this.acc = innerValue;
  67. this.hasValue = true;
  68. destination.next(innerValue);
  69. }
  70. notifyComplete(innerSub) {
  71. const buffer = this.buffer;
  72. const destination = this.destination;
  73. destination.remove(innerSub);
  74. this.active--;
  75. if (buffer.length > 0) {
  76. this._next(buffer.shift());
  77. }
  78. else if (this.active === 0 && this.hasCompleted) {
  79. if (this.hasValue === false) {
  80. this.destination.next(this.acc);
  81. }
  82. this.destination.complete();
  83. }
  84. }
  85. }
  86. //# sourceMappingURL=mergeScan.js.map