retry.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Subscriber_1 = require('../Subscriber');
  8. /**
  9. * Returns an Observable that mirrors the source Observable with the exception of an `error`. If the source Observable
  10. * calls `error`, this method will resubscribe to the source Observable for a maximum of `count` resubscriptions (given
  11. * as a number parameter) rather than propagating the `error` call.
  12. *
  13. * <img src="./img/retry.png" width="100%">
  14. *
  15. * Any and all items emitted by the source Observable will be emitted by the resulting Observable, even those emitted
  16. * during failed subscriptions. For example, if an Observable fails at first but emits [1, 2] then succeeds the second
  17. * time and emits: [1, 2, 3, 4, 5] then the complete stream of emissions and notifications
  18. * would be: [1, 2, 1, 2, 3, 4, 5, `complete`].
  19. * @param {number} count - Number of retry attempts before failing.
  20. * @return {Observable} The source Observable modified with the retry logic.
  21. * @method retry
  22. * @owner Observable
  23. */
  24. function retry(count) {
  25. if (count === void 0) { count = -1; }
  26. return function (source) { return source.lift(new RetryOperator(count, source)); };
  27. }
  28. exports.retry = retry;
  29. var RetryOperator = (function () {
  30. function RetryOperator(count, source) {
  31. this.count = count;
  32. this.source = source;
  33. }
  34. RetryOperator.prototype.call = function (subscriber, source) {
  35. return source.subscribe(new RetrySubscriber(subscriber, this.count, this.source));
  36. };
  37. return RetryOperator;
  38. }());
  39. /**
  40. * We need this JSDoc comment for affecting ESDoc.
  41. * @ignore
  42. * @extends {Ignored}
  43. */
  44. var RetrySubscriber = (function (_super) {
  45. __extends(RetrySubscriber, _super);
  46. function RetrySubscriber(destination, count, source) {
  47. _super.call(this, destination);
  48. this.count = count;
  49. this.source = source;
  50. }
  51. RetrySubscriber.prototype.error = function (err) {
  52. if (!this.isStopped) {
  53. var _a = this, source = _a.source, count = _a.count;
  54. if (count === 0) {
  55. return _super.prototype.error.call(this, err);
  56. }
  57. else if (count > -1) {
  58. this.count = count - 1;
  59. }
  60. source.subscribe(this._unsubscribeAndRecycle());
  61. }
  62. };
  63. return RetrySubscriber;
  64. }(Subscriber_1.Subscriber));
  65. //# sourceMappingURL=retry.js.map