1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Observer, OperatorFunction } from '../types';
7 * Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
10 * A simple example emitting true if all elements are less than 5, false otherwise
12 * import { of } from 'rxjs';
13 * import { every } from 'rxjs/operators';
15 * of(1, 2, 3, 4, 5, 6).pipe(
18 * .subscribe(x => console.log(x)); // -> false
21 * @param {function} predicate A function for determining if an item meets a specified condition.
22 * @param {any} [thisArg] Optional object to use for `this` in the callback.
23 * @return {Observable} An Observable of booleans that determines if all items of the source Observable meet the condition specified.
27 export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
28 thisArg?: any): OperatorFunction<T, boolean> {
29 return (source: Observable<T>) => source.lift(new EveryOperator(predicate, thisArg, source));
32 class EveryOperator<T> implements Operator<T, boolean> {
33 constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
34 private thisArg?: any,
35 private source?: Observable<T>) {
38 call(observer: Subscriber<boolean>, source: any): any {
39 return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
44 * We need this JSDoc comment for affecting ESDoc.
48 class EverySubscriber<T> extends Subscriber<T> {
49 private index: number = 0;
51 constructor(destination: Observer<boolean>,
52 private predicate: (value: T, index: number, source: Observable<T>) => boolean,
54 private source?: Observable<T>) {
56 this.thisArg = thisArg || this;
59 private notifyComplete(everyValueMatch: boolean): void {
60 this.destination.next(everyValueMatch);
61 this.destination.complete();
64 protected _next(value: T): void {
67 result = this.predicate.call(this.thisArg, value, this.index++, this.source);
69 this.destination.error(err);
74 this.notifyComplete(false);
78 protected _complete(): void {
79 this.notifyComplete(true);