Angular 如何链接可观察的RxJS列表
对单页应用程序使用Angular CLI 6 我需要做的是以下几点 发出HTTP post请求。我会得到你的同意。结果或副作用将需要一段时间才能处理。 我将不得不为结果准备投票,比如说每秒一次 要轮询结果,我需要进行HTTPGET调用并检查结果。如果结果完整,我就完成了。 否则我将不得不继续投票 在我所做的工作中,我有两个obserable,一个用于httppost,另一个用于httpget调用。 我使用setTimeout进行轮询。这个组织的代码,当我点击setTimeout时,我去了never-land,我必须杀死这个应用程序 关于这个问题有什么提示吗 到目前为止,我得到的是Angular 如何链接可观察的RxJS列表,angular,rxjs6,Angular,Rxjs6,对单页应用程序使用Angular CLI 6 我需要做的是以下几点 发出HTTP post请求。我会得到你的同意。结果或副作用将需要一段时间才能处理。 我将不得不为结果准备投票,比如说每秒一次 要轮询结果,我需要进行HTTPGET调用并检查结果。如果结果完整,我就完成了。 否则我将不得不继续投票 在我所做的工作中,我有两个obserable,一个用于httppost,另一个用于httpget调用。 我使用setTimeout进行轮询。这个组织的代码,当我点击setTimeout时,我去了neve
private initiateAnalysis(){
this.initiateRequest$()
.subscribe(response =>{
const error = getErrorMessage(response);
if (error !== null) {
console.error(error);
} else {
this.processResults();
}
},
(err: HttpErrorResponse) =>{
console.error('feature error:', err);
});
}
private initiateRequest$(): Observable<any>{
let params: any = {
};
return this.problemsService.postRequest('postURL', {}, params)
}
private checkForResponse$(): Observable<any>{
let params: any = {
};
return this.problemsService.getResults('someURL', params);
}
private processResults(){
this.doneWithNecRiskAnalysis = false;
while (!this.doneWithNecRiskAnalysis) {
setTimeout(() =>{
this.checkForResults(); // I never to this line in the code...
}, 1000);
}
}
private checkForResults() {
this.checkForResponse$()
.subscribe(response =>{
const error = getErrorMessage(response);
if (error !== null) {
console.error(error);
} else {
if (1 === 1) { // just for now
this.showResults(response.payload);
}
}
},
(err: HttpErrorResponse) =>{
console.error('feature error:', err);
});
}
private showResults(results) {
console.log('results', results);
}
private initiateAnalysis(){
此.initiateRequest$()
.订阅(响应=>{
常量错误=getErrorMessage(响应);
如果(错误!==null){
控制台错误(error);
}否则{
这个.processResults();
}
},
(错误:HttpErrorResponse)=>{
console.error('featureerror:',err);
});
}
私有initiateRequest$():可观察{
设params:any={
};
返回此.problemsService.postRequest('postrl',{},params)
}
private checkForResponse$():可观察{
设params:any={
};
返回this.problemsService.getResults('someURL',params);
}
私有进程结果(){
this.donewithnecrisksanalysis=false;
而(!this.donewithnec风险分析){
设置超时(()=>{
this.checkForResults();//我从未在代码中看到这一行。。。
}, 1000);
}
}
私人checkForResults(){
此.checkForResponse$()
.订阅(响应=>{
常量错误=getErrorMessage(响应);
如果(错误!==null){
控制台错误(error);
}否则{
如果(1==1){//现在
这个.showResults(response.payload);
}
}
},
(错误:HttpErrorResponse)=>{
console.error('featureerror:',err);
});
}
私人展示结果(结果){
console.log('results',results);
}
这会不断要求服务器在1秒内检查结果。你从不等到下一秒再问
要使您的代码变得更加简单和干净,我要做的第一件事是重构您的服务(和/或后端),以便它们的可观察对象在出现错误时发出错误,而不是发出正常的错误消息
其余的将假定您已经这样做了
您还应该停止使用any
类型
然后,代码可以简化为以下内容:
private initiateAnalysis() {
this.initiateRequest$().pipe( // sends the first request to start the analysis
switchMap(() => interval(1000)), // when the response comes back, start emitting an event every second from this observable
mergeMap(() => this.checkForResponse$()), // each second, send the GET request and emit the results. Merge all the results into the resulting observable
filter(results => this.isAnalysisComplete(results)), // filter out the results if they are not the final, correct results
first() // only take the first complete results, to avoid continuing sending GET requests
).subscribe(results => this.showResults(results));
}
private initiateRequest$(): Observable<void> {
const params = {
};
return this.problemsService.postRequest('postURL', {}, params)
}
private checkForResponse$(): Observable<Results>{
const params = {
};
return this.problemsService.getResults('someURL', params);
}
private showResults(results: Results) {
console.log('results', results);
}
private initiateAnalysis(){
此.initiateRequest$().pipe(//发送第一个请求以启动分析
switchMap(()=>interval(1000)),//当响应返回时,开始每秒从这个可观察对象发出一个事件
mergeMap(()=>this.checkForResponse$()),//每秒发送GET请求并发出结果。将所有结果合并到结果可观察对象中
筛选(results=>this.isAnalysisComplete(results)),//如果结果不是最终的正确结果,则筛选出结果
first()//仅获取第一个完整结果,以避免继续发送GET请求
).subscribe(results=>this.showResults(results));
}
私有initiateRequest$():可观察{
常量参数={
};
返回此.problemsService.postRequest('postrl',{},params)
}
private checkForResponse$():可观察{
常量参数={
};
返回this.problemsService.getResults('someURL',params);
}
私人展示结果(结果:结果){
console.log('results',results);
}
如果要在发送下一个请求之前等待上一个响应,您可能更喜欢使用concatMap()
而不是mergeMap()
下面是一个演示,当实际HTTP请求被随机延迟和随机响应所取代时:如果您想让我们解释代码的问题,您需要发布代码。我认为轮询的可观察对象应该在HTTP请求后使用setInterval并调用onNext。然后你可以使用takeWhile操作符谢谢,我读到关于takeWhile的用法,看起来很有前途。你能详细解释一下你说的“设置间隔并调用onNext”是什么意思吗?这太好了,非常感谢。但有一个问题。您假设来自get和处理的第一个响应结束。这是不正确的。我需要对get进行轮询,以发送“合适”的响应,即获取响应,检查是否存在某些值,然后显示结果。在您的示例中,我只处理一次get结果,然后结束处理并显示结果。有道理吗?不,一点也不。您没有阅读代码和注释,也没有尝试演示。去做吧
filter(results=>this.isAnalysisComplete(results)),//如果结果不是最终的、正确的结果,请过滤掉结果
对不起,刚才我注意到了。非常感谢你。
private initiateAnalysis() {
this.initiateRequest$().pipe( // sends the first request to start the analysis
switchMap(() => interval(1000)), // when the response comes back, start emitting an event every second from this observable
mergeMap(() => this.checkForResponse$()), // each second, send the GET request and emit the results. Merge all the results into the resulting observable
filter(results => this.isAnalysisComplete(results)), // filter out the results if they are not the final, correct results
first() // only take the first complete results, to avoid continuing sending GET requests
).subscribe(results => this.showResults(results));
}
private initiateRequest$(): Observable<void> {
const params = {
};
return this.problemsService.postRequest('postURL', {}, params)
}
private checkForResponse$(): Observable<Results>{
const params = {
};
return this.problemsService.getResults('someURL', params);
}
private showResults(results: Results) {
console.log('results', results);
}