retry.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.retry = void 0;
  4. var lift_1 = require("../util/lift");
  5. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  6. var identity_1 = require("../util/identity");
  7. var timer_1 = require("../observable/timer");
  8. var innerFrom_1 = require("../observable/innerFrom");
  9. function retry(configOrCount) {
  10. if (configOrCount === void 0) { configOrCount = Infinity; }
  11. var config;
  12. if (configOrCount && typeof configOrCount === 'object') {
  13. config = configOrCount;
  14. }
  15. else {
  16. config = {
  17. count: configOrCount,
  18. };
  19. }
  20. var _a = config.count, count = _a === void 0 ? Infinity : _a, delay = config.delay, _b = config.resetOnSuccess, resetOnSuccess = _b === void 0 ? false : _b;
  21. return count <= 0
  22. ? identity_1.identity
  23. : lift_1.operate(function (source, subscriber) {
  24. var soFar = 0;
  25. var innerSub;
  26. var subscribeForRetry = function () {
  27. var syncUnsub = false;
  28. innerSub = source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  29. if (resetOnSuccess) {
  30. soFar = 0;
  31. }
  32. subscriber.next(value);
  33. }, undefined, function (err) {
  34. if (soFar++ < count) {
  35. var resub_1 = function () {
  36. if (innerSub) {
  37. innerSub.unsubscribe();
  38. innerSub = null;
  39. subscribeForRetry();
  40. }
  41. else {
  42. syncUnsub = true;
  43. }
  44. };
  45. if (delay != null) {
  46. var notifier = typeof delay === 'number' ? timer_1.timer(delay) : innerFrom_1.innerFrom(delay(err, soFar));
  47. var notifierSubscriber_1 = OperatorSubscriber_1.createOperatorSubscriber(subscriber, function () {
  48. notifierSubscriber_1.unsubscribe();
  49. resub_1();
  50. }, function () {
  51. subscriber.complete();
  52. });
  53. notifier.subscribe(notifierSubscriber_1);
  54. }
  55. else {
  56. resub_1();
  57. }
  58. }
  59. else {
  60. subscriber.error(err);
  61. }
  62. }));
  63. if (syncUnsub) {
  64. innerSub.unsubscribe();
  65. innerSub = null;
  66. subscribeForRetry();
  67. }
  68. };
  69. subscribeForRetry();
  70. });
  71. }
  72. exports.retry = retry;
  73. //# sourceMappingURL=retry.js.map