2017-03-10 8 views
0

からのデータを更新し、私は私がする必要があるシナリオがあります。角度&RxJS5 - HTTP&ソケット

  1. をHTTPが応答したら、同じを使用して、HTTPサービス
  2. を呼び出すために
  3. 利用ルートのparamsのparamsルートを取得ルートのparamsたびソケットが応答ソケットサービス
  4. を呼び出すために、HTTPサービスからの更新データ

理想的には、私はこの私を維持したいと思いますnストリーム。

CombineLatestのみ使用できますか?またはScanオペレータを関与させますか?

this.data$ = this.route.params 
       .switchMap(params => { 
        return Observable.forkJoin([ 
         Observable.of(params), 
         this.http.get('api', { prop1: params.prop1, prop2: params.prop2 }) 
        ]) 
       }).combineLatest(([params, data]) => { 
        this.socket.get('event', { prop1: params.prop1, prop2: params.prop2 }), 
         updateData 
       }) 


    private updateData(data, socketData): any[] { 
     //only returns data = [params, data] 
     //socketData always undef 
     return data; 
    } 

答えて

1

私はあなたの流れを理解する方法から、私はあなたがcombineLatestまたはscan演算子のいずれかを必要とする必要はありません、あなただけの巣2 switchMapsする必要があります。

this.data$ = this.route.params 
       .switchMap(params => this.http.get('api', { prop1: params.prop1, prop2: params.prop2 }) 
        .switchMap(httpResult => this.socket.get('event', { prop1: params.prop1, prop2: params.prop2 }) // assuming socket.get(...) is a perpetual stream 
         .map(socketResult => doWhateverCombinationWith(httpResult, socketResult)) 
         .startWith(httpResult); // not sure if this is required, your question is kind of vague, but essentially this will cause the stream to emit the original httpResult before there is socketData 
        ) 
       ); 
+0

私は、ネストされたことをオペレータを認識していませんでした正当だった?私はそれが問題を引き起こす印象の下にあったが、見えなかった。ありがとう – Thibs