2017-03-10 33 views
0

更新數據我有一種情況,我需要:角及RxJS5 - 從http:插座

  1. 獲取路線PARAMS
  2. 使用路線PARAMS調用http服務
  3. 一旦HTTP響應,使用相同的路線PARAMS調用Socket服務
  4. 每次插座響應,從HTTP服務更新數據

理想我想保持這個我一個流。

如果只有我可以使用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

從我如何理解你的流量,我不需要你需要任何combineLatestscan運營商,你只需要窩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