Debian Codegraph @

node/rxjs/src/internal/operators/single.ts

raw
1 import { Observable } from '../Observable';
2 import { EmptyError } from '../util/EmptyError';
3
4 import { MonoTypeOperatorFunction, OperatorFunction, TruthyTypesOf } from '../types';
5 import { SequenceError } from '../util/SequenceError';
6 import { NotFoundError } from '../util/NotFoundError';
7 import { operate } from '../util/lift';
8 import { createOperatorSubscriber } from './OperatorSubscriber';
9
10 export function single<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
11 export function single<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): MonoTypeOperatorFunction<T>;
12
13 /**
14 * Returns an observable that asserts that only one value is
15 * emitted from the observable that matches the predicate. If no
16 * predicate is provided, then it will assert that the observable
17 * only emits one value.
18 *
19 * If the source Observable did not emit `next` before completion, it
20 * will emit an {@link EmptyError} to the Observer's `error` callback.
21 *
22 * In the event that two values are found that match the predicate,
23 * or when there are two values emitted and no predicate, it will
24 * emit a {@link SequenceError} to the Observer's `error` callback.
25 *
26 * In the event that no values match the predicate, if one is provided,
27 * it will emit a {@link NotFoundError} to the Observer's `error` callback.
28 *
29 * ## Example
30 *
31 * Expect only `name` beginning with `'B'`
32 *
33 * ```ts
34 * import { of, single } from 'rxjs';
35 *
36 * const source1 = of(
37 * { name: 'Ben' },
38 * { name: 'Tracy' },
39 * { name: 'Laney' },
40 * { name: 'Lily' }
41 * );
42 *
43 * source1
44 * .pipe(single(x => x.name.startsWith('B')))
45 * .subscribe(x => console.log(x));
46 * // Emits 'Ben'
47 *
48 *
49 * const source2 = of(
50 * { name: 'Ben' },
51 * { name: 'Tracy' },
52 * { name: 'Bradley' },
53 * { name: 'Lincoln' }
54 * );
55 *
56 * source2
57 * .pipe(single(x => x.name.startsWith('B')))
58 * .subscribe({ error: err => console.error(err) });
59 * // Error emitted: SequenceError('Too many values match')
60 *
61 *
62 * const source3 = of(
63 * { name: 'Laney' },
64 * { name: 'Tracy' },
65 * { name: 'Lily' },
66 * { name: 'Lincoln' }
67 * );
68 *
69 * source3
70 * .pipe(single(x => x.name.startsWith('B')))
71 * .subscribe({ error: err => console.error(err) });
72 * // Error emitted: NotFoundError('No values match')
73 * ```
74 *
75 * @see {@link first}
76 * @see {@link find}
77 * @see {@link findIndex}
78 * @see {@link elementAt}
79 *
80 * @throws {NotFoundError} Delivers a `NotFoundError` to the Observer's `error`
81 * callback if the Observable completes before any `next` notification was sent.
82 * @throws {SequenceError} Delivers a `SequenceError` if more than one value is
83 * emitted that matches the provided predicate. If no predicate is provided, it
84 * will deliver a `SequenceError` if more than one value comes from the source.
85 * @throws {EmptyError} Delivers an `EmptyError` if no values were `next`ed prior
86 * to completion.
87 *
88 * @param predicate A predicate function to evaluate items emitted by the source
89 * Observable.
90 * @return A function that returns an Observable that emits the single item
91 * emitted by the source Observable that matches the predicate.
92 */
93 export function single<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): MonoTypeOperatorFunction<T> {
94 return operate((source, subscriber) => {
95 let hasValue = false;
96 let singleValue: T;
97 let seenValue = false;
98 let index = 0;
99 source.subscribe(
100 createOperatorSubscriber(
101 subscriber,
102 (value) => {
103 seenValue = true;
104 if (!predicate || predicate(value, index++, source)) {
105 hasValue && subscriber.error(new SequenceError('Too many matching values'));
106 hasValue = true;
107 singleValue = value;
108 }
109 },
110 () => {
111 if (hasValue) {
112 subscriber.next(singleValue);
113 subscriber.complete();
114 } else {
115 subscriber.error(seenValue ? new NotFoundError('No matching values') : new EmptyError());
116 }
117 }
118 )
119 );
120 });
121 }