generate.js 3.6 KB

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