RxJS merge

2020-10-13 18:45 更新

創(chuàng)建一個輸出 Observable,該輸出同時從每個給定的輸入 Observable 發(fā)出所有值。

merge<T, R>(...observables: any[]): Observable<R>

參量

可觀察的 輸入可觀察對象以合并在一起。

returns

Observable<R>:一個 Observable,它發(fā)出作為每個輸入 Observable 的結果的項目。

描述

通過將多個 Observable 的值混合到一個 Observable中,將其平展在一起。

合并大理石圖

merge訂閱每個給定的輸入 Observable(作為參數),并簡單地將所有值從所有輸入 Observable 轉發(fā)(不進行任何轉換)到輸出 Observable。僅當所有輸入 Observable 完成后,輸出 Observable 才完成。輸入 Observable 傳遞的任何錯誤將立即在輸出 Observable 上發(fā)出。

例子

合并兩個 Observable:1s 間隔和點擊

import { merge, fromEvent, interval } from 'rxjs';


const clicks = fromEvent(document, 'click');
const timer = interval(1000);
const clicksOrTimer = merge(clicks, timer);
clicksOrTimer.subscribe(x => console.log(x));


// Results in the following:
// timer will emit ascending values, one every second(1000ms) to console
// clicks logs MouseEvents to console everytime the "document" is clicked
// Since the two streams are merged you see these happening
// as they occur.

合并3個 Observable,但只有 2 個同時運行

import { merge, interval } from 'rxjs';
import { take } from 'rxjs/operators';


const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const timer3 = interval(500).pipe(take(10));
const concurrent = 2; // the argument
const merged = merge(timer1, timer2, timer3, concurrent);
merged.subscribe(x => console.log(x));


// Results in the following:
// - First timer1 and timer2 will run concurrently
// - timer1 will emit a value every 1000ms for 10 iterations
// - timer2 will emit a value every 2000ms for 6 iterations
// - after timer1 hits its max iteration, timer2 will
//   continue, and timer3 will start to run concurrently with timer2
// - when timer2 hits its max iteration it terminates, and
//   timer3 will continue to emit a value every 500ms until it is complete

超載

merge(v1: any, scheduler: SchedulerLike): Observable<T>

參量: 類型:
v1 類型:any
調度器 類型:SchedulerLike

returnsObservable<T>

merge(v1: any, concurrent: number, scheduler: SchedulerLike): Observable<T>

參量: 類型:
v1 類型:any
同時 類型:number
調度器 類型:SchedulerLike

returnsObservable<T>

merge(v1: any, v2: any, scheduler: SchedulerLike): Observable<T | T2>

參量: 類型:
v1 類型:any
v2 類型:any
調度器 類型:SchedulerLike
returnsObservable<T | T2>

merge(v1: any, v2: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2>

參量: 類型:
v1 類型:any
v2 類型:any
同時 類型:number
調度器 類型:SchedulerLike
returnsObservable<T | T2>

merge(v1: any, v2: any, v3: any, scheduler: SchedulerLike): Observable<T | T2 | T3>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
同時 類型:number
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, v4: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
同時 類型:number
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
同時 類型:number
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5>
merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>
參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
v6 類型:any
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
v6 類型:any
同時 類型:number
調度器 類型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any): Observable<T>

參量: 類型:
v1 類型:any

returnsObservable<T>

merge(v1: any, concurrent?: number): Observable<T>

參量: 類型:
v1 類型:any
同時 可選的默認值為undefined。  類型:number

returnsObservable<T>

merge(v1: any, v2: any): Observable<T | T2>

參量: 類型:
v1 類型:any
v2 類型:any
returnsObservable<T | T2>

merge(v1: any, v2: any, concurrent?: number): Observable<T | T2>

參量: 類型:
v1 類型:any
v2 類型:any
同時 可選的。默認值為undefined。類型:number
returnsObservable<T | T2>

merge(v1: any, v2: any, v3: any): Observable<T | T2 | T3>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, concurrent?: number): Observable<T | T2 | T3>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
同時 可選的。默認值為undefined。類型:number。
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, v4: any): Observable<T | T2 | T3 | T4>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, concurrent?: number): Observable<T | T2 | T3 | T4>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
同時 可選的。默認值為undefined。類型:number
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, v5: any): Observable<T | T2 | T3 | T4 | T5>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4類型:any。v5類型:any
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, concurrent?: number): Observable<T | T2 | T3 | T4 | T5>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
同時 可選的。默認值為undefined。類型:number
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any): Observable<T | T2 | T3 | T4 | T5 | T6>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
v6 類型:any
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, concurrent?: number): Observable<T | T2 | T3 | T4 | T5 | T6>

參量: 類型:
v1 類型:any
v2 類型:any
v3 類型:any
v4 類型:any
v5 類型:any
v6 類型:any
同時 可選的。默認值為undefined。類型:number
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(...observables: any[]): Observable<T>

參量: 類型:
可觀察的 類型:any[]

returnsObservable<T>

merge(...observables: any[]): Observable<T>

參量: 類型:
可觀察的 類型:any[]

returnsObservable<T>

merge(...observables: any[]): Observable<R>

參量: 類型:
可觀察的 類型:any[]

returnsObservable<R>

merge(...observables: any[]): Observable<R>

參量: 類型:
可觀察的 類型:any[]

returnsObservable<R>

也可以看看

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號