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';
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
11 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
14 * Returns an Observable that mirrors the source Observable with the exception of a `complete`. If the source
15 * Observable calls `complete`, this method will emit to the Observable returned from `notifier`. If that Observable
16 * calls `complete` or `error`, then this method will call `complete` or `error` on the child subscription. Otherwise
17 * this method will resubscribe to the source Observable.
22 * Repeat a message stream on click
24 * import { of, fromEvent } from 'rxjs';
25 * import { repeatWhen } from 'rxjs/operators';
27 * const source = of('Repeat message');
28 * const documentClick$ = fromEvent(document, 'click');
30 * source.pipe(repeatWhen(() => documentClick$)
31 * ).subscribe(data => console.log(data))
35 * @see {@link retryWhen}
37 * @param {function(notifications: Observable): Observable} notifier - Receives an Observable of notifications with
38 * which a user can `complete` or `error`, aborting the repetition.
39 * @return {Observable} The source Observable modified with repeat logic.
43 export function repeatWhen<T>(notifier: (notifications: Observable<any>) => Observable<any>): MonoTypeOperatorFunction<T> {
44 return (source: Observable<T>) => source.lift(new RepeatWhenOperator(notifier));
47 class RepeatWhenOperator<T> implements Operator<T, T> {
48 constructor(protected notifier: (notifications: Observable<any>) => Observable<any>) {
51 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
52 return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, source));
57 * We need this JSDoc comment for affecting ESDoc.
61 class RepeatWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
63 private notifications: Subject<any>;
64 private retries: Observable<any>;
65 private retriesSubscription: Subscription;
66 private sourceIsBeingSubscribedTo: boolean = true;
68 constructor(destination: Subscriber<R>,
69 private notifier: (notifications: Observable<any>) => Observable<any>,
70 private source: Observable<T>) {
74 notifyNext(outerValue: T, innerValue: R,
75 outerIndex: number, innerIndex: number,
76 innerSub: InnerSubscriber<T, R>): void {
77 this.sourceIsBeingSubscribedTo = true;
78 this.source.subscribe(this);
81 notifyComplete(innerSub: InnerSubscriber<T, R>): void {
82 if (this.sourceIsBeingSubscribedTo === false) {
83 return super.complete();
88 this.sourceIsBeingSubscribedTo = false;
90 if (!this.isStopped) {
92 this.subscribeToRetries();
94 if (!this.retriesSubscription || this.retriesSubscription.closed) {
95 return super.complete();
98 this._unsubscribeAndRecycle();
99 this.notifications.next();
103 /** @deprecated This is an internal implementation detail, do not use. */
105 const { notifications, retriesSubscription } = this;
107 notifications.unsubscribe();
108 this.notifications = null;
110 if (retriesSubscription) {
111 retriesSubscription.unsubscribe();
112 this.retriesSubscription = null;
117 /** @deprecated This is an internal implementation detail, do not use. */
118 _unsubscribeAndRecycle(): Subscriber<T> {
119 const { _unsubscribe } = this;
121 this._unsubscribe = null;
122 super._unsubscribeAndRecycle();
123 this._unsubscribe = _unsubscribe;
128 private subscribeToRetries() {
129 this.notifications = new Subject();
132 const { notifier } = this;
133 retries = notifier(this.notifications);
135 return super.complete();
137 this.retries = retries;
138 this.retriesSubscription = subscribeToResult(this, retries);