1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
6 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
7 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>;
8 export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>;
11 * Emits values emitted by the source Observable so long as each value satisfies
12 * the given `predicate`, and then completes as soon as this `predicate` is not
15 * <span class="informal">Takes values from the source only while they pass the
16 * condition given. When the first value does not satisfy, it completes.</span>
20 * `takeWhile` subscribes and begins mirroring the source Observable. Each value
21 * emitted on the source is given to the `predicate` function which returns a
22 * boolean, representing a condition to be satisfied by the source values. The
23 * output Observable emits the source values until such time as the `predicate`
24 * returns false, at which point `takeWhile` stops mirroring the source
25 * Observable and completes the output Observable.
28 * Emit click events only while the clientX property is greater than 200
30 * import { fromEvent } from 'rxjs';
31 * import { takeWhile } from 'rxjs/operators';
33 * const clicks = fromEvent(document, 'click');
34 * const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
35 * result.subscribe(x => console.log(x));
39 * @see {@link takeLast}
40 * @see {@link takeUntil}
43 * @param {function(value: T, index: number): boolean} predicate A function that
44 * evaluates a value emitted by the source Observable and returns a boolean.
45 * Also takes the (zero-based) index as the second argument.
46 * @param {boolean} inclusive When set to `true` the value that caused
47 * `predicate` to return `false` will also be emitted.
48 * @return {Observable<T>} An Observable that emits the values from the source
49 * Observable so long as each value satisfies the condition defined by the
50 * `predicate`, then completes.
54 export function takeWhile<T>(
55 predicate: (value: T, index: number) => boolean,
56 inclusive = false): MonoTypeOperatorFunction<T> {
57 return (source: Observable<T>) =>
58 source.lift(new TakeWhileOperator(predicate, inclusive));
61 class TakeWhileOperator<T> implements Operator<T, T> {
63 private predicate: (value: T, index: number) => boolean,
64 private inclusive: boolean) {}
66 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
67 return source.subscribe(
68 new TakeWhileSubscriber(subscriber, this.predicate, this.inclusive));
73 * We need this JSDoc comment for affecting ESDoc.
77 class TakeWhileSubscriber<T> extends Subscriber<T> {
78 private index: number = 0;
81 destination: Subscriber<T>,
82 private predicate: (value: T, index: number) => boolean,
83 private inclusive: boolean) {
87 protected _next(value: T): void {
88 const destination = this.destination;
91 result = this.predicate(value, this.index++);
93 destination.error(err);
96 this.nextOrComplete(value, result);
99 private nextOrComplete(value: T, predicateResult: boolean): void {
100 const destination = this.destination;
101 if (Boolean(predicateResult)) {
102 destination.next(value);
104 if (this.inclusive) {
105 destination.next(value);
107 destination.complete();