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