| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { ObservableInput, ObservableInputTuple, SchedulerLike } from '../types'; |
| 3 |
import { mergeAll } from '../operators/mergeAll'; |
| 4 |
import { innerFrom } from './innerFrom'; |
| 5 |
import { EMPTY } from './empty'; |
| 6 |
import { popNumber, popScheduler } from '../util/args'; |
| 7 |
import { from } from './from'; |
| 8 |
|
| 9 |
export function merge<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A[number]>; |
| 10 |
export function merge<A extends readonly unknown[]>(...sourcesAndConcurrency: [...ObservableInputTuple<A>, number?]): Observable<A[number]>; |
| 11 |
/** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `mergeAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */ |
| 12 |
export function merge<A extends readonly unknown[]>( |
| 13 |
...sourcesAndScheduler: [...ObservableInputTuple<A>, SchedulerLike?] |
| 14 |
): Observable<A[number]>; |
| 15 |
/** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `mergeAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */ |
| 16 |
export function merge<A extends readonly unknown[]>( |
| 17 |
...sourcesAndConcurrencyAndScheduler: [...ObservableInputTuple<A>, number?, SchedulerLike?] |
| 18 |
): Observable<A[number]>; |
| 19 |
|
| 20 |
/** |
| 21 |
* Creates an output Observable which concurrently emits all values from every |
| 22 |
* given input Observable. |
| 23 |
* |
| 24 |
* <span class="informal">Flattens multiple Observables together by blending |
| 25 |
* their values into one Observable.</span> |
| 26 |
* |
| 27 |
*  |
| 28 |
* |
| 29 |
* `merge` subscribes to each given input Observable (as arguments), and simply |
| 30 |
* forwards (without doing any transformation) all the values from all the input |
| 31 |
* Observables to the output Observable. The output Observable only completes |
| 32 |
* once all input Observables have completed. Any error delivered by an input |
| 33 |
* Observable will be immediately emitted on the output Observable. |
| 34 |
* |
| 35 |
* ## Examples |
| 36 |
* |
| 37 |
* Merge together two Observables: 1s interval and clicks |
| 38 |
* |
| 39 |
* ```ts |
| 40 |
* import { merge, fromEvent, interval } from 'rxjs'; |
| 41 |
* |
| 42 |
* const clicks = fromEvent(document, 'click'); |
| 43 |
* const timer = interval(1000); |
| 44 |
* const clicksOrTimer = merge(clicks, timer); |
| 45 |
* clicksOrTimer.subscribe(x => console.log(x)); |
| 46 |
* |
| 47 |
* // Results in the following: |
| 48 |
* // timer will emit ascending values, one every second(1000ms) to console |
| 49 |
* // clicks logs MouseEvents to console every time the "document" is clicked |
| 50 |
* // Since the two streams are merged you see these happening |
| 51 |
* // as they occur. |
| 52 |
* ``` |
| 53 |
* |
| 54 |
* Merge together 3 Observables, but run only 2 concurrently |
| 55 |
* |
| 56 |
* ```ts |
| 57 |
* import { interval, take, merge } from 'rxjs'; |
| 58 |
* |
| 59 |
* const timer1 = interval(1000).pipe(take(10)); |
| 60 |
* const timer2 = interval(2000).pipe(take(6)); |
| 61 |
* const timer3 = interval(500).pipe(take(10)); |
| 62 |
* |
| 63 |
* const concurrent = 2; // the argument |
| 64 |
* const merged = merge(timer1, timer2, timer3, concurrent); |
| 65 |
* merged.subscribe(x => console.log(x)); |
| 66 |
* |
| 67 |
* // Results in the following: |
| 68 |
* // - First timer1 and timer2 will run concurrently |
| 69 |
* // - timer1 will emit a value every 1000ms for 10 iterations |
| 70 |
* // - timer2 will emit a value every 2000ms for 6 iterations |
| 71 |
* // - after timer1 hits its max iteration, timer2 will |
| 72 |
* // continue, and timer3 will start to run concurrently with timer2 |
| 73 |
* // - when timer2 hits its max iteration it terminates, and |
| 74 |
* // timer3 will continue to emit a value every 500ms until it is complete |
| 75 |
* ``` |
| 76 |
* |
| 77 |
* @see {@link mergeAll} |
| 78 |
* @see {@link mergeMap} |
| 79 |
* @see {@link mergeMapTo} |
| 80 |
* @see {@link mergeScan} |
| 81 |
* |
| 82 |
* @param args `ObservableInput`s to merge together. If the last parameter |
| 83 |
* is of type number, `merge` will use it to limit number of concurrently |
| 84 |
* subscribed `ObservableInput`s. If the last parameter is {@link SchedulerLike}, |
| 85 |
* it will be used for scheduling the emission of values. |
| 86 |
* @return An Observable that emits items that are the result of every input Observable. |
| 87 |
*/ |
| 88 |
export function merge(...args: (ObservableInput<unknown> | number | SchedulerLike)[]): Observable<unknown> { |
| 89 |
const scheduler = popScheduler(args); |
| 90 |
const concurrent = popNumber(args, Infinity); |
| 91 |
const sources = args as ObservableInput<unknown>[]; |
| 92 |
return !sources.length |
| 93 |
? // No source provided |
| 94 |
EMPTY |
| 95 |
: sources.length === 1 |
| 96 |
? // One source? Just return it. |
| 97 |
innerFrom(sources[0]) |
| 98 |
: // Merge all sources |
| 99 |
mergeAll(concurrent)(from(sources, scheduler)); |
| 100 |
} |