[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { Operator } from '../Operator';
|
---|
| 3 | import { Observer, OperatorFunction } from '../types';
|
---|
| 4 | import { Subscriber } from '../Subscriber';
|
---|
| 5 | /**
|
---|
| 6 | * Counts the number of emissions on the source and emits that number when the
|
---|
| 7 | * source completes.
|
---|
| 8 | *
|
---|
| 9 | * <span class="informal">Tells how many values were emitted, when the source
|
---|
| 10 | * completes.</span>
|
---|
| 11 | *
|
---|
| 12 | * ![](count.png)
|
---|
| 13 | *
|
---|
| 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`.
|
---|
| 22 | *
|
---|
| 23 | * ## Examples
|
---|
| 24 | *
|
---|
| 25 | * Counts how many seconds have passed before the first click happened
|
---|
| 26 | * ```ts
|
---|
| 27 | * import { fromEvent, interval } from 'rxjs';
|
---|
| 28 | * import { count, takeUntil } from 'rxjs/operators';
|
---|
| 29 | *
|
---|
| 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));
|
---|
| 35 | * ```
|
---|
| 36 | *
|
---|
| 37 | * Counts how many odd numbers are there between 1 and 7
|
---|
| 38 | * ```ts
|
---|
| 39 | * import { range } from 'rxjs';
|
---|
| 40 | * import { count } from 'rxjs/operators';
|
---|
| 41 | *
|
---|
| 42 | * const numbers = range(1, 7);
|
---|
| 43 | * const result = numbers.pipe(count(i => i % 2 === 1));
|
---|
| 44 | * result.subscribe(x => console.log(x));
|
---|
| 45 | * // Results in:
|
---|
| 46 | * // 4
|
---|
| 47 | * ```
|
---|
| 48 | *
|
---|
| 49 | * @see {@link max}
|
---|
| 50 | * @see {@link min}
|
---|
| 51 | * @see {@link reduce}
|
---|
| 52 | *
|
---|
| 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
|
---|
| 55 | * arguments of:
|
---|
| 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
|
---|
| 60 | * described above.
|
---|
| 61 | * @method count
|
---|
| 62 | * @owner Observable
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 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));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 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>) {
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | call(subscriber: Subscriber<number>, source: any): any {
|
---|
| 75 | return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * We need this JSDoc comment for affecting ESDoc.
|
---|
| 81 | * @ignore
|
---|
| 82 | * @extends {Ignored}
|
---|
| 83 | */
|
---|
| 84 | class CountSubscriber<T> extends Subscriber<T> {
|
---|
| 85 | private count: number = 0;
|
---|
| 86 | private index: number = 0;
|
---|
| 87 |
|
---|
| 88 | constructor(destination: Observer<number>,
|
---|
| 89 | private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
|
---|
| 90 | private source?: Observable<T>) {
|
---|
| 91 | super(destination);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | protected _next(value: T): void {
|
---|
| 95 | if (this.predicate) {
|
---|
| 96 | this._tryPredicate(value);
|
---|
| 97 | } else {
|
---|
| 98 | this.count++;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private _tryPredicate(value: T) {
|
---|
| 103 | let result: any;
|
---|
| 104 |
|
---|
| 105 | try {
|
---|
| 106 | result = this.predicate(value, this.index++, this.source);
|
---|
| 107 | } catch (err) {
|
---|
| 108 | this.destination.error(err);
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (result) {
|
---|
| 113 | this.count++;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | protected _complete(): void {
|
---|
| 118 | this.destination.next(this.count);
|
---|
| 119 | this.destination.complete();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|