onErrorResumeNext.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /** PURE_IMPORTS_START .._observable_FromObservable,.._util_isArray,.._OuterSubscriber,.._util_subscribeToResult 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 { FromObservable } from '../observable/FromObservable';
  10. import { isArray } from '../util/isArray';
  11. import { OuterSubscriber } from '../OuterSubscriber';
  12. import { subscribeToResult } from '../util/subscribeToResult';
  13. /* tslint:enable:max-line-length */
  14. /**
  15. * When any of the provided Observable emits an complete or error notification, it immediately subscribes to the next one
  16. * that was passed.
  17. *
  18. * <span class="informal">Execute series of Observables no matter what, even if it means swallowing errors.</span>
  19. *
  20. * <img src="./img/onErrorResumeNext.png" width="100%">
  21. *
  22. * `onErrorResumeNext` is an operator that accepts a series of Observables, provided either directly as
  23. * arguments or as an array. If no single Observable is provided, returned Observable will simply behave the same
  24. * as the source.
  25. *
  26. * `onErrorResumeNext` returns an Observable that starts by subscribing and re-emitting values from the source Observable.
  27. * When its stream of values ends - no matter if Observable completed or emitted an error - `onErrorResumeNext`
  28. * will subscribe to the first Observable that was passed as an argument to the method. It will start re-emitting
  29. * its values as well and - again - when that stream ends, `onErrorResumeNext` will proceed to subscribing yet another
  30. * Observable in provided series, no matter if previous Observable completed or ended with an error. This will
  31. * be happening until there is no more Observables left in the series, at which point returned Observable will
  32. * complete - even if the last subscribed stream ended with an error.
  33. *
  34. * `onErrorResumeNext` can be therefore thought of as version of {@link concat} operator, which is more permissive
  35. * when it comes to the errors emitted by its input Observables. While `concat` subscribes to the next Observable
  36. * in series only if previous one successfully completed, `onErrorResumeNext` subscribes even if it ended with
  37. * an error.
  38. *
  39. * Note that you do not get any access to errors emitted by the Observables. In particular do not
  40. * expect these errors to appear in error callback passed to {@link subscribe}. If you want to take
  41. * specific actions based on what error was emitted by an Observable, you should try out {@link catch} instead.
  42. *
  43. *
  44. * @example <caption>Subscribe to the next Observable after map fails</caption>
  45. * Rx.Observable.of(1, 2, 3, 0)
  46. * .map(x => {
  47. * if (x === 0) { throw Error(); }
  48. return 10 / x;
  49. * })
  50. * .onErrorResumeNext(Rx.Observable.of(1, 2, 3))
  51. * .subscribe(
  52. * val => console.log(val),
  53. * err => console.log(err), // Will never be called.
  54. * () => console.log('that\'s it!')
  55. * );
  56. *
  57. * // Logs:
  58. * // 10
  59. * // 5
  60. * // 3.3333333333333335
  61. * // 1
  62. * // 2
  63. * // 3
  64. * // "that's it!"
  65. *
  66. * @see {@link concat}
  67. * @see {@link catch}
  68. *
  69. * @param {...ObservableInput} observables Observables passed either directly or as an array.
  70. * @return {Observable} An Observable that emits values from source Observable, but - if it errors - subscribes
  71. * to the next passed Observable and so on, until it completes or runs out of Observables.
  72. * @method onErrorResumeNext
  73. * @owner Observable
  74. */
  75. export function onErrorResumeNext() {
  76. var nextSources = [];
  77. for (var _i = 0; _i < arguments.length; _i++) {
  78. nextSources[_i - 0] = arguments[_i];
  79. }
  80. if (nextSources.length === 1 && isArray(nextSources[0])) {
  81. nextSources = nextSources[0];
  82. }
  83. return function (source) { return source.lift(new OnErrorResumeNextOperator(nextSources)); };
  84. }
  85. /* tslint:enable:max-line-length */
  86. export function onErrorResumeNextStatic() {
  87. var nextSources = [];
  88. for (var _i = 0; _i < arguments.length; _i++) {
  89. nextSources[_i - 0] = arguments[_i];
  90. }
  91. var source = null;
  92. if (nextSources.length === 1 && isArray(nextSources[0])) {
  93. nextSources = nextSources[0];
  94. }
  95. source = nextSources.shift();
  96. return new FromObservable(source, null).lift(new OnErrorResumeNextOperator(nextSources));
  97. }
  98. var OnErrorResumeNextOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  99. function OnErrorResumeNextOperator(nextSources) {
  100. this.nextSources = nextSources;
  101. }
  102. OnErrorResumeNextOperator.prototype.call = function (subscriber, source) {
  103. return source.subscribe(new OnErrorResumeNextSubscriber(subscriber, this.nextSources));
  104. };
  105. return OnErrorResumeNextOperator;
  106. }());
  107. var OnErrorResumeNextSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  108. __extends(OnErrorResumeNextSubscriber, _super);
  109. function OnErrorResumeNextSubscriber(destination, nextSources) {
  110. _super.call(this, destination);
  111. this.destination = destination;
  112. this.nextSources = nextSources;
  113. }
  114. OnErrorResumeNextSubscriber.prototype.notifyError = function (error, innerSub) {
  115. this.subscribeToNextSource();
  116. };
  117. OnErrorResumeNextSubscriber.prototype.notifyComplete = function (innerSub) {
  118. this.subscribeToNextSource();
  119. };
  120. OnErrorResumeNextSubscriber.prototype._error = function (err) {
  121. this.subscribeToNextSource();
  122. };
  123. OnErrorResumeNextSubscriber.prototype._complete = function () {
  124. this.subscribeToNextSource();
  125. };
  126. OnErrorResumeNextSubscriber.prototype.subscribeToNextSource = function () {
  127. var next = this.nextSources.shift();
  128. if (next) {
  129. this.add(subscribeToResult(this, next));
  130. }
  131. else {
  132. this.destination.complete();
  133. }
  134. };
  135. return OnErrorResumeNextSubscriber;
  136. }(OuterSubscriber));
  137. //# sourceMappingURL=onErrorResumeNext.js.map