source: trip-planner-front/node_modules/rxjs/src/internal/operators/last.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.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 { takeLast } from './takeLast';
8import { throwIfEmpty } from './throwIfEmpty';
9import { defaultIfEmpty } from './defaultIfEmpty';
10import { identity } from '../util/identity';
11
12/* tslint:disable:max-line-length */
13export function last<T, D = T>(
14 predicate?: null,
15 defaultValue?: D
16): OperatorFunction<T, T | D>;
17export 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>;
21export 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 */
44export 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}
Note: See TracBrowser for help on using the repository browser.