GenerateObservable.js 4.6 KB

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