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.

mergeMap.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { subscribeToResult } from '../util/subscribeToResult';
  2. import { OuterSubscriber } from '../OuterSubscriber';
  3. import { InnerSubscriber } from '../InnerSubscriber';
  4. import { map } from './map';
  5. import { from } from '../observable/from';
  6. export function mergeMap(project, resultSelector, concurrent = Number.POSITIVE_INFINITY) {
  7. if (typeof resultSelector === 'function') {
  8. return (source) => source.pipe(mergeMap((a, i) => from(project(a, i)).pipe(map((b, ii) => resultSelector(a, b, i, ii))), concurrent));
  9. }
  10. else if (typeof resultSelector === 'number') {
  11. concurrent = resultSelector;
  12. }
  13. return (source) => source.lift(new MergeMapOperator(project, concurrent));
  14. }
  15. export class MergeMapOperator {
  16. constructor(project, concurrent = Number.POSITIVE_INFINITY) {
  17. this.project = project;
  18. this.concurrent = concurrent;
  19. }
  20. call(observer, source) {
  21. return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
  22. }
  23. }
  24. export class MergeMapSubscriber extends OuterSubscriber {
  25. constructor(destination, project, concurrent = Number.POSITIVE_INFINITY) {
  26. super(destination);
  27. this.project = project;
  28. this.concurrent = concurrent;
  29. this.hasCompleted = false;
  30. this.buffer = [];
  31. this.active = 0;
  32. this.index = 0;
  33. }
  34. _next(value) {
  35. if (this.active < this.concurrent) {
  36. this._tryNext(value);
  37. }
  38. else {
  39. this.buffer.push(value);
  40. }
  41. }
  42. _tryNext(value) {
  43. let result;
  44. const index = this.index++;
  45. try {
  46. result = this.project(value, index);
  47. }
  48. catch (err) {
  49. this.destination.error(err);
  50. return;
  51. }
  52. this.active++;
  53. this._innerSub(result, value, index);
  54. }
  55. _innerSub(ish, value, index) {
  56. const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
  57. const destination = this.destination;
  58. destination.add(innerSubscriber);
  59. subscribeToResult(this, ish, value, index, innerSubscriber);
  60. }
  61. _complete() {
  62. this.hasCompleted = true;
  63. if (this.active === 0 && this.buffer.length === 0) {
  64. this.destination.complete();
  65. }
  66. this.unsubscribe();
  67. }
  68. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  69. this.destination.next(innerValue);
  70. }
  71. notifyComplete(innerSub) {
  72. const buffer = this.buffer;
  73. this.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. this.destination.complete();
  80. }
  81. }
  82. }
  83. //# sourceMappingURL=mergeMap.js.map