Debian Codegraph @

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

raw
1 import { OperatorFunction, ObservableInput, ObservedValueOf, SchedulerLike } from '../types';
2 import { operate } from '../util/lift';
3 import { mergeInternals } from './mergeInternals';
4
5 /* tslint:disable:max-line-length */
6 export function expand<T, O extends ObservableInput<unknown>>(
7 project: (value: T, index: number) => O,
8 concurrent?: number,
9 scheduler?: SchedulerLike
10 ): OperatorFunction<T, ObservedValueOf<O>>;
11 /**
12 * @deprecated The `scheduler` parameter will be removed in v8. If you need to schedule the inner subscription,
13 * use `subscribeOn` within the projection function: `expand((value) => fn(value).pipe(subscribeOn(scheduler)))`.
14 * Details: Details: https://rxjs.dev/deprecations/scheduler-argument
15 */
16 export function expand<T, O extends ObservableInput<unknown>>(
17 project: (value: T, index: number) => O,
18 concurrent: number | undefined,
19 scheduler: SchedulerLike
20 ): OperatorFunction<T, ObservedValueOf<O>>;
21 /* tslint:enable:max-line-length */
22
23 /**
24 * Recursively projects each source value to an Observable which is merged in
25 * the output Observable.
26 *
27 * <span class="informal">It's similar to {@link mergeMap}, but applies the
28 * projection function to every source value as well as every output value.
29 * It's recursive.</span>
30 *
31 * ![](expand.png)
32 *
33 * Returns an Observable that emits items based on applying a function that you
34 * supply to each item emitted by the source Observable, where that function
35 * returns an Observable, and then merging those resulting Observables and
36 * emitting the results of this merger. *Expand* will re-emit on the output
37 * Observable every source value. Then, each output value is given to the
38 * `project` function which returns an inner Observable to be merged on the
39 * output Observable. Those output values resulting from the projection are also
40 * given to the `project` function to produce new output values. This is how
41 * *expand* behaves recursively.
42 *
43 * ## Example
44 *
45 * Start emitting the powers of two on every click, at most 10 of them
46 *
47 * ```ts
48 * import { fromEvent, map, expand, of, delay, take } from 'rxjs';
49 *
50 * const clicks = fromEvent(document, 'click');
51 * const powersOfTwo = clicks.pipe(
52 * map(() => 1),
53 * expand(x => of(2 * x).pipe(delay(1000))),
54 * take(10)
55 * );
56 * powersOfTwo.subscribe(x => console.log(x));
57 * ```
58 *
59 * @see {@link mergeMap}
60 * @see {@link mergeScan}
61 *
62 * @param project A function that, when applied to an item emitted by the source
63 * or the output Observable, returns an Observable.
64 * @param concurrent Maximum number of input Observables being subscribed to
65 * concurrently.
66 * @param scheduler The {@link SchedulerLike} to use for subscribing to
67 * each projected inner Observable.
68 * @return A function that returns an Observable that emits the source values
69 * and also result of applying the projection function to each value emitted on
70 * the output Observable and merging the results of the Observables obtained
71 * from this transformation.
72 */
73 export function expand<T, O extends ObservableInput<unknown>>(
74 project: (value: T, index: number) => O,
75 concurrent = Infinity,
76 scheduler?: SchedulerLike
77 ): OperatorFunction<T, ObservedValueOf<O>> {
78 concurrent = (concurrent || 0) < 1 ? Infinity : concurrent;
79 return operate((source, subscriber) =>
80 mergeInternals(
81 // General merge params
82 source,
83 subscriber,
84 project,
85 concurrent,
86
87 // onBeforeNext
88 undefined,
89
90 // Expand-specific
91 true, // Use expand path
92 scheduler // Inner subscription scheduler
93 )
94 );
95 }