Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / last.ts
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 { takeLast } from './takeLast';
8 import { throwIfEmpty } from './throwIfEmpty';
9 import { defaultIfEmpty } from './defaultIfEmpty';
10 import { identity } from '../util/identity';
11
12 /* tslint:disable:max-line-length */
13 export function last<T, D = T>(
14   predicate?: null,
15   defaultValue?: D
16 ): OperatorFunction<T, T | D>;
17 export function last<T, S extends T>(
18   predicate: (value: T, index: number, source: Observable<T>) => value is S,
19   defaultValue?: S
20 ): OperatorFunction<T, S>;
21 export function last<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  * Returns an Observable that emits only the last item emitted by the source Observable.
29  * It optionally takes a predicate function as a parameter, in which case, rather than emitting
30  * the last item from the source Observable, the resulting Observable will emit the last item
31  * from the source Observable that satisfies the predicate.
32  *
33  * ![](last.png)
34  *
35  * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
36  * callback if the Observable completes before any `next` notification was sent.
37  * @param {function} [predicate] - The condition any source emitted item has to satisfy.
38  * @param {any} [defaultValue] - An optional default value to provide if last
39  * predicate isn't met or no values were emitted.
40  * @return {Observable} An Observable that emits only the last item satisfying the given condition
41  * from the source, or an NoSuchElementException if no such items are emitted.
42  * @throws - Throws if no items that match the predicate are emitted by the source Observable.
43  */
44 export function last<T, D>(
45   predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
46   defaultValue?: D
47 ): OperatorFunction<T, T | D> {
48   const hasDefaultValue = arguments.length >= 2;
49   return (source: Observable<T>) => source.pipe(
50     predicate ? filter((v, i) => predicate(v, i, source)) : identity,
51     takeLast(1),
52     hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
53   );
54 }