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