Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / find.ts
1 import {Observable} from '../Observable';
2 import {Operator} from '../Operator';
3 import {Subscriber} from '../Subscriber';
4 import {OperatorFunction} from '../types';
5
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>;
10 /**
11  * Emits only the first value emitted by the source Observable that meets some
12  * condition.
13  *
14  * <span class="informal">Finds the first value that passes some test and emits
15  * that.</span>
16  *
17  * ![](find.png)
18  *
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.
23  *
24  * ## Example
25  * Find and emit the first click that happens on a DIV element
26  * ```ts
27  * import { fromEvent } from 'rxjs';
28  * import { find } from 'rxjs/operators';
29  *
30  * const clicks = fromEvent(document, 'click');
31  * const result = clicks.pipe(find(ev => ev.target.tagName === 'DIV'));
32  * result.subscribe(x => console.log(x));
33  * ```
34  *
35  * @see {@link filter}
36  * @see {@link first}
37  * @see {@link findIndex}
38  * @see {@link take}
39  *
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
45  * condition.
46  * @method find
47  * @owner Observable
48  */
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');
53   }
54   return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, false, thisArg)) as Observable<T | undefined>;
55 }
56
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) {
62   }
63
64   call(observer: Subscriber<T>, source: any): any {
65     return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
66   }
67 }
68
69 /**
70  * We need this JSDoc comment for affecting ESDoc.
71  * @ignore
72  * @extends {Ignored}
73  */
74 export class FindValueSubscriber<T> extends Subscriber<T> {
75   private index: number = 0;
76
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) {
82     super(destination);
83   }
84
85   private notifyComplete(value: any): void {
86     const destination = this.destination;
87
88     destination.next(value);
89     destination.complete();
90     this.unsubscribe();
91   }
92
93   protected _next(value: T): void {
94     const {predicate, thisArg} = this;
95     const index = this.index++;
96     try {
97       const result = predicate.call(thisArg || this, value, index, this.source);
98       if (result) {
99         this.notifyComplete(this.yieldIndex ? index : value);
100       }
101     } catch (err) {
102       this.destination.error(err);
103     }
104   }
105
106   protected _complete(): void {
107     this.notifyComplete(this.yieldIndex ? -1 : undefined);
108   }
109 }