retry.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = Infinity) {
  7. let config;
  8. if (configOrCount && typeof configOrCount === 'object') {
  9. config = configOrCount;
  10. }
  11. else {
  12. config = {
  13. count: configOrCount,
  14. };
  15. }
  16. const { count = Infinity, delay, resetOnSuccess: resetOnSuccess = false } = config;
  17. return count <= 0
  18. ? identity
  19. : operate((source, subscriber) => {
  20. let soFar = 0;
  21. let innerSub;
  22. const subscribeForRetry = () => {
  23. let syncUnsub = false;
  24. innerSub = source.subscribe(createOperatorSubscriber(subscriber, (value) => {
  25. if (resetOnSuccess) {
  26. soFar = 0;
  27. }
  28. subscriber.next(value);
  29. }, undefined, (err) => {
  30. if (soFar++ < count) {
  31. const resub = () => {
  32. if (innerSub) {
  33. innerSub.unsubscribe();
  34. innerSub = null;
  35. subscribeForRetry();
  36. }
  37. else {
  38. syncUnsub = true;
  39. }
  40. };
  41. if (delay != null) {
  42. const notifier = typeof delay === 'number' ? timer(delay) : innerFrom(delay(err, soFar));
  43. const notifierSubscriber = createOperatorSubscriber(subscriber, () => {
  44. notifierSubscriber.unsubscribe();
  45. resub();
  46. }, () => {
  47. subscriber.complete();
  48. });
  49. notifier.subscribe(notifierSubscriber);
  50. }
  51. else {
  52. resub();
  53. }
  54. }
  55. else {
  56. subscriber.error(err);
  57. }
  58. }));
  59. if (syncUnsub) {
  60. innerSub.unsubscribe();
  61. innerSub = null;
  62. subscribeForRetry();
  63. }
  64. };
  65. subscribeForRetry();
  66. });
  67. }
  68. //# sourceMappingURL=retry.js.map