bfff46fa9566005e993a98b8f1799f25da89bb6de9e2143ce70fbfc0238d0254995ebf7cee9668183359a86de748bf011daf6feb45b6387aeffb72029f2fe3 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 a `complete`. If the source
  10. * Observable calls `complete`, this method will emit to the Observable returned from `notifier`. If that Observable
  11. * calls `complete` or `error`, then this method will call `complete` or `error` on the child subscription. Otherwise
  12. * this method will resubscribe to the source Observable.
  13. *
  14. * ![](repeatWhen.png)
  15. *
  16. * ## Example
  17. * Repeat a message stream on click
  18. * ```ts
  19. * import { of, fromEvent } from 'rxjs';
  20. * import { repeatWhen } from 'rxjs/operators';
  21. *
  22. * const source = of('Repeat message');
  23. * const documentClick$ = fromEvent(document, 'click');
  24. *
  25. * source.pipe(repeatWhen(() => documentClick$)
  26. * ).subscribe(data => console.log(data))
  27. * ```
  28. * @see {@link repeat}
  29. * @see {@link retry}
  30. * @see {@link retryWhen}
  31. *
  32. * @param {function(notifications: Observable): Observable} notifier - Receives an Observable of notifications with
  33. * which a user can `complete` or `error`, aborting the repetition.
  34. * @return {Observable} The source Observable modified with repeat logic.
  35. * @method repeatWhen
  36. * @owner Observable
  37. */
  38. export function repeatWhen<T>(notifier: (notifications: Observable<any>) => Observable<any>): MonoTypeOperatorFunction<T> {
  39. return (source: Observable<T>) => source.lift(new RepeatWhenOperator(notifier));
  40. }
  41. class RepeatWhenOperator<T> implements Operator<T, T> {
  42. constructor(protected notifier: (notifications: Observable<any>) => Observable<any>) {
  43. }
  44. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  45. return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, source));
  46. }
  47. }
  48. /**
  49. * We need this JSDoc comment for affecting ESDoc.
  50. * @ignore
  51. * @extends {Ignored}
  52. */
  53. class RepeatWhenSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
  54. private notifications?: Subject<any>;
  55. private retries?: Observable<any>;
  56. private retriesSubscription?: Subscription;
  57. private sourceIsBeingSubscribedTo: boolean = true;
  58. constructor(destination: Subscriber<R>,
  59. private notifier: (notifications: Observable<any>) => Observable<any>,
  60. private source: Observable<T>) {
  61. super(destination);
  62. }
  63. notifyNext(): void {
  64. this.sourceIsBeingSubscribedTo = true;
  65. this.source.subscribe(this);
  66. }
  67. notifyComplete(): void {
  68. if (this.sourceIsBeingSubscribedTo === false) {
  69. return super.complete();
  70. }
  71. }
  72. complete() {
  73. this.sourceIsBeingSubscribedTo = false;
  74. if (!this.isStopped) {
  75. if (!this.retries) {
  76. this.subscribeToRetries();
  77. }
  78. if (!this.retriesSubscription || this.retriesSubscription.closed) {
  79. return super.complete();
  80. }
  81. this._unsubscribeAndRecycle();
  82. this.notifications!.next(undefined);
  83. }
  84. }
  85. /** @deprecated This is an internal implementation detail, do not use. */
  86. _unsubscribe() {
  87. const { notifications, retriesSubscription } = this;
  88. if (notifications) {
  89. notifications.unsubscribe();
  90. this.notifications = undefined;
  91. }
  92. if (retriesSubscription) {
  93. retriesSubscription.unsubscribe();
  94. this.retriesSubscription = undefined;
  95. }
  96. this.retries = undefined;
  97. }
  98. /** @deprecated This is an internal implementation detail, do not use. */
  99. _unsubscribeAndRecycle(): Subscriber<T> {
  100. const { _unsubscribe } = this;
  101. this._unsubscribe = null!;
  102. super._unsubscribeAndRecycle();
  103. this._unsubscribe = _unsubscribe;
  104. return this;
  105. }
  106. private subscribeToRetries() {
  107. this.notifications = new Subject();
  108. let retries;
  109. try {
  110. const { notifier } = this;
  111. retries = notifier(this.notifications);
  112. } catch (e) {
  113. return super.complete();
  114. }
  115. this.retries = retries;
  116. this.retriesSubscription = innerSubscribe(retries, new SimpleInnerSubscriber(this));
  117. }
  118. }