1 import { Observable } from '../Observable';
2 import { Operator } from '../Operator';
3 import { Subscriber } from '../Subscriber';
4 import { EmptyError } from '../util/EmptyError';
5 import { OperatorFunction } from '../../internal/types';
6 import { filter } from './filter';
7 import { take } from './take';
8 import { defaultIfEmpty } from './defaultIfEmpty';
9 import { throwIfEmpty } from './throwIfEmpty';
10 import { identity } from '../util/identity';
12 /* tslint:disable:max-line-length */
13 export function first<T, D = T>(
16 ): OperatorFunction<T, T | D>;
17 export function first<T, S extends T>(
18 predicate: (value: T, index: number, source: Observable<T>) => value is S,
20 ): OperatorFunction<T, S>;
21 export function first<T, D = T>(
22 predicate: (value: T, index: number, source: Observable<T>) => boolean,
24 ): OperatorFunction<T, T | D>;
25 /* tslint:enable:max-line-length */
28 * Emits only the first value (or the first value that meets some condition)
29 * emitted by the source Observable.
31 * <span class="informal">Emits only the first value. Or emits only the first
32 * value that passes some test.</span>
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.
45 * Emit only the first click that happens on the DOM
47 * import { fromEvent } from 'rxjs';
48 * import { first } from 'rxjs/operators';
50 * const clicks = fromEvent(document, 'click');
51 * const result = clicks.pipe(first());
52 * result.subscribe(x => console.log(x));
55 * Emits the first click that happens on a DIV
57 * import { fromEvent } from 'rxjs';
58 * import { first } from 'rxjs/operators';
60 * const clicks = fromEvent(document, 'click');
61 * const result = clicks.pipe(first(ev => ev.target.tagName === 'DIV'));
62 * result.subscribe(x => console.log(x));
69 * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
70 * callback if the Observable completes before any `next` notification was sent.
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
81 export function first<T, D>(
82 predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
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,
89 hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),