retryWhen.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** PURE_IMPORTS_START .._Subject,.._util_tryCatch,.._util_errorObject,.._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 { Subject } from '../Subject';
  10. import { tryCatch } from '../util/tryCatch';
  11. import { errorObject } from '../util/errorObject';
  12. import { OuterSubscriber } from '../OuterSubscriber';
  13. import { subscribeToResult } from '../util/subscribeToResult';
  14. /**
  15. * Returns an Observable that mirrors the source Observable with the exception of an `error`. If the source Observable
  16. * calls `error`, this method will emit the Throwable that caused the error to the Observable returned from `notifier`.
  17. * If that Observable calls `complete` or `error` then this method will call `complete` or `error` on the child
  18. * subscription. Otherwise this method will resubscribe to the source Observable.
  19. *
  20. * <img src="./img/retryWhen.png" width="100%">
  21. *
  22. * @param {function(errors: Observable): Observable} notifier - Receives an Observable of notifications with which a
  23. * user can `complete` or `error`, aborting the retry.
  24. * @return {Observable} The source Observable modified with retry logic.
  25. * @method retryWhen
  26. * @owner Observable
  27. */
  28. export function retryWhen(notifier) {
  29. return function (source) { return source.lift(new RetryWhenOperator(notifier, source)); };
  30. }
  31. var RetryWhenOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  32. function RetryWhenOperator(notifier, source) {
  33. this.notifier = notifier;
  34. this.source = source;
  35. }
  36. RetryWhenOperator.prototype.call = function (subscriber, source) {
  37. return source.subscribe(new RetryWhenSubscriber(subscriber, this.notifier, this.source));
  38. };
  39. return RetryWhenOperator;
  40. }());
  41. /**
  42. * We need this JSDoc comment for affecting ESDoc.
  43. * @ignore
  44. * @extends {Ignored}
  45. */
  46. var RetryWhenSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  47. __extends(RetryWhenSubscriber, _super);
  48. function RetryWhenSubscriber(destination, notifier, source) {
  49. _super.call(this, destination);
  50. this.notifier = notifier;
  51. this.source = source;
  52. }
  53. RetryWhenSubscriber.prototype.error = function (err) {
  54. if (!this.isStopped) {
  55. var errors = this.errors;
  56. var retries = this.retries;
  57. var retriesSubscription = this.retriesSubscription;
  58. if (!retries) {
  59. errors = new Subject();
  60. retries = tryCatch(this.notifier)(errors);
  61. if (retries === errorObject) {
  62. return _super.prototype.error.call(this, errorObject.e);
  63. }
  64. retriesSubscription = subscribeToResult(this, retries);
  65. }
  66. else {
  67. this.errors = null;
  68. this.retriesSubscription = null;
  69. }
  70. this._unsubscribeAndRecycle();
  71. this.errors = errors;
  72. this.retries = retries;
  73. this.retriesSubscription = retriesSubscription;
  74. errors.next(err);
  75. }
  76. };
  77. /** @deprecated internal use only */ RetryWhenSubscriber.prototype._unsubscribe = function () {
  78. var _a = this, errors = _a.errors, retriesSubscription = _a.retriesSubscription;
  79. if (errors) {
  80. errors.unsubscribe();
  81. this.errors = null;
  82. }
  83. if (retriesSubscription) {
  84. retriesSubscription.unsubscribe();
  85. this.retriesSubscription = null;
  86. }
  87. this.retries = null;
  88. };
  89. RetryWhenSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  90. var _a = this, errors = _a.errors, retries = _a.retries, retriesSubscription = _a.retriesSubscription;
  91. this.errors = null;
  92. this.retries = null;
  93. this.retriesSubscription = null;
  94. this._unsubscribeAndRecycle();
  95. this.errors = errors;
  96. this.retries = retries;
  97. this.retriesSubscription = retriesSubscription;
  98. this.source.subscribe(this);
  99. };
  100. return RetryWhenSubscriber;
  101. }(OuterSubscriber));
  102. //# sourceMappingURL=retryWhen.js.map