source: trip-planner-front/node_modules/rxjs/src/internal/operators/first.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: 3.4 KB
Line 
1import { Observable } from '../Observable';
2import { Operator } from '../Operator';
3import { Subscriber } from '../Subscriber';
4import { EmptyError } from '../util/EmptyError';
5import { OperatorFunction } from '../../internal/types';
6import { filter } from './filter';
7import { take } from './take';
8import { defaultIfEmpty } from './defaultIfEmpty';
9import { throwIfEmpty } from './throwIfEmpty';
10import { identity } from '../util/identity';
11
12/* tslint:disable:max-line-length */
13export function first<T, D = T>(
14 predicate?: null,
15 defaultValue?: D
16): OperatorFunction<T, T | D>;
17export function first<T, S extends T>(
18 predicate: (value: T, index: number, source: Observable<T>) => value is S,
19 defaultValue?: S
20): OperatorFunction<T, S>;
21export function first<T, D = T>(
22 predicate: (value: T, index: number, source: Observable<T>) => boolean,
23 defaultValue?: D
24): OperatorFunction<T, T | D>;
25/* tslint:enable:max-line-length */
26
27/**
28 * Emits only the first value (or the first value that meets some condition)
29 * emitted by the source Observable.
30 *
31 * <span class="informal">Emits only the first value. Or emits only the first
32 * value that passes some test.</span>
33 *
34 * ![](first.png)
35 *
36 * If called with no arguments, `first` emits the first value of the source
37 * Observable, then completes. If called with a `predicate` function, `first`
38 * emits the first value of the source that matches the specified condition. It
39 * may also take a deprecated `resultSelector` function to produce the output
40 * value from the input value, and a `defaultValue` to emit in case the source
41 * completes before it is able to emit a valid value. Throws an error if
42 * `defaultValue` was not provided and a matching element is not found.
43 *
44 * ## Examples
45 * Emit only the first click that happens on the DOM
46 * ```ts
47 * import { fromEvent } from 'rxjs';
48 * import { first } from 'rxjs/operators';
49 *
50 * const clicks = fromEvent(document, 'click');
51 * const result = clicks.pipe(first());
52 * result.subscribe(x => console.log(x));
53 * ```
54 *
55 * Emits the first click that happens on a DIV
56 * ```ts
57 * import { fromEvent } from 'rxjs';
58 * import { first } from 'rxjs/operators';
59 *
60 * const clicks = fromEvent(document, 'click');
61 * const result = clicks.pipe(first(ev => ev.target.tagName === 'DIV'));
62 * result.subscribe(x => console.log(x));
63 * ```
64 *
65 * @see {@link filter}
66 * @see {@link find}
67 * @see {@link take}
68 *
69 * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
70 * callback if the Observable completes before any `next` notification was sent.
71 *
72 * @param {function(value: T, index: number, source: Observable<T>): boolean} [predicate]
73 * An optional function called with each item to test for condition matching.
74 * @param {R} [defaultValue] The default value emitted in case no valid value
75 * was found on the source.
76 * @return {Observable<T|R>} An Observable of the first item that matches the
77 * condition.
78 * @method first
79 * @owner Observable
80 */
81export function first<T, D>(
82 predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
83 defaultValue?: D
84): OperatorFunction<T, T | D> {
85 const hasDefaultValue = arguments.length >= 2;
86 return (source: Observable<T>) => source.pipe(
87 predicate ? filter((v, i) => predicate(v, i, source)) : identity,
88 take(1),
89 hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
90 );
91}
Note: See TracBrowser for help on using the repository browser.