onErrorResumeNext.js 5.9 KB

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