如何使用RXJS来改进这个函数?

huangapple go评论65阅读模式
英文:

How can I improve this function using RXJS?

问题

我对RXJS还不熟悉,我想创建一个函数,该函数会递归地检查状态,直到状态变为"Available"。我找到了使用纯JS来表示我所寻找的内容:

setTimeout(() => {
    this.updateStatus();
}, status === 'Pending' ? 0 : 2000);
英文:

I'm new with RXJS and I would like to do a function which checks recursively a status until it will be "Available". I found this with vanilla JS to represent what I'm looking for:

setTimeout(() => {
                   this.updateStatus();
                 },status === 'Pending' ? 0 : 2000);

答案1

得分: 1

你提到想要在状态可用之前一直进行检查。我假设你正在寻找一种轮询方式,但是你的代码片段中使用 setTimeout() 方法只会在延迟后运行一次,不会持续调用 updateStatus 方法。如果你想要使用纯JS计时器,你需要考虑使用 setInterval()

如果你想要使用rxjs每隔几秒钟进行检查,一种解决方案是使用 timertimer 将在初始延迟后,每隔 period 毫秒发出一个事件。点击此处查看文档

你可以按照以下方式进行操作:

timer(delay, interval)
  .pipe(
    tap(() => this.updateStatus()),
    filter(() => this.status === 'Available'),
    take(1)
  ).subscribe(() => {
    console.log('Complete');
    //在这里执行工作
  });

这个示例使用 tap 操作符来每隔 interval 毫秒调用你的 updateStatus 方法,一旦你的 status 变量设置为 'Available',可观察对象将触发,subscribe 块中的代码将被执行。take(1) 确保只有一个发射通过,一旦状态不再是挂起的,你的可观察对象将被关闭,计时器将不再有任何活跃的订阅者。

如果你的意图是只检查一次,你也可以使用 timer 在延迟后运行一次,类似于你的 setTimeout() 示例:

timer(delay).subscribe(x => {
    this.updateStatus();
    //在这里做一些事情。
})
英文:

You've mentioned you want to keep checking the status until it's available. I'm assuming you're looking for some kind of polling, but your code snippet with the the setTimeout() method will only run once after a delay and won't keep calling the updateStatus method. If you wanted to do a vanilla JS timer you'd need to look at using setInterval()

If you want to check every few seconds using rxjs, one solution could be to use the timer. The timer will emit an event every period milliseconds, after an initial delay of delay milliseconds. See here for docs

You could do something along these lines:

timer(delay, interval)
  .pipe(
    tap(() => this.updateStatus())
    filter(() => this.status === 'Available'),
    take(1)
  ).subscribe(() => {
    console.log('Complete');
    //do work here
  });

This example uses the tap operator to call your updateStatus method every interval milliseconds after the initial delay has passed. Once your status var is set to 'Available', the observable will fire and the code in the subscribe block will be executed. The take(1) ensures that only one emission gets through once the status is no longer pending, so your observable will be closed and the timer will no longer have any active subscribers.

If your intent is to only check once, you can also use the timer to run once after a delay, similar to your setTimeout() example:

  timer(delay).subscribe(x => {
    this.updateStatus();
    //do something here.
  })

huangapple
  • 本文由 发表于 2023年2月8日 18:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定