source: trip-planner-front/node_modules/rxjs/src/internal/operators/elementAt.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.6 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
4import { Observable } from '../Observable';
5import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6import { filter } from './filter';
7import { throwIfEmpty } from './throwIfEmpty';
8import { defaultIfEmpty } from './defaultIfEmpty';
9import { 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 */
59export 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}
Note: See TracBrowser for help on using the repository browser.