UNPKG

1.5 kBPlain TextView Raw
1import { Observable } from 'rxjs';
2import { take as higherOrder } from 'rxjs/operators';
3
4/**
5 * Emits only the first `count` values emitted by the source Observable.
6 *
7 * <span class="informal">Takes the first `count` values from the source, then
8 * completes.</span>
9 *
10 * <img src="./img/take.png" width="100%">
11 *
12 * `take` returns an Observable that emits only the first `count` values emitted
13 * by the source Observable. If the source emits fewer than `count` values then
14 * all of its values are emitted. After that, it completes, regardless if the
15 * source completes.
16 *
17 * @example <caption>Take the first 5 seconds of an infinite 1-second interval Observable</caption>
18 * var interval = Rx.Observable.interval(1000);
19 * var five = interval.take(5);
20 * five.subscribe(x => console.log(x));
21 *
22 * @see {@link takeLast}
23 * @see {@link takeUntil}
24 * @see {@link takeWhile}
25 * @see {@link skip}
26 *
27 * @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
28 * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
29 *
30 * @param {number} count The maximum number of `next` values to emit.
31 * @return {Observable<T>} An Observable that emits only the first `count`
32 * values emitted by the source Observable, or all of the values from the source
33 * if the source emits fewer than `count` values.
34 * @method take
35 * @owner Observable
36 */
37export function take<T>(this: Observable<T>, count: number): Observable<T> {
38 return higherOrder(count)(this) as Observable<T>;
39}