Debian Codegraph @

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

raw
1 import { Observable } from '../Observable';
2 import { Subscriber } from '../Subscriber';
3 import { OperatorFunction, TruthyTypesOf } from '../types';
4 import { operate } from '../util/lift';
5 import { createOperatorSubscriber } from './OperatorSubscriber';
6
7 export function find<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
8 /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
9 export function find<T, S extends T, A>(
10 predicate: (this: A, value: T, index: number, source: Observable<T>) => value is S,
11 thisArg: A
12 ): OperatorFunction<T, S | undefined>;
13 export function find<T, S extends T>(
14 predicate: (value: T, index: number, source: Observable<T>) => value is S
15 ): OperatorFunction<T, S | undefined>;
16 /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
17 export function find<T, A>(
18 predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
19 thisArg: A
20 ): OperatorFunction<T, T | undefined>;
21 export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, T | undefined>;
22 /**
23 * Emits only the first value emitted by the source Observable that meets some
24 * condition.
25 *
26 * <span class="informal">Finds the first value that passes some test and emits
27 * that.</span>
28 *
29 * ![](find.png)
30 *
31 * `find` searches for the first item in the source Observable that matches the
32 * specified condition embodied by the `predicate`, and returns the first
33 * occurrence in the source. Unlike {@link first}, the `predicate` is required
34 * in `find`, and does not emit an error if a valid value is not found
35 * (emits `undefined` instead).
36 *
37 * ## Example
38 *
39 * Find and emit the first click that happens on a DIV element
40 *
41 * ```ts
42 * import { fromEvent, find } from 'rxjs';
43 *
44 * const div = document.createElement('div');
45 * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
46 * document.body.appendChild(div);
47 *
48 * const clicks = fromEvent(document, 'click');
49 * const result = clicks.pipe(find(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
50 * result.subscribe(x => console.log(x));
51 * ```
52 *
53 * @see {@link filter}
54 * @see {@link first}
55 * @see {@link findIndex}
56 * @see {@link take}
57 *
58 * @param predicate A function called with each item to test for condition matching.
59 * @param thisArg An optional argument to determine the value of `this` in the
60 * `predicate` function.
61 * @return A function that returns an Observable that emits the first item that
62 * matches the condition.
63 */
64 export function find<T>(
65 predicate: (value: T, index: number, source: Observable<T>) => boolean,
66 thisArg?: any
67 ): OperatorFunction<T, T | undefined> {
68 return operate(createFind(predicate, thisArg, 'value'));
69 }
70
71 export function createFind<T>(
72 predicate: (value: T, index: number, source: Observable<T>) => boolean,
73 thisArg: any,
74 emit: 'value' | 'index'
75 ) {
76 const findIndex = emit === 'index';
77 return (source: Observable<T>, subscriber: Subscriber<any>) => {
78 let index = 0;
79 source.subscribe(
80 createOperatorSubscriber(
81 subscriber,
82 (value) => {
83 const i = index++;
84 if (predicate.call(thisArg, value, i, source)) {
85 subscriber.next(findIndex ? i : value);
86 subscriber.complete();
87 }
88 },
89 () => {
90 subscriber.next(findIndex ? -1 : undefined);
91 subscriber.complete();
92 }
93 )
94 );
95 };
96 }