如何链接可观察的RxJS列表

时间:2018-10-01 18:03:37

标签: angular rxjs6

为单个页面应用程序使用Angular CLI 6

我需要做的是以下几点 发出HTTP发布请求。我会回来的。结果或副作用需要一段时间才能准备好进行处理。 我将不得不轮询结果是否准备就绪,每秒钟说一次。

要轮询结果,我需要进行HTTP get调用并检查结果。如果结果完成,我就完成了。 否则,我将不得不继续投票。

我所做的,我有两个令人讨厌的地方,一个用于HTTP发布,另一个用于HTTP get调用。 我使用setTimeout进行轮询。这种组织代码的方法,当我遇到setTimeout时,我永远也永远不要着陆,我必须杀死应用程序...

有关此问题的任何提示吗?

到目前为止,我有

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);
}

1 个答案:

答案 0 :(得分:2)

belongs_to: ticket_category, class_name: "Admin::TicketCategory

这经常要求服务器在1秒内检查结果。您再也不需要等待下一秒。

我要做的第一件事就是使您的代码更加简单,整洁,以便重构您的服务(和/或您的后端),以便它们的可观察项在发生错误时发出错误 ,即使发出错误消息也不会发出正常信号。

其余人员将假定您已完成操作。

您还应该停止使用while (!this.doneWithNecRiskAnalysis) { setTimeout(() =>{ this.checkForResults(); // I never to this line in the code... }, 1000); } 类型。

然后可以将代码简化为以下内容:

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); } 而不是concatMap()

这是一个演示,其中实际的HTTP请求被随机延迟和随机响应代替:https://stackblitz.com/edit/angular-bt17fb?file=src%2Fapp%2Fapp.component.ts