rxjs:定期执行一些操作,之间有特定的延迟 [英] rxjs: perform some action regulary with specific delay in between

查看:95
本文介绍了rxjs:定期执行一些操作,之间有特定的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户端应用程序将请求发送到服务器,这可能需要很长时间才能完成.请求完成或失败后,客户端应等待一段时间(即10秒),然后再次发送请求.

当前可行的解决方案是这样的:

  appRequest = new Subject();ngOnInit():void {this.appRequest.delay(10000).subscribe(()=> this.refresh());this.refresh();}刷新() {this.api.getApplications().subscribe(a => {this.updateApplications(a);this.appRequest.next();},()=>this.appRequest.next());} 

是否有更优雅的解决方案?

我可以定期使用计时器,但是除非先前的请求完成,否则我不希望发送新的请求.仅在先前的请求完成后,我才需要等待10秒钟,然后再次发送请求.这应该无限期地重复.

getApplications()函数由swagger生成,并且在内部使用angular的http客户端库.当前的观察结果是,除非您预订 getApplications()返回的 Observable ,否则它将不会向服务器发送请求.

解决方案

repeatWhen()运算符似乎是为此目的而设计的,但是缺少rxjs风格的示例和文档.

这是RxJava的文档(说明也适用于RxJ) stopRequesting = new Subject();ngOnInit(){this.api.getApplications().repeatWhen(已完成=>已完成.delay(10000)).takeUntil(stopRequesting).subscribe(a => this.updateApplications(a))}ngOnDestroy(){this.stopRequesting.next(true);}

演示

Client applications sends request to server, that could potentially take long to complete. Once request is finished or failed, client should wait some period of time (i.e. 10 seconds) and then again send the request.

Current working solution is this:

appRequest = new Subject();

ngOnInit(): void {
  this.appRequest.delay(10000).subscribe(() => this.refresh());
  this.refresh();
}

refresh() {
  this.api.getApplications().subscribe(a => {
      this.updateApplications(a);
      this.appRequest.next();
    },() => this.appRequest.next()
  );
}

Is there a more elegant solution for this?

EDIT:

I could use timer with regular intervals but I don't want to send new request unless previous request has finished. Only after previous request has finished, I want to wait 10 seconds and do send request again. This should repeat indefinitely.

getApplications() function is generated by swagger and it internally uses angular's http client library. Current observation is that unless you subscribe to Observable returned by getApplications(), it will not send request to server.

解决方案

The repeatWhen() operator seems designed for this, but there is a lack of examples and docs in the rxjs flavor.

Here's a doc for RxJava (description also applies to RxJs) RxJava's repeatWhen and retryWhen, explained.

Uses
Poll for data periodically using repeatWhen + delay:
source.repeatWhen(completed => completed.delay(5000))

Your version might be

stopRequesting = new Subject();

ngOnInit() {
  this.api.getApplications()
    .repeatWhen(completed => completed.delay(10000))
    .takeUntil(stopRequesting)
    .subscribe(a => this.updateApplications(a))
} 

ngOnDestroy() {
  this.stopRequesting.next(true);
}

Demo

// log to html output
log = function(x) { document.write(x + "<br />"); };

const stop = new Rx.Subject();

Rx.Observable.interval(500)
  .take(2)
  .repeatWhen(completed => completed.delay(1000))
  .takeUntil(stop)
  .subscribe(
    x => log(`Next: ${x}`),
    err => log(`Error: ${err}`),
    () => log('Completed')
  );

setTimeout(() => stop.next(true), 10000)

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>

这篇关于rxjs:定期执行一些操作,之间有特定的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆