retryWhen.js 4.2KB

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