Debian Codegraph @

node/rxjs/src/internal/operators/scan.ts

raw
1 import { OperatorFunction } from '../types';
2 import { operate } from '../util/lift';
3 import { scanInternals } from './scanInternals';
4
5 export function scan<V, A = V>(accumulator: (acc: A | V, value: V, index: number) => A): OperatorFunction<V, V | A>;
6 export function scan<V, A>(accumulator: (acc: A, value: V, index: number) => A, seed: A): OperatorFunction<V, A>;
7 export function scan<V, A, S>(accumulator: (acc: A | S, value: V, index: number) => A, seed: S): OperatorFunction<V, A>;
8
9 // TODO: link to a "redux pattern" section in the guide (location TBD)
10
11 /**
12 * Useful for encapsulating and managing state. Applies an accumulator (or "reducer function")
13 * to each value from the source after an initial state is established -- either via
14 * a `seed` value (second argument), or from the first value from the source.
15 *
16 * <span class="informal">It's like {@link reduce}, but emits the current
17 * accumulation state after each update</span>
18 *
19 * ![](scan.png)
20 *
21 * This operator maintains an internal state and emits it after processing each value as follows:
22 *
23 * 1. First value arrives
24 * - If a `seed` value was supplied (as the second argument to `scan`), let `state = seed` and `value = firstValue`.
25 * - If NO `seed` value was supplied (no second argument), let `state = firstValue` and go to 3.
26 * 2. Let `state = accumulator(state, value)`.
27 * - If an error is thrown by `accumulator`, notify the consumer of an error. The process ends.
28 * 3. Emit `state`.
29 * 4. Next value arrives, let `value = nextValue`, go to 2.
30 *
31 * ## Examples
32 *
33 * An average of previous numbers. This example shows how
34 * not providing a `seed` can prime the stream with the
35 * first value from the source.
36 *
37 * ```ts
38 * import { of, scan, map } from 'rxjs';
39 *
40 * const numbers$ = of(1, 2, 3);
41 *
42 * numbers$
43 * .pipe(
44 * // Get the sum of the numbers coming in.
45 * scan((total, n) => total + n),
46 * // Get the average by dividing the sum by the total number
47 * // received so far (which is 1 more than the zero-based index).
48 * map((sum, index) => sum / (index + 1))
49 * )
50 * .subscribe(console.log);
51 * ```
52 *
53 * The Fibonacci sequence. This example shows how you can use
54 * a seed to prime accumulation process. Also... you know... Fibonacci.
55 * So important to like, computers and stuff that its whiteboarded
56 * in job interviews. Now you can show them the Rx version! (Please don't, haha)
57 *
58 * ```ts
59 * import { interval, scan, map, startWith } from 'rxjs';
60 *
61 * const firstTwoFibs = [0, 1];
62 * // An endless stream of Fibonacci numbers.
63 * const fibonacci$ = interval(1000).pipe(
64 * // Scan to get the fibonacci numbers (after 0, 1)
65 * scan(([a, b]) => [b, a + b], firstTwoFibs),
66 * // Get the second number in the tuple, it's the one you calculated
67 * map(([, n]) => n),
68 * // Start with our first two digits :)
69 * startWith(...firstTwoFibs)
70 * );
71 *
72 * fibonacci$.subscribe(console.log);
73 * ```
74 *
75 * @see {@link expand}
76 * @see {@link mergeScan}
77 * @see {@link reduce}
78 * @see {@link switchScan}
79 *
80 * @param accumulator A "reducer function". This will be called for each value after an initial state is
81 * acquired.
82 * @param seed The initial state. If this is not provided, the first value from the source will
83 * be used as the initial state, and emitted without going through the accumulator. All subsequent values
84 * will be processed by the accumulator function. If this is provided, all values will go through
85 * the accumulator function.
86 * @return A function that returns an Observable of the accumulated values.
87 */
88 export function scan<V, A, S>(accumulator: (acc: V | A | S, value: V, index: number) => A, seed?: S): OperatorFunction<V, V | A> {
89 // providing a seed of `undefined` *should* be valid and trigger
90 // hasSeed! so don't use `seed !== undefined` checks!
91 // For this reason, we have to check it here at the original call site
92 // otherwise inside Operator/Subscriber we won't know if `undefined`
93 // means they didn't provide anything or if they literally provided `undefined`
94 return operate(scanInternals(accumulator, seed as S, arguments.length >= 2, true));
95 }