Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

retryWhen.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { Subject } from '../Subject';
  5. import { Subscription } from '../Subscription';
  6. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  7. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  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 emit the Throwable that caused the error to the Observable returned from `notifier`.
  11. * If that Observable calls `complete` or `error` then this method will call `complete` or `error` on the child
  12. * subscription. Otherwise this method will resubscribe to the source Observable.
  13. *
  14. * ![](retryWhen.png)
  15. *
  16. * @param {function(errors: Observable): Observable} notifier - Receives an Observable of notifications with which a
  17. * user can `complete` or `error`, aborting the retry.
  18. * @return {Observable} The source Observable modified with retry logic.
  19. * @method retryWhen
  20. * @owner Observable
  21. */
  22. export function retryWhen<T>(notifier: (errors: Observable<any>) => Observable<any>): MonoTypeOperatorFunction<T> {
  23. return (source: Observable<T>) => source.lift(new RetryWhenOperator(notifier, source));
  24. }
  25. class RetryWhenOperator<T> implements Operator<T, T> {
  26. constructor(protected notifier: (errors: Observable<any>) => Observable<any>,
  27. protected source: Observable<T>) {
  28. }
  29. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  30. return source.subscribe(new RetryWhenSubscriber(subscriber, this.notifier, this.source));
  31. }
  32. }
  33. /**
  34. * We need this JSDoc comment for affecting ESDoc.
  35. * @ignore
  36. * @extends {Ignored}
  37. */
  38. class RetryWhenSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
  39. private errors?: Subject<any>;
  40. private retries?: Observable<any>;
  41. private retriesSubscription?: Subscription;
  42. constructor(destination: Subscriber<R>,
  43. private notifier: (errors: Observable<any>) => Observable<any>,
  44. private source: Observable<T>) {
  45. super(destination);
  46. }
  47. error(err: any) {
  48. if (!this.isStopped) {
  49. let errors = this.errors;
  50. let retries: any = this.retries;
  51. let retriesSubscription = this.retriesSubscription;
  52. if (!retries) {
  53. errors = new Subject();
  54. try {
  55. const { notifier } = this;
  56. retries = notifier(errors);
  57. } catch (e) {
  58. return super.error(e);
  59. }
  60. retriesSubscription = innerSubscribe(retries, new SimpleInnerSubscriber(this));
  61. } else {
  62. this.errors = undefined;
  63. this.retriesSubscription = undefined;
  64. }
  65. this._unsubscribeAndRecycle();
  66. this.errors = errors;
  67. this.retries = retries;
  68. this.retriesSubscription = retriesSubscription;
  69. errors!.next(err);
  70. }
  71. }
  72. /** @deprecated This is an internal implementation detail, do not use. */
  73. _unsubscribe() {
  74. const { errors, retriesSubscription } = this;
  75. if (errors) {
  76. errors.unsubscribe();
  77. this.errors = undefined;
  78. }
  79. if (retriesSubscription) {
  80. retriesSubscription.unsubscribe();
  81. this.retriesSubscription = undefined;
  82. }
  83. this.retries = undefined;
  84. }
  85. notifyNext(): void {
  86. const { _unsubscribe } = this;
  87. this._unsubscribe = null!;
  88. this._unsubscribeAndRecycle();
  89. this._unsubscribe = _unsubscribe;
  90. this.source.subscribe(this);
  91. }
  92. }