1 import {Observable} from '../Observable';
2 import {Operator} from '../Operator';
3 import {Subscriber} from '../Subscriber';
4 import {OperatorFunction} from '../types';
6 export function find<T, S extends T>(predicate: (value: T, index: number, source: Observable<T>) => value is S,
7 thisArg?: any): OperatorFunction<T, S | undefined>;
8 export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
9 thisArg?: any): OperatorFunction<T, T | undefined>;
11 * Emits only the first value emitted by the source Observable that meets some
14 * <span class="informal">Finds the first value that passes some test and emits
19 * `find` searches for the first item in the source Observable that matches the
20 * specified condition embodied by the `predicate`, and returns the first
21 * occurrence in the source. Unlike {@link first}, the `predicate` is required
22 * in `find`, and does not emit an error if a valid value is not found.
25 * Find and emit the first click that happens on a DIV element
27 * import { fromEvent } from 'rxjs';
28 * import { find } from 'rxjs/operators';
30 * const clicks = fromEvent(document, 'click');
31 * const result = clicks.pipe(find(ev => ev.target.tagName === 'DIV'));
32 * result.subscribe(x => console.log(x));
37 * @see {@link findIndex}
40 * @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
41 * A function called with each item to test for condition matching.
42 * @param {any} [thisArg] An optional argument to determine the value of `this`
43 * in the `predicate` function.
44 * @return {Observable<T>} An Observable of the first item that matches the
49 export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
50 thisArg?: any): OperatorFunction<T, T | undefined> {
51 if (typeof predicate !== 'function') {
52 throw new TypeError('predicate is not a function');
54 return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, false, thisArg)) as Observable<T | undefined>;
57 export class FindValueOperator<T> implements Operator<T, T | number | undefined> {
58 constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
59 private source: Observable<T>,
60 private yieldIndex: boolean,
61 private thisArg?: any) {
64 call(observer: Subscriber<T>, source: any): any {
65 return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
70 * We need this JSDoc comment for affecting ESDoc.
74 export class FindValueSubscriber<T> extends Subscriber<T> {
75 private index: number = 0;
77 constructor(destination: Subscriber<T>,
78 private predicate: (value: T, index: number, source: Observable<T>) => boolean,
79 private source: Observable<T>,
80 private yieldIndex: boolean,
81 private thisArg?: any) {
85 private notifyComplete(value: any): void {
86 const destination = this.destination;
88 destination.next(value);
89 destination.complete();
93 protected _next(value: T): void {
94 const {predicate, thisArg} = this;
95 const index = this.index++;
97 const result = predicate.call(thisArg || this, value, index, this.source);
99 this.notifyComplete(this.yieldIndex ? index : value);
102 this.destination.error(err);
106 protected _complete(): void {
107 this.notifyComplete(this.yieldIndex ? -1 : undefined);