Debian Codegraph @

node/rxjs/src/internal/operators/repeatWhen.ts

raw
1 import { Observable } from '../Observable';
2 import { innerFrom } from '../observable/innerFrom';
3 import { Subject } from '../Subject';
4 import { Subscription } from '../Subscription';
5
6 import { MonoTypeOperatorFunction, ObservableInput } from '../types';
7 import { operate } from '../util/lift';
8 import { createOperatorSubscriber } from './OperatorSubscriber';
9
10 /**
11 * Returns an Observable that mirrors the source Observable with the exception of a `complete`. If the source
12 * Observable calls `complete`, this method will emit to the Observable returned from `notifier`. If that Observable
13 * calls `complete` or `error`, then this method will call `complete` or `error` on the child subscription. Otherwise
14 * this method will resubscribe to the source Observable.
15 *
16 * ![](repeatWhen.png)
17 *
18 * ## Example
19 *
20 * Repeat a message stream on click
21 *
22 * ```ts
23 * import { of, fromEvent, repeatWhen } from 'rxjs';
24 *
25 * const source = of('Repeat message');
26 * const documentClick$ = fromEvent(document, 'click');
27 *
28 * const result = source.pipe(repeatWhen(() => documentClick$));
29 *
30 * result.subscribe(data => console.log(data))
31 * ```
32 *
33 * @see {@link repeat}
34 * @see {@link retry}
35 * @see {@link retryWhen}
36 *
37 * @param notifier Function that receives an Observable of notifications with
38 * which a user can `complete` or `error`, aborting the repetition.
39 * @return A function that returns an Observable that mirrors the source
40 * Observable with the exception of a `complete`.
41 * @deprecated Will be removed in v9 or v10. Use {@link repeat}'s {@link RepeatConfig#delay delay} option instead.
42 * Instead of `repeatWhen(() => notify$)`, use: `repeat({ delay: () => notify$ })`.
43 */
44 export function repeatWhen<T>(notifier: (notifications: Observable<void>) => ObservableInput<any>): MonoTypeOperatorFunction<T> {
45 return operate((source, subscriber) => {
46 let innerSub: Subscription | null;
47 let syncResub = false;
48 let completions$: Subject<void>;
49 let isNotifierComplete = false;
50 let isMainComplete = false;
51
52 /**
53 * Checks to see if we can complete the result, completes it, and returns `true` if it was completed.
54 */
55 const checkComplete = () => isMainComplete && isNotifierComplete && (subscriber.complete(), true);
56 /**
57 * Gets the subject to send errors through. If it doesn't exist,
58 * we know we need to setup the notifier.
59 */
60 const getCompletionSubject = () => {
61 if (!completions$) {
62 completions$ = new Subject();
63
64 // If the call to `notifier` throws, it will be caught by the OperatorSubscriber
65 // In the main subscription -- in `subscribeForRepeatWhen`.
66 innerFrom(notifier(completions$)).subscribe(
67 createOperatorSubscriber(
68 subscriber,
69 () => {
70 if (innerSub) {
71 subscribeForRepeatWhen();
72 } else {
73 // If we don't have an innerSub yet, that's because the inner subscription
74 // call hasn't even returned yet. We've arrived here synchronously.
75 // So we flag that we want to resub, such that we can ensure finalization
76 // happens before we resubscribe.
77 syncResub = true;
78 }
79 },
80 () => {
81 isNotifierComplete = true;
82 checkComplete();
83 }
84 )
85 );
86 }
87 return completions$;
88 };
89
90 const subscribeForRepeatWhen = () => {
91 isMainComplete = false;
92
93 innerSub = source.subscribe(
94 createOperatorSubscriber(subscriber, undefined, () => {
95 isMainComplete = true;
96 // Check to see if we are complete, and complete if so.
97 // If we are not complete. Get the subject. This calls the `notifier` function.
98 // If that function fails, it will throw and `.next()` will not be reached on this
99 // line. The thrown error is caught by the _complete handler in this
100 // `OperatorSubscriber` and handled appropriately.
101 !checkComplete() && getCompletionSubject().next();
102 })
103 );
104
105 if (syncResub) {
106 // Ensure that the inner subscription is torn down before
107 // moving on to the next subscription in the synchronous case.
108 // If we don't do this here, all inner subscriptions will not be
109 // torn down until the entire observable is done.
110 innerSub.unsubscribe();
111 // It is important to null this out. Not only to free up memory, but
112 // to make sure code above knows we are in a subscribing state to
113 // handle synchronous resubscription.
114 innerSub = null;
115 // We may need to do this multiple times, so reset the flags.
116 syncResub = false;
117 // Resubscribe
118 subscribeForRepeatWhen();
119 }
120 };
121
122 // Start the subscription
123 subscribeForRepeatWhen();
124 });
125 }