GenerateObservable.js 4.7KB

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