1 |
|
---|
2 | import { mergeMap } from './mergeMap';
|
---|
3 | import { identity } from '../util/identity';
|
---|
4 | import { OperatorFunction, ObservableInput } from '../types';
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Converts a higher-order Observable into a first-order Observable which
|
---|
8 | * concurrently delivers all values that are emitted on the inner Observables.
|
---|
9 | *
|
---|
10 | * <span class="informal">Flattens an Observable-of-Observables.</span>
|
---|
11 | *
|
---|
12 | * ![](mergeAll.png)
|
---|
13 | *
|
---|
14 | * `mergeAll` subscribes to an Observable that emits Observables, also known as
|
---|
15 | * a higher-order Observable. Each time it observes one of these emitted inner
|
---|
16 | * Observables, it subscribes to that and delivers all the values from the
|
---|
17 | * inner Observable on the output Observable. The output Observable only
|
---|
18 | * completes once all inner Observables have completed. Any error delivered by
|
---|
19 | * a inner Observable will be immediately emitted on the output Observable.
|
---|
20 | *
|
---|
21 | * ## Examples
|
---|
22 | * Spawn a new interval Observable for each click event, and blend their outputs as one Observable
|
---|
23 | * ```ts
|
---|
24 | * import { fromEvent, interval } from 'rxjs';
|
---|
25 | * import { map, mergeAll } from 'rxjs/operators';
|
---|
26 | *
|
---|
27 | * const clicks = fromEvent(document, 'click');
|
---|
28 | * const higherOrder = clicks.pipe(map((ev) => interval(1000)));
|
---|
29 | * const firstOrder = higherOrder.pipe(mergeAll());
|
---|
30 | * firstOrder.subscribe(x => console.log(x));
|
---|
31 | * ```
|
---|
32 | *
|
---|
33 | * Count from 0 to 9 every second for each click, but only allow 2 concurrent timers
|
---|
34 | * ```ts
|
---|
35 | * import { fromEvent, interval } from 'rxjs';
|
---|
36 | * import { take, map, mergeAll } from 'rxjs/operators';
|
---|
37 | *
|
---|
38 | * const clicks = fromEvent(document, 'click');
|
---|
39 | * const higherOrder = clicks.pipe(
|
---|
40 | * map((ev) => interval(1000).pipe(take(10))),
|
---|
41 | * );
|
---|
42 | * const firstOrder = higherOrder.pipe(mergeAll(2));
|
---|
43 | * firstOrder.subscribe(x => console.log(x));
|
---|
44 | * ```
|
---|
45 | *
|
---|
46 | * @see {@link combineAll}
|
---|
47 | * @see {@link concatAll}
|
---|
48 | * @see {@link exhaust}
|
---|
49 | * @see {@link merge}
|
---|
50 | * @see {@link mergeMap}
|
---|
51 | * @see {@link mergeMapTo}
|
---|
52 | * @see {@link mergeScan}
|
---|
53 | * @see {@link switchAll}
|
---|
54 | * @see {@link switchMap}
|
---|
55 | * @see {@link zipAll}
|
---|
56 | *
|
---|
57 | * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of inner
|
---|
58 | * Observables being subscribed to concurrently.
|
---|
59 | * @return {Observable} An Observable that emits values coming from all the
|
---|
60 | * inner Observables emitted by the source Observable.
|
---|
61 | * @method mergeAll
|
---|
62 | * @owner Observable
|
---|
63 | */
|
---|
64 | export function mergeAll<T>(concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<ObservableInput<T>, T> {
|
---|
65 | return mergeMap(identity, concurrent);
|
---|
66 | }
|
---|