source: trip-planner-front/node_modules/rxjs/src/internal/operators/takeUntil.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4
5import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6import { innerSubscribe, SimpleInnerSubscriber, SimpleOuterSubscriber } from '../innerSubscribe';
7
8/**
9 * Emits the values emitted by the source Observable until a `notifier`
10 * Observable emits a value.
11 *
12 * <span class="informal">Lets values pass until a second Observable,
13 * `notifier`, emits a value. Then, it completes.</span>
14 *
15 * ![](takeUntil.png)
16 *
17 * `takeUntil` subscribes and begins mirroring the source Observable. It also
18 * monitors a second Observable, `notifier` that you provide. If the `notifier`
19 * emits a value, the output Observable stops mirroring the source Observable
20 * and completes. If the `notifier` doesn't emit any value and completes
21 * then `takeUntil` will pass all values.
22 *
23 * ## Example
24 * Tick every second until the first click happens
25 * ```ts
26 * import { fromEvent, interval } from 'rxjs';
27 * import { takeUntil } from 'rxjs/operators';
28 *
29 * const source = interval(1000);
30 * const clicks = fromEvent(document, 'click');
31 * const result = source.pipe(takeUntil(clicks));
32 * result.subscribe(x => console.log(x));
33 * ```
34 *
35 * @see {@link take}
36 * @see {@link takeLast}
37 * @see {@link takeWhile}
38 * @see {@link skip}
39 *
40 * @param {Observable} notifier The Observable whose first emitted value will
41 * cause the output Observable of `takeUntil` to stop emitting values from the
42 * source Observable.
43 * @return {Observable<T>} An Observable that emits the values from the source
44 * Observable until such time as `notifier` emits its first value.
45 * @method takeUntil
46 * @owner Observable
47 */
48export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
49 return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
50}
51
52class TakeUntilOperator<T> implements Operator<T, T> {
53 constructor(private notifier: Observable<any>) {
54 }
55
56 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
57 const takeUntilSubscriber = new TakeUntilSubscriber(subscriber);
58 const notifierSubscription = innerSubscribe(this.notifier, new SimpleInnerSubscriber(takeUntilSubscriber));
59 if (notifierSubscription && !takeUntilSubscriber.seenValue) {
60 takeUntilSubscriber.add(notifierSubscription);
61 return source.subscribe(takeUntilSubscriber);
62 }
63 return takeUntilSubscriber;
64 }
65}
66
67/**
68 * We need this JSDoc comment for affecting ESDoc.
69 * @ignore
70 * @extends {Ignored}
71 */
72class TakeUntilSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
73 seenValue = false;
74
75 constructor(destination: Subscriber<any>, ) {
76 super(destination);
77 }
78
79 notifyNext(): void {
80 this.seenValue = true;
81 this.complete();
82 }
83
84 notifyComplete(): void {
85 // noop
86 }
87}
Note: See TracBrowser for help on using the repository browser.