1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { OperatorFunction, MonoTypeOperatorFunction } from '../types';
6 /* tslint:disable:max-line-length */
7 export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed: R): OperatorFunction<T, R>;
8 export function scan<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;
9 export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R): OperatorFunction<T, R>;
10 /* tslint:enable:max-line-length */
13 * Applies an accumulator function over the source Observable, and returns each
14 * intermediate result, with an optional seed value.
16 * <span class="informal">It's like {@link reduce}, but emits the current
17 * accumulation whenever the source emits a value.</span>
21 * Combines together all values emitted on the source, using an accumulator
22 * function that knows how to join a new source value into the accumulation from
23 * the past. Is similar to {@link reduce}, but emits the intermediate
26 * Returns an Observable that applies a specified `accumulator` function to each
27 * item emitted by the source Observable. If a `seed` value is specified, then
28 * that value will be used as the initial value for the accumulator. If no seed
29 * value is specified, the first item of the source is used as the seed.
32 * Count the number of click events
34 * import { fromEvent } from 'rxjs';
35 * import { scan, mapTo } from 'rxjs/operators';
37 * const clicks = fromEvent(document, 'click');
38 * const ones = clicks.pipe(mapTo(1));
40 * const count = ones.pipe(scan((acc, one) => acc + one, seed));
41 * count.subscribe(x => console.log(x));
45 * @see {@link mergeScan}
48 * @param {function(acc: R, value: T, index: number): R} accumulator
49 * The accumulator function called on each source value.
50 * @param {T|R} [seed] The initial accumulation value.
51 * @return {Observable<R>} An observable of the accumulated values.
55 export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed?: T | R): OperatorFunction<T, R> {
57 // providing a seed of `undefined` *should* be valid and trigger
58 // hasSeed! so don't use `seed !== undefined` checks!
59 // For this reason, we have to check it here at the original call site
60 // otherwise inside Operator/Subscriber we won't know if `undefined`
61 // means they didn't provide anything or if they literally provided `undefined`
62 if (arguments.length >= 2) {
66 return function scanOperatorFunction(source: Observable<T>): Observable<R> {
67 return source.lift(new ScanOperator(accumulator, seed, hasSeed));
71 class ScanOperator<T, R> implements Operator<T, R> {
72 constructor(private accumulator: (acc: R, value: T, index: number) => R, private seed?: T | R, private hasSeed: boolean = false) {}
74 call(subscriber: Subscriber<R>, source: any): any {
75 return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
80 * We need this JSDoc comment for affecting ESDoc.
84 class ScanSubscriber<T, R> extends Subscriber<T> {
85 private index: number = 0;
91 set seed(value: T | R) {
96 constructor(destination: Subscriber<R>, private accumulator: (acc: R, value: T, index: number) => R, private _seed: T | R,
97 private hasSeed: boolean) {
101 protected _next(value: T): void {
104 this.destination.next(value);
106 return this._tryNext(value);
110 private _tryNext(value: T): void {
111 const index = this.index++;
114 result = this.accumulator(<R>this.seed, value, index);
116 this.destination.error(err);
119 this.destination.next(result);