source: trip-planner-front/node_modules/rxjs/src/internal/operators/take.ts

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
4import { empty } from '../observable/empty';
5import { Observable } from '../Observable';
6import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
7
8/**
9 * Emits only the first `count` values emitted by the source Observable.
10 *
11 * <span class="informal">Takes the first `count` values from the source, then
12 * completes.</span>
13 *
14 * ![](take.png)
15 *
16 * `take` returns an Observable that emits only the first `count` values emitted
17 * by the source Observable. If the source emits fewer than `count` values then
18 * all of its values are emitted. After that, it completes, regardless if the
19 * source completes.
20 *
21 * ## Example
22 * Take the first 5 seconds of an infinite 1-second interval Observable
23 * ```ts
24 * import { interval } from 'rxjs';
25 * import { take } from 'rxjs/operators';
26 *
27 * const intervalCount = interval(1000);
28 * const takeFive = intervalCount.pipe(take(5));
29 * takeFive.subscribe(x => console.log(x));
30 *
31 * // Logs:
32 * // 0
33 * // 1
34 * // 2
35 * // 3
36 * // 4
37 * ```
38 *
39 * @see {@link takeLast}
40 * @see {@link takeUntil}
41 * @see {@link takeWhile}
42 * @see {@link skip}
43 *
44 * @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
45 * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
46 *
47 * @param {number} count The maximum number of `next` values to emit.
48 * @return {Observable<T>} An Observable that emits only the first `count`
49 * values emitted by the source Observable, or all of the values from the source
50 * if the source emits fewer than `count` values.
51 * @method take
52 * @owner Observable
53 */
54export function take<T>(count: number): MonoTypeOperatorFunction<T> {
55 return (source: Observable<T>) => {
56 if (count === 0) {
57 return empty();
58 } else {
59 return source.lift(new TakeOperator(count));
60 }
61 };
62}
63
64class TakeOperator<T> implements Operator<T, T> {
65 constructor(private total: number) {
66 if (this.total < 0) {
67 throw new ArgumentOutOfRangeError;
68 }
69 }
70
71 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
72 return source.subscribe(new TakeSubscriber(subscriber, this.total));
73 }
74}
75
76/**
77 * We need this JSDoc comment for affecting ESDoc.
78 * @ignore
79 * @extends {Ignored}
80 */
81class TakeSubscriber<T> extends Subscriber<T> {
82 private count: number = 0;
83
84 constructor(destination: Subscriber<T>, private total: number) {
85 super(destination);
86 }
87
88 protected _next(value: T): void {
89 const total = this.total;
90 const count = ++this.count;
91 if (count <= total) {
92 this.destination.next(value);
93 if (count === total) {
94 this.destination.complete();
95 this.unsubscribe();
96 }
97 }
98 }
99}
Note: See TracBrowser for help on using the repository browser.