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.

expand.js 3.1KB

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