重试可观察直到满足条件

时间:2019-06-28 04:53:27

标签: typescript rxjs

这是我的打字稿功能isIndexComplete

isIndexComplete(index_id:string): Observable<boolean> {
        console.log("Entering get indexer status");
        return this._http.get<IIndexerGetStatus>(`http://${environment.agent_ip}:${environment.agent_port}/`,{
            params:{
                action:'IndexerGetStatus',
                epochTime:'true',
                maxResults:'1',
                responseFormat:'simplejson',
                Index:index_id
            }
        }).pipe(
            map(response => {
                if (response.autnresponse.responsedata.item[0].percentage_processed == '100'){
                    console.log("percentage_processed "+ response.autnresponse.responsedata.item[0].percentage_processed);
                    return true;
                }
                else{
                    console.log("percentage_processed "+ response.autnresponse.responsedata.item[0].percentage_processed);
                    this.currentProgress = response.autnresponse.responsedata.item[0].percentage_processed;
                    this.isIndexComplete(index_id).subscribe();
                    return false
                }
            })
        )
    }

我需要继续从SaveAgentRecord调用isIndexComplete,直到this.value为100%

SaveAgentRecord(topicText:string, topicTextBoolean:string): void {
    this._agentstore.createAgentEntry(topicText, topicTextBoolean).subscribe( response =>{
        var index_id = this._agentstore.getIndexID(response);
        this._agentstore.isIndexComplete(index_id).subscribe(response => {
            this.AgentRuleCreationProgress = +this._agentstore.currentProgress;
            this.value = this.AgentRuleCreationProgress;
            if(this.value < 100){
                console.log("Value less than 100");
            }
        })
        console.log("Get Index Status shows 100%")
        this.ShowAgentCreated(topicText);
    });
}

我该怎么做?另外我也不想执行this.ShowAgentCreated(topicText),直到其100%

1 个答案:

答案 0 :(得分:0)

类似的事情,但是用this._http.get<IIndexerGetStatus>替换源,并根据. currentProgress属性更改百分比

defer(()=>{
  let percentage=0
  return source.pipe(
    tap(_=>percentage++),
    repeatWhen((e) => {
    return timer(2000).pipe(takeWhile(_=>percentage<2))
  }),
)
}).subscribe()