retry.js 2.7 KB

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