-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathexpand.ts
81 lines (76 loc) · 2.88 KB
/
expand.ts
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
import type { OperatorFunction, ObservableInput, ObservedValueOf } from '../types.js';
import { Observable } from '../Observable.js';
import { mergeInternals } from './mergeInternals.js';
export function expand<T, O extends ObservableInput<unknown>>(
project: (value: T, index: number) => O,
concurrent?: number
): OperatorFunction<T, ObservedValueOf<O>>;
/**
* 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 project A function that, when applied to an item emitted by the source
* or the output Observable, returns an Observable.
* @param concurrent Maximum number of input Observables being subscribed to
* concurrently.
* @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
): OperatorFunction<T, ObservedValueOf<O>> {
concurrent = (concurrent || 0) < 1 ? Infinity : concurrent;
return (source) =>
new Observable((subscriber) =>
mergeInternals(
// General merge params
source,
subscriber,
// HACK: Cast because TypeScript seems to get confused here.
project as (value: T, index: number) => ObservableInput<ObservedValueOf<O>>,
concurrent,
// onBeforeNext
undefined,
// Expand-specific
true // Use expand path
)
);
}