[英]Angular & RxJS5 - updating data from http & socket
I have a scenario where I need to: 我有一种情况需要:
Ideally I would like to keep this in a stream. 理想情况下,我想保持此状态。
If only I could use CombineLatest
? 如果我可以使用
CombineLatest
? or involve the Scan
operator? 或涉及
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;
}
From how I understood your flow I don't need you need any of the combineLatest
or scan
operators, you just need to nest two switchMaps
: 根据我对流程的了解,我不需要任何
combineLatest
或scan
运算符,只需要嵌套两个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
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.