GenerateObservable.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Observable } from '../Observable';
  2. import { isScheduler } from '../util/isScheduler';
  3. const selfSelector = (value) => value;
  4. /**
  5. * We need this JSDoc comment for affecting ESDoc.
  6. * @extends {Ignored}
  7. * @hide true
  8. */
  9. export class GenerateObservable extends Observable {
  10. constructor(initialState, condition, iterate, resultSelector, scheduler) {
  11. super();
  12. this.initialState = initialState;
  13. this.condition = condition;
  14. this.iterate = iterate;
  15. this.resultSelector = resultSelector;
  16. this.scheduler = scheduler;
  17. }
  18. static create(initialStateOrOptions, condition, iterate, resultSelectorOrObservable, scheduler) {
  19. if (arguments.length == 1) {
  20. return new GenerateObservable(initialStateOrOptions.initialState, initialStateOrOptions.condition, initialStateOrOptions.iterate, initialStateOrOptions.resultSelector || selfSelector, initialStateOrOptions.scheduler);
  21. }
  22. if (resultSelectorOrObservable === undefined || isScheduler(resultSelectorOrObservable)) {
  23. return new GenerateObservable(initialStateOrOptions, condition, iterate, selfSelector, resultSelectorOrObservable);
  24. }
  25. return new GenerateObservable(initialStateOrOptions, condition, iterate, resultSelectorOrObservable, scheduler);
  26. }
  27. /** @deprecated internal use only */ _subscribe(subscriber) {
  28. let state = this.initialState;
  29. if (this.scheduler) {
  30. return this.scheduler.schedule(GenerateObservable.dispatch, 0, {
  31. subscriber,
  32. iterate: this.iterate,
  33. condition: this.condition,
  34. resultSelector: this.resultSelector,
  35. state });
  36. }
  37. const { condition, resultSelector, iterate } = this;
  38. do {
  39. if (condition) {
  40. let conditionResult;
  41. try {
  42. conditionResult = condition(state);
  43. }
  44. catch (err) {
  45. subscriber.error(err);
  46. return;
  47. }
  48. if (!conditionResult) {
  49. subscriber.complete();
  50. break;
  51. }
  52. }
  53. let value;
  54. try {
  55. value = resultSelector(state);
  56. }
  57. catch (err) {
  58. subscriber.error(err);
  59. return;
  60. }
  61. subscriber.next(value);
  62. if (subscriber.closed) {
  63. break;
  64. }
  65. try {
  66. state = iterate(state);
  67. }
  68. catch (err) {
  69. subscriber.error(err);
  70. return;
  71. }
  72. } while (true);
  73. }
  74. static dispatch(state) {
  75. const { subscriber, condition } = state;
  76. if (subscriber.closed) {
  77. return;
  78. }
  79. if (state.needIterate) {
  80. try {
  81. state.state = state.iterate(state.state);
  82. }
  83. catch (err) {
  84. subscriber.error(err);
  85. return;
  86. }
  87. }
  88. else {
  89. state.needIterate = true;
  90. }
  91. if (condition) {
  92. let conditionResult;
  93. try {
  94. conditionResult = condition(state.state);
  95. }
  96. catch (err) {
  97. subscriber.error(err);
  98. return;
  99. }
  100. if (!conditionResult) {
  101. subscriber.complete();
  102. return;
  103. }
  104. if (subscriber.closed) {
  105. return;
  106. }
  107. }
  108. let value;
  109. try {
  110. value = state.resultSelector(state.state);
  111. }
  112. catch (err) {
  113. subscriber.error(err);
  114. return;
  115. }
  116. if (subscriber.closed) {
  117. return;
  118. }
  119. subscriber.next(value);
  120. if (subscriber.closed) {
  121. return;
  122. }
  123. return this.schedule(state);
  124. }
  125. }
  126. //# sourceMappingURL=GenerateObservable.js.map