generate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var Observable_1 = require("../Observable");
  4. var identity_1 = require("../util/identity");
  5. var isScheduler_1 = require("../util/isScheduler");
  6. function generate(initialStateOrOptions, condition, iterate, resultSelectorOrObservable, scheduler) {
  7. var resultSelector;
  8. var initialState;
  9. if (arguments.length == 1) {
  10. var options = initialStateOrOptions;
  11. initialState = options.initialState;
  12. condition = options.condition;
  13. iterate = options.iterate;
  14. resultSelector = options.resultSelector || identity_1.identity;
  15. scheduler = options.scheduler;
  16. }
  17. else if (resultSelectorOrObservable === undefined || isScheduler_1.isScheduler(resultSelectorOrObservable)) {
  18. initialState = initialStateOrOptions;
  19. resultSelector = identity_1.identity;
  20. scheduler = resultSelectorOrObservable;
  21. }
  22. else {
  23. initialState = initialStateOrOptions;
  24. resultSelector = resultSelectorOrObservable;
  25. }
  26. return new Observable_1.Observable(function (subscriber) {
  27. var state = initialState;
  28. if (scheduler) {
  29. return scheduler.schedule(dispatch, 0, {
  30. subscriber: subscriber,
  31. iterate: iterate,
  32. condition: condition,
  33. resultSelector: resultSelector,
  34. state: state
  35. });
  36. }
  37. do {
  38. if (condition) {
  39. var conditionResult = void 0;
  40. try {
  41. conditionResult = condition(state);
  42. }
  43. catch (err) {
  44. subscriber.error(err);
  45. return undefined;
  46. }
  47. if (!conditionResult) {
  48. subscriber.complete();
  49. break;
  50. }
  51. }
  52. var value = void 0;
  53. try {
  54. value = resultSelector(state);
  55. }
  56. catch (err) {
  57. subscriber.error(err);
  58. return undefined;
  59. }
  60. subscriber.next(value);
  61. if (subscriber.closed) {
  62. break;
  63. }
  64. try {
  65. state = iterate(state);
  66. }
  67. catch (err) {
  68. subscriber.error(err);
  69. return undefined;
  70. }
  71. } while (true);
  72. return undefined;
  73. });
  74. }
  75. exports.generate = generate;
  76. function dispatch(state) {
  77. var subscriber = state.subscriber, condition = state.condition;
  78. if (subscriber.closed) {
  79. return undefined;
  80. }
  81. if (state.needIterate) {
  82. try {
  83. state.state = state.iterate(state.state);
  84. }
  85. catch (err) {
  86. subscriber.error(err);
  87. return undefined;
  88. }
  89. }
  90. else {
  91. state.needIterate = true;
  92. }
  93. if (condition) {
  94. var conditionResult = void 0;
  95. try {
  96. conditionResult = condition(state.state);
  97. }
  98. catch (err) {
  99. subscriber.error(err);
  100. return undefined;
  101. }
  102. if (!conditionResult) {
  103. subscriber.complete();
  104. return undefined;
  105. }
  106. if (subscriber.closed) {
  107. return undefined;
  108. }
  109. }
  110. var value;
  111. try {
  112. value = state.resultSelector(state.state);
  113. }
  114. catch (err) {
  115. subscriber.error(err);
  116. return undefined;
  117. }
  118. if (subscriber.closed) {
  119. return undefined;
  120. }
  121. subscriber.next(value);
  122. if (subscriber.closed) {
  123. return undefined;
  124. }
  125. return this.schedule(state);
  126. }
  127. //# sourceMappingURL=generate.js.map