| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { EmptyError } from '../util/EmptyError'; |
| 3 |
import { OperatorFunction, TruthyTypesOf } from '../types'; |
| 4 |
import { filter } from './filter'; |
| 5 |
import { take } from './take'; |
| 6 |
import { defaultIfEmpty } from './defaultIfEmpty'; |
| 7 |
import { throwIfEmpty } from './throwIfEmpty'; |
| 8 |
import { identity } from '../util/identity'; |
| 9 |
|
| 10 |
export function first<T, D = T>(predicate?: null, defaultValue?: D): OperatorFunction<T, T | D>; |
| 11 |
export function first<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>; |
| 12 |
export function first<T, D>(predicate: BooleanConstructor, defaultValue: D): OperatorFunction<T, TruthyTypesOf<T> | D>; |
| 13 |
export function first<T, S extends T>( |
| 14 |
predicate: (value: T, index: number, source: Observable<T>) => value is S, |
| 15 |
defaultValue?: S |
| 16 |
): OperatorFunction<T, S>; |
| 17 |
export function first<T, S extends T, D>( |
| 18 |
predicate: (value: T, index: number, source: Observable<T>) => value is S, |
| 19 |
defaultValue: D |
| 20 |
): OperatorFunction<T, S | D>; |
| 21 |
export function first<T, D = T>( |
| 22 |
predicate: (value: T, index: number, source: Observable<T>) => boolean, |
| 23 |
defaultValue?: D |
| 24 |
): OperatorFunction<T, T | D>; |
| 25 |
|
| 26 |
/** |
| 27 |
* Emits only the first value (or the first value that meets some condition) |
| 28 |
* emitted by the source Observable. |
| 29 |
* |
| 30 |
* <span class="informal">Emits only the first value. Or emits only the first |
| 31 |
* value that passes some test.</span> |
| 32 |
* |
| 33 |
*  |
| 34 |
* |
| 35 |
* If called with no arguments, `first` emits the first value of the source |
| 36 |
* Observable, then completes. If called with a `predicate` function, `first` |
| 37 |
* emits the first value of the source that matches the specified condition. Emits an error |
| 38 |
* notification if `defaultValue` was not provided and a matching element is not found. |
| 39 |
* |
| 40 |
* ## Examples |
| 41 |
* |
| 42 |
* Emit only the first click that happens on the DOM |
| 43 |
* |
| 44 |
* ```ts |
| 45 |
* import { fromEvent, first } from 'rxjs'; |
| 46 |
* |
| 47 |
* const clicks = fromEvent(document, 'click'); |
| 48 |
* const result = clicks.pipe(first()); |
| 49 |
* result.subscribe(x => console.log(x)); |
| 50 |
* ``` |
| 51 |
* |
| 52 |
* Emits the first click that happens on a DIV |
| 53 |
* |
| 54 |
* ```ts |
| 55 |
* import { fromEvent, first } from 'rxjs'; |
| 56 |
* |
| 57 |
* const div = document.createElement('div'); |
| 58 |
* div.style.cssText = 'width: 200px; height: 200px; background: #09c;'; |
| 59 |
* document.body.appendChild(div); |
| 60 |
* |
| 61 |
* const clicks = fromEvent(document, 'click'); |
| 62 |
* const result = clicks.pipe(first(ev => (<HTMLElement>ev.target).tagName === 'DIV')); |
| 63 |
* result.subscribe(x => console.log(x)); |
| 64 |
* ``` |
| 65 |
* |
| 66 |
* @see {@link filter} |
| 67 |
* @see {@link find} |
| 68 |
* @see {@link take} |
| 69 |
* @see {@link last} |
| 70 |
* |
| 71 |
* @throws {EmptyError} Delivers an `EmptyError` to the Observer's `error` |
| 72 |
* callback if the Observable completes before any `next` notification was sent. |
| 73 |
* This is how `first()` is different from `take(1)` which completes instead. |
| 74 |
* |
| 75 |
* @param predicate An optional function called with each item to test for condition |
| 76 |
* matching. |
| 77 |
* @param defaultValue The default value emitted in case no valid value was found on |
| 78 |
* the source. |
| 79 |
* @return A function that returns an Observable that emits the first item that |
| 80 |
* matches the condition. |
| 81 |
*/ |
| 82 |
export function first<T, D>( |
| 83 |
predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null, |
| 84 |
defaultValue?: D |
| 85 |
): OperatorFunction<T, T | D> { |
| 86 |
const hasDefaultValue = arguments.length >= 2; |
| 87 |
return (source: Observable<T>) => |
| 88 |
source.pipe( |
| 89 |
predicate ? filter((v, i) => predicate(v, i, source)) : identity, |
| 90 |
take(1), |
| 91 |
hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new EmptyError()) |
| 92 |
); |
| 93 |
} |