繁体   English   中英

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

[英]Http polling and conditional logic with rxjs in angular project

我正处于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?
            });
    });

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

我想你正在寻找更像这样的东西:

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请求以获取实际结果,然后传播。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM