1 | import { Observable } from '../Observable';
|
---|
2 | import { ObservableInput, SchedulerLike} from '../types';
|
---|
3 | import { isScheduler } from '../util/isScheduler';
|
---|
4 | import { mergeAll } from '../operators/mergeAll';
|
---|
5 | import { fromArray } from './fromArray';
|
---|
6 |
|
---|
7 | /* tslint:disable:max-line-length */
|
---|
8 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
9 | export function merge<T>(v1: ObservableInput<T>, scheduler: SchedulerLike): Observable<T>;
|
---|
10 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
11 | export function merge<T>(v1: ObservableInput<T>, concurrent: number, scheduler: SchedulerLike): Observable<T>;
|
---|
12 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
13 | export function merge<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>, scheduler: SchedulerLike): Observable<T | T2>;
|
---|
14 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
15 | export function merge<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2>;
|
---|
16 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
17 | export function merge<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, scheduler: SchedulerLike): Observable<T | T2 | T3>;
|
---|
18 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
19 | export function merge<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3>;
|
---|
20 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
21 | export function merge<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>;
|
---|
22 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
23 | export function merge<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>;
|
---|
24 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
25 | export function merge<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>;
|
---|
26 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
27 | export function merge<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>;
|
---|
28 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
29 | export function merge<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>;
|
---|
30 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
31 | export function merge<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>;
|
---|
32 |
|
---|
33 | export function merge<T>(v1: ObservableInput<T>): Observable<T>;
|
---|
34 | export function merge<T>(v1: ObservableInput<T>, concurrent?: number): Observable<T>;
|
---|
35 | export function merge<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<T | T2>;
|
---|
36 | export function merge<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>, concurrent?: number): Observable<T | T2>;
|
---|
37 | export function merge<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<T | T2 | T3>;
|
---|
38 | export function merge<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, concurrent?: number): Observable<T | T2 | T3>;
|
---|
39 | export function merge<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<T | T2 | T3 | T4>;
|
---|
40 | export function merge<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, concurrent?: number): Observable<T | T2 | T3 | T4>;
|
---|
41 | export function merge<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<T | T2 | T3 | T4 | T5>;
|
---|
42 | export function merge<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, concurrent?: number): Observable<T | T2 | T3 | T4 | T5>;
|
---|
43 | export function merge<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<T | T2 | T3 | T4 | T5 | T6>;
|
---|
44 | export function merge<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, concurrent?: number): Observable<T | T2 | T3 | T4 | T5 | T6>;
|
---|
45 | export function merge<T>(...observables: (ObservableInput<T> | number)[]): Observable<T>;
|
---|
46 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
47 | export function merge<T>(...observables: (ObservableInput<T> | SchedulerLike | number)[]): Observable<T>;
|
---|
48 | export function merge<T, R>(...observables: (ObservableInput<any> | number)[]): Observable<R>;
|
---|
49 | /** @deprecated use {@link scheduled} and {@link mergeAll} (e.g. `scheduled([ob1, ob2, ob3], scheduled).pipe(mergeAll())*/
|
---|
50 | export function merge<T, R>(...observables: (ObservableInput<any> | SchedulerLike | number)[]): Observable<R>;
|
---|
51 | /* tslint:enable:max-line-length */
|
---|
52 | /**
|
---|
53 | * Creates an output Observable which concurrently emits all values from every
|
---|
54 | * given input Observable.
|
---|
55 | *
|
---|
56 | * <span class="informal">Flattens multiple Observables together by blending
|
---|
57 | * their values into one Observable.</span>
|
---|
58 | *
|
---|
59 | * ![](merge.png)
|
---|
60 | *
|
---|
61 | * `merge` subscribes to each given input Observable (as arguments), and simply
|
---|
62 | * forwards (without doing any transformation) all the values from all the input
|
---|
63 | * Observables to the output Observable. The output Observable only completes
|
---|
64 | * once all input Observables have completed. Any error delivered by an input
|
---|
65 | * Observable will be immediately emitted on the output Observable.
|
---|
66 | *
|
---|
67 | * ## Examples
|
---|
68 | * ### Merge together two Observables: 1s interval and clicks
|
---|
69 | * ```ts
|
---|
70 | * import { merge, fromEvent, interval } from 'rxjs';
|
---|
71 | *
|
---|
72 | * const clicks = fromEvent(document, 'click');
|
---|
73 | * const timer = interval(1000);
|
---|
74 | * const clicksOrTimer = merge(clicks, timer);
|
---|
75 | * clicksOrTimer.subscribe(x => console.log(x));
|
---|
76 | *
|
---|
77 | * // Results in the following:
|
---|
78 | * // timer will emit ascending values, one every second(1000ms) to console
|
---|
79 | * // clicks logs MouseEvents to console everytime the "document" is clicked
|
---|
80 | * // Since the two streams are merged you see these happening
|
---|
81 | * // as they occur.
|
---|
82 | * ```
|
---|
83 | *
|
---|
84 | * ### Merge together 3 Observables, but only 2 run concurrently
|
---|
85 | * ```ts
|
---|
86 | * import { merge, interval } from 'rxjs';
|
---|
87 | * import { take } from 'rxjs/operators';
|
---|
88 | *
|
---|
89 | * const timer1 = interval(1000).pipe(take(10));
|
---|
90 | * const timer2 = interval(2000).pipe(take(6));
|
---|
91 | * const timer3 = interval(500).pipe(take(10));
|
---|
92 | * const concurrent = 2; // the argument
|
---|
93 | * const merged = merge(timer1, timer2, timer3, concurrent);
|
---|
94 | * merged.subscribe(x => console.log(x));
|
---|
95 | *
|
---|
96 | * // Results in the following:
|
---|
97 | * // - First timer1 and timer2 will run concurrently
|
---|
98 | * // - timer1 will emit a value every 1000ms for 10 iterations
|
---|
99 | * // - timer2 will emit a value every 2000ms for 6 iterations
|
---|
100 | * // - after timer1 hits its max iteration, timer2 will
|
---|
101 | * // continue, and timer3 will start to run concurrently with timer2
|
---|
102 | * // - when timer2 hits its max iteration it terminates, and
|
---|
103 | * // timer3 will continue to emit a value every 500ms until it is complete
|
---|
104 | * ```
|
---|
105 | *
|
---|
106 | * @see {@link mergeAll}
|
---|
107 | * @see {@link mergeMap}
|
---|
108 | * @see {@link mergeMapTo}
|
---|
109 | * @see {@link mergeScan}
|
---|
110 | *
|
---|
111 | * @param {...ObservableInput} observables Input Observables to merge together.
|
---|
112 | * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
|
---|
113 | * Observables being subscribed to concurrently.
|
---|
114 | * @param {SchedulerLike} [scheduler=null] The {@link SchedulerLike} to use for managing
|
---|
115 | * concurrency of input Observables.
|
---|
116 | * @return {Observable} an Observable that emits items that are the result of
|
---|
117 | * every input Observable.
|
---|
118 | * @static true
|
---|
119 | * @name merge
|
---|
120 | * @owner Observable
|
---|
121 | */
|
---|
122 | export function merge<T, R>(...observables: Array<ObservableInput<any> | SchedulerLike | number>): Observable<R> {
|
---|
123 | let concurrent = Number.POSITIVE_INFINITY;
|
---|
124 | let scheduler: SchedulerLike = null;
|
---|
125 | let last: any = observables[observables.length - 1];
|
---|
126 | if (isScheduler(last)) {
|
---|
127 | scheduler = <SchedulerLike>observables.pop();
|
---|
128 | if (observables.length > 1 && typeof observables[observables.length - 1] === 'number') {
|
---|
129 | concurrent = <number>observables.pop();
|
---|
130 | }
|
---|
131 | } else if (typeof last === 'number') {
|
---|
132 | concurrent = <number>observables.pop();
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (scheduler === null && observables.length === 1 && observables[0] instanceof Observable) {
|
---|
136 | return <Observable<R>>observables[0];
|
---|
137 | }
|
---|
138 |
|
---|
139 | return mergeAll<R>(concurrent)(fromArray<any>(observables, scheduler));
|
---|
140 | }
|
---|