在角项目中使用rxjs进行Http轮询和条件逻辑

Chr*_*mer 2 observable rxjs angular

我正处于Angular 4项目的早期阶段,试图为我的http服务创建一个客户端.这些服务可以返回以下两种情况之一:

  1. 来自服务的实际所需json响应,http状态为200,或
  2. 一个guid,表示一个作业令牌,我可以用它来轮询长时间运行的作业的状态,http状态为202.

当服务返回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?
            });
    });
Run Code Online (Sandbox Code Playgroud)

有人能给我一些关于如何使用逻辑的提示repeatWhen吗?我没有看到任何使用来自observable本身的内容来决定是否重新订阅的示例.

pau*_*els 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}`))

    });
Run Code Online (Sandbox Code Playgroud)

基本上你的第一个条件是正确的,第二个部分你需要启动请求并不断重复它,该skipWhile部分将检查结果并且不会传播它们直到状态被标记为完成,然后你可以flatMapTo用来转换结果消息直接进入另一个API请求以获取实际结果然后传播.