[6a3a178] | 1 | import { Operator } from '../Operator';
|
---|
| 2 | import { Observable } from '../Observable';
|
---|
| 3 | import { Subscriber } from '../Subscriber';
|
---|
| 4 | import { OperatorFunction, MonoTypeOperatorFunction } from '../types';
|
---|
| 5 |
|
---|
| 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 */
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Applies an accumulator function over the source Observable, and returns each
|
---|
| 14 | * intermediate result, with an optional seed value.
|
---|
| 15 | *
|
---|
| 16 | * <span class="informal">It's like {@link reduce}, but emits the current
|
---|
| 17 | * accumulation whenever the source emits a value.</span>
|
---|
| 18 | *
|
---|
| 19 | * ![](scan.png)
|
---|
| 20 | *
|
---|
| 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
|
---|
| 24 | * accumulations.
|
---|
| 25 | *
|
---|
| 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.
|
---|
| 30 | *
|
---|
| 31 | * ## Example
|
---|
| 32 | * Count the number of click events
|
---|
| 33 | * ```ts
|
---|
| 34 | * import { fromEvent } from 'rxjs';
|
---|
| 35 | * import { scan, mapTo } from 'rxjs/operators';
|
---|
| 36 | *
|
---|
| 37 | * const clicks = fromEvent(document, 'click');
|
---|
| 38 | * const ones = clicks.pipe(mapTo(1));
|
---|
| 39 | * const seed = 0;
|
---|
| 40 | * const count = ones.pipe(scan((acc, one) => acc + one, seed));
|
---|
| 41 | * count.subscribe(x => console.log(x));
|
---|
| 42 | * ```
|
---|
| 43 | *
|
---|
| 44 | * @see {@link expand}
|
---|
| 45 | * @see {@link mergeScan}
|
---|
| 46 | * @see {@link reduce}
|
---|
| 47 | *
|
---|
| 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.
|
---|
| 52 | * @method scan
|
---|
| 53 | * @owner Observable
|
---|
| 54 | */
|
---|
| 55 | export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed?: T | R): OperatorFunction<T, R> {
|
---|
| 56 | let hasSeed = false;
|
---|
| 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) {
|
---|
| 63 | hasSeed = true;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return function scanOperatorFunction(source: Observable<T>): Observable<R> {
|
---|
| 67 | return source.lift(new ScanOperator(accumulator, seed, hasSeed));
|
---|
| 68 | };
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 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) {}
|
---|
| 73 |
|
---|
| 74 | call(subscriber: Subscriber<R>, source: any): any {
|
---|
| 75 | return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * We need this JSDoc comment for affecting ESDoc.
|
---|
| 81 | * @ignore
|
---|
| 82 | * @extends {Ignored}
|
---|
| 83 | */
|
---|
| 84 | class ScanSubscriber<T, R> extends Subscriber<T> {
|
---|
| 85 | private index: number = 0;
|
---|
| 86 |
|
---|
| 87 | get seed(): T | R {
|
---|
| 88 | return this._seed;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | set seed(value: T | R) {
|
---|
| 92 | this.hasSeed = true;
|
---|
| 93 | this._seed = value;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | constructor(destination: Subscriber<R>, private accumulator: (acc: R, value: T, index: number) => R, private _seed: T | R,
|
---|
| 97 | private hasSeed: boolean) {
|
---|
| 98 | super(destination);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | protected _next(value: T): void {
|
---|
| 102 | if (!this.hasSeed) {
|
---|
| 103 | this.seed = value;
|
---|
| 104 | this.destination.next(value);
|
---|
| 105 | } else {
|
---|
| 106 | return this._tryNext(value);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private _tryNext(value: T): void {
|
---|
| 111 | const index = this.index++;
|
---|
| 112 | let result: any;
|
---|
| 113 | try {
|
---|
| 114 | result = this.accumulator(<R>this.seed, value, index);
|
---|
| 115 | } catch (err) {
|
---|
| 116 | this.destination.error(err);
|
---|
| 117 | }
|
---|
| 118 | this.seed = result;
|
---|
| 119 | this.destination.next(result);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|