[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { scan } from './scan';
|
---|
| 3 | import { takeLast } from './takeLast';
|
---|
| 4 | import { defaultIfEmpty } from './defaultIfEmpty';
|
---|
| 5 | import { OperatorFunction, MonoTypeOperatorFunction } from '../types';
|
---|
| 6 | import { pipe } from '../util/pipe';
|
---|
| 7 |
|
---|
| 8 | /* tslint:disable:max-line-length */
|
---|
| 9 | export function reduce<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed: R): OperatorFunction<T, R>;
|
---|
| 10 | export function reduce<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;
|
---|
| 11 | export function reduce<T, R>(accumulator: (acc: R, value: T, index: number) => R): OperatorFunction<T, R>;
|
---|
| 12 | /* tslint:enable:max-line-length */
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Applies an accumulator function over the source Observable, and returns the
|
---|
| 16 | * accumulated result when the source completes, given an optional seed value.
|
---|
| 17 | *
|
---|
| 18 | * <span class="informal">Combines together all values emitted on the source,
|
---|
| 19 | * using an accumulator function that knows how to join a new source value into
|
---|
| 20 | * the accumulation from the past.</span>
|
---|
| 21 | *
|
---|
| 22 | * ![](reduce.png)
|
---|
| 23 | *
|
---|
| 24 | * Like
|
---|
| 25 | * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
|
---|
| 26 | * `reduce` applies an `accumulator` function against an accumulation and each
|
---|
| 27 | * value of the source Observable (from the past) to reduce it to a single
|
---|
| 28 | * value, emitted on the output Observable. Note that `reduce` will only emit
|
---|
| 29 | * one value, only when the source Observable completes. It is equivalent to
|
---|
| 30 | * applying operator {@link scan} followed by operator {@link last}.
|
---|
| 31 | *
|
---|
| 32 | * Returns an Observable that applies a specified `accumulator` function to each
|
---|
| 33 | * item emitted by the source Observable. If a `seed` value is specified, then
|
---|
| 34 | * that value will be used as the initial value for the accumulator. If no seed
|
---|
| 35 | * value is specified, the first item of the source is used as the seed.
|
---|
| 36 | *
|
---|
| 37 | * ## Example
|
---|
| 38 | * Count the number of click events that happened in 5 seconds
|
---|
| 39 | * ```ts
|
---|
| 40 | * import { fromEvent, interval } from 'rxjs';
|
---|
| 41 | * import { reduce, takeUntil, mapTo } from 'rxjs/operators';
|
---|
| 42 | *
|
---|
| 43 | * const clicksInFiveSeconds = fromEvent(document, 'click').pipe(
|
---|
| 44 | * takeUntil(interval(5000)),
|
---|
| 45 | * );
|
---|
| 46 | * const ones = clicksInFiveSeconds.pipe(mapTo(1));
|
---|
| 47 | * const seed = 0;
|
---|
| 48 | * const count = ones.pipe(reduce((acc, one) => acc + one, seed));
|
---|
| 49 | * count.subscribe(x => console.log(x));
|
---|
| 50 | * ```
|
---|
| 51 | *
|
---|
| 52 | * @see {@link count}
|
---|
| 53 | * @see {@link expand}
|
---|
| 54 | * @see {@link mergeScan}
|
---|
| 55 | * @see {@link scan}
|
---|
| 56 | *
|
---|
| 57 | * @param {function(acc: R, value: T, index: number): R} accumulator The accumulator function
|
---|
| 58 | * called on each source value.
|
---|
| 59 | * @param {R} [seed] The initial accumulation value.
|
---|
| 60 | * @return {Observable<R>} An Observable that emits a single value that is the
|
---|
| 61 | * result of accumulating the values emitted by the source Observable.
|
---|
| 62 | * @method reduce
|
---|
| 63 | * @owner Observable
|
---|
| 64 | */
|
---|
| 65 | export function reduce<T, R>(accumulator: (acc: T | R, value: T, index?: number) => T | R, seed?: T | R): OperatorFunction<T, T | R> {
|
---|
| 66 | // providing a seed of `undefined` *should* be valid and trigger
|
---|
| 67 | // hasSeed! so don't use `seed !== undefined` checks!
|
---|
| 68 | // For this reason, we have to check it here at the original call site
|
---|
| 69 | // otherwise inside Operator/Subscriber we won't know if `undefined`
|
---|
| 70 | // means they didn't provide anything or if they literally provided `undefined`
|
---|
| 71 | if (arguments.length >= 2) {
|
---|
| 72 | return function reduceOperatorFunctionWithSeed(source: Observable<T>): Observable<T | R> {
|
---|
| 73 | return pipe(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);
|
---|
| 74 | };
|
---|
| 75 | }
|
---|
| 76 | return function reduceOperatorFunction(source: Observable<T>): Observable<T | R> {
|
---|
| 77 | return pipe(
|
---|
| 78 | scan<T, T | R>((acc, value, index) => accumulator(acc, value, index + 1)),
|
---|
| 79 | takeLast(1),
|
---|
| 80 | )(source);
|
---|
| 81 | };
|
---|
| 82 | }
|
---|