Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

retryWhen.ts 3.6KB

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