1 | import { Operator } from '../Operator';
|
---|
2 | import { Subscriber } from '../Subscriber';
|
---|
3 | import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
|
---|
4 | import { Observable } from '../Observable';
|
---|
5 | import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
|
---|
6 | import { filter } from './filter';
|
---|
7 | import { throwIfEmpty } from './throwIfEmpty';
|
---|
8 | import { defaultIfEmpty } from './defaultIfEmpty';
|
---|
9 | import { take } from './take';
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Emits the single value at the specified `index` in a sequence of emissions
|
---|
13 | * from the source Observable.
|
---|
14 | *
|
---|
15 | * <span class="informal">Emits only the i-th value, then completes.</span>
|
---|
16 | *
|
---|
17 | * ![](elementAt.png)
|
---|
18 | *
|
---|
19 | * `elementAt` returns an Observable that emits the item at the specified
|
---|
20 | * `index` in the source Observable, or a default value if that `index` is out
|
---|
21 | * of range and the `default` argument is provided. If the `default` argument is
|
---|
22 | * not given and the `index` is out of range, the output Observable will emit an
|
---|
23 | * `ArgumentOutOfRangeError` error.
|
---|
24 | *
|
---|
25 | * ## Example
|
---|
26 | * Emit only the third click event
|
---|
27 | * ```ts
|
---|
28 | * import { fromEvent } from 'rxjs';
|
---|
29 | * import { elementAt } from 'rxjs/operators';
|
---|
30 | *
|
---|
31 | * const clicks = fromEvent(document, 'click');
|
---|
32 | * const result = clicks.pipe(elementAt(2));
|
---|
33 | * result.subscribe(x => console.log(x));
|
---|
34 | *
|
---|
35 | * // Results in:
|
---|
36 | * // click 1 = nothing
|
---|
37 | * // click 2 = nothing
|
---|
38 | * // click 3 = MouseEvent object logged to console
|
---|
39 | * ```
|
---|
40 | *
|
---|
41 | * @see {@link first}
|
---|
42 | * @see {@link last}
|
---|
43 | * @see {@link skip}
|
---|
44 | * @see {@link single}
|
---|
45 | * @see {@link take}
|
---|
46 | *
|
---|
47 | * @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
|
---|
48 | * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0` or the
|
---|
49 | * Observable has completed before emitting the i-th `next` notification.
|
---|
50 | *
|
---|
51 | * @param {number} index Is the number `i` for the i-th source emission that has
|
---|
52 | * happened since the subscription, starting from the number `0`.
|
---|
53 | * @param {T} [defaultValue] The default value returned for missing indices.
|
---|
54 | * @return {Observable} An Observable that emits a single item, if it is found.
|
---|
55 | * Otherwise, will emit the default value if given. If not, then emits an error.
|
---|
56 | * @method elementAt
|
---|
57 | * @owner Observable
|
---|
58 | */
|
---|
59 | export function elementAt<T>(index: number, defaultValue?: T): MonoTypeOperatorFunction<T> {
|
---|
60 | if (index < 0) { throw new ArgumentOutOfRangeError(); }
|
---|
61 | const hasDefaultValue = arguments.length >= 2;
|
---|
62 | return (source: Observable<T>) => source.pipe(
|
---|
63 | filter((v, i) => i === index),
|
---|
64 | take(1),
|
---|
65 | hasDefaultValue
|
---|
66 | ? defaultIfEmpty(defaultValue)
|
---|
67 | : throwIfEmpty(() => new ArgumentOutOfRangeError()),
|
---|
68 | );
|
---|
69 | }
|
---|