1 import { Observable } from '../Observable';
2 import { Operator } from '../Operator';
3 import { Observer, OperatorFunction } from '../types';
4 import { Subscriber } from '../Subscriber';
6 * Counts the number of emissions on the source and emits that number when the
9 * <span class="informal">Tells how many values were emitted, when the source
14 * `count` transforms an Observable that emits values into an Observable that
15 * emits a single value that represents the number of values emitted by the
16 * source Observable. If the source Observable terminates with an error, `count`
17 * will pass this error notification along without emitting a value first. If
18 * the source Observable does not terminate at all, `count` will neither emit
19 * a value nor terminate. This operator takes an optional `predicate` function
20 * as argument, in which case the output emission will represent the number of
21 * source values that matched `true` with the `predicate`.
25 * Counts how many seconds have passed before the first click happened
27 * import { fromEvent, interval } from 'rxjs';
28 * import { count, takeUntil } from 'rxjs/operators';
30 * const seconds = interval(1000);
31 * const clicks = fromEvent(document, 'click');
32 * const secondsBeforeClick = seconds.pipe(takeUntil(clicks));
33 * const result = secondsBeforeClick.pipe(count());
34 * result.subscribe(x => console.log(x));
37 * Counts how many odd numbers are there between 1 and 7
39 * import { range } from 'rxjs';
40 * import { count } from 'rxjs/operators';
42 * const numbers = range(1, 7);
43 * const result = numbers.pipe(count(i => i % 2 === 1));
44 * result.subscribe(x => console.log(x));
53 * @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
54 * boolean function to select what values are to be counted. It is provided with
56 * - `value`: the value from the source Observable.
57 * - `index`: the (zero-based) "index" of the value from the source Observable.
58 * - `source`: the source Observable instance itself.
59 * @return {Observable} An Observable of one number that represents the count as
65 export function count<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number> {
66 return (source: Observable<T>) => source.lift(new CountOperator(predicate, source));
69 class CountOperator<T> implements Operator<T, number> {
70 constructor(private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
71 private source?: Observable<T>) {
74 call(subscriber: Subscriber<number>, source: any): any {
75 return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
80 * We need this JSDoc comment for affecting ESDoc.
84 class CountSubscriber<T> extends Subscriber<T> {
85 private count: number = 0;
86 private index: number = 0;
88 constructor(destination: Observer<number>,
89 private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
90 private source?: Observable<T>) {
94 protected _next(value: T): void {
96 this._tryPredicate(value);
102 private _tryPredicate(value: T) {
106 result = this.predicate(value, this.index++, this.source);
108 this.destination.error(err);
117 protected _complete(): void {
118 this.destination.next(this.count);
119 this.destination.complete();