我正处于Angular 4项目的早期阶段,试图为我的http服务创建一个客户端。这些服务可以返回以下两种方式之一:
当服务返回200状态时,我想直接返回其json内容,这样就够了。
当结果状态为202时,我想要使用该令牌轮询一个单独的URL并接收带有状态信息的json对象。当状态信息显示完成时,我想再发送一个请求返回输出的最终URL。
我不确定如何在请求中处理我需要的条件逻辑。我怀疑我可以使用repeatWhen
来监控轮询请求的结果,但我无法弄清楚如何使用它。
http.request('/some/longrunning/job')
.flatMap((resp: Response) => {
if (resp.status !== 202)
return Observable.of(resp);
var job_token = resp.json().token;
return http.get('/jobstatus/' + job_token)
// result from this endpoint is a json object that looks something
// like { status: 'running' } or { status: 'complete' }
.repeatWhen(function (polling_resp: Observable<Response>) {
// How do I use polling_resp here to look at my http response?
// Do I need to explicitly subscribe to it? And if so,
// how then do I inspect the response and return the
// appropriate value to indicate whether or not to
// resubscribe via the repeatWhen?
});
});
有人可以给我一些关于如何在repeatWhen
中使用逻辑的提示吗?我没有看到任何使用来自observable本身的内容来决定是否重新订阅的示例。
答案 0 :(得分:7)
我认为你正在寻找更像这样的东西:
http.request('/some/longrunning/job')
.flatMap((resp: Response) => {
if (resp.status !== 202)
return Observable.of(resp);
const token = resp.json().token;
return http.get(`/jobstatus/${token}`)
// You can use repeatWhen if you need more nuanced logic for
// how many times you want this to repeat, and how it should
// space out repeat attempts.
// This is just an example that guarantees at least 1 second between
// poll attempts so as to not overload the server
.repeatWhen(c => c.debounceTime(1000))
// Ignore values until you have completed
.skipWhile(resp => resp.json().status !== 'completed')
// At this point you can stop the polling
.take(1)
// You don't actually care about the value of the response
// just that it has "completed" this maps that response to the
// call to fetch the result status.
.flatMapTo(http.get(`/jobresult/${token}`))
});
基本上你的第一个条件是正确的,第二个部分你需要启动请求并不断重复它,skipWhile
部分将检查结果并且不会传播它们直到状态被标记为完成,你然后可以使用flatMapTo
将生成的消息直接转换为另一个API请求,以获取实际结果,然后传播。