为什么我要使用 RxJS interval() 或 timer() 轮询而不是 window.setInterval()? [英] Why would I use RxJS interval() or timer() polling instead of window.setInterval()?

查看:37
本文介绍了为什么我要使用 RxJS interval() 或 timer() 轮询而不是 window.setInterval()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:每分钟(60000 毫秒)调用一个函数,该函数分派商店操作以获取商品的 lastUpdated 状态,根据响应和过滤,更新商店,并且更新的存储被读取为可观察的并显示在视图中).只要网络应用程序处于打开状态(无限期),这就会发生.

Use case: Call a function every minute (60000 ms) that dispatches store action to fetch lastUpdated status of items, which upon response and filtering, updates the store, and updated store is read as an observable and displayed in the view). This needs to happen for as long as the web app is open (so indefinitely).

目前,我正在使用这个:

Currently, I'm using this:

this.refreshDate = window.setInterval(
  () => this.store.dispatch(new FetchLastUpdate())
, 60000);

当视图被销毁/卸载时,我会删除间隔:

And when view is destroyed/dismounted, I delete the interval as so:

if (this.refreshDate) {
  clearInterval(this.refreshDate);
}

这是高效/有效,还是麻烦?

Is this efficient/effective, or is it troublesome?

为什么我要使用 RxJS 轮询策略,例如:

Why would I want to use an RxJS polling strategy like:

interval(60000)
  .pipe(
    startWith(0),
    switchMap(() => this.store.dispatch(new FetchLastUpdate()))
   );

timer(0, 60000)
  .pipe(
    switchMap(() => this.store.dispatch(new FetchLastUpdate()))
  );

<小时>

TL;DR: window.setInterval() vs. RxJS timer()/interval()


TL;DR: window.setInterval() vs. RxJS timer()/interval()

使用 RxJS 函数来设置间隔或执行轮询有很大的好处,这些好处在 selected answer 中有解释 也在评论中,但结论是(通过评论中的讨论)对于本文开头用例"部分中定义的非常简单的需求post,没有必要使用RxJS,事实上,如果你没有在程序的任何其他部分使用RxJS,不要仅仅为了这个而导入它,但是在我的情况下,我已经在其他地方导入并使用了RxJS.

There is great benefit to using RxJS functions to set an interval or perform polling, these benefits are explained in the selected answer but also in comments, but it is concluded (by discussions in the comments) that for the very simple requirement defined in the "Use case" section at the beginning of this post, it is unnecessary to use RxJS, and in fact if you are not using RxJS in any other part of your program, do not import it just for this, however in my case, I had already imported and used RxJS elsewhere.

推荐答案

RxJS 的优势:

懒惰

你可以创建你的 Observables 并且在你调用 subscribe 之前什么都不会发生.Observable = 纯函数.这给你更多的控制,更容易的推理,并允许下一点......

You can create your Observables and until you call subscribe nothing is happening. Observable = pure function. This gives you more control, easier reasoning and allows for next point...

可组合性

您可以将 interval/timer 与其他 operators 结合起来,以统一的方式非常轻松地创建自定义逻辑 - 例如您可以maprepeatretrytake...等等.查看所有操作符

You can combine interval/timer with other operators creating custom logic very easily in unified way - for example you can map, repeat, retry, take... etc. see all operators

错误处理

如果出现错误,您负责调用 clearTimeout/clearInterval - Observables 会为您处理.导致更干净的代码更少的内存泄漏错误.

In case of an error you are responsible for calling clearTimeout/clearInterval - Observables are handling this for you. Resulting in cleaner code and fewer memory leak bugs.

当然,你用 Observables 做的任何事情你也可以在没有 Observables 的情况下做——但这不是重点.Observable 旨在让您的生活更轻松.

Of course anything you do with Observables you can also do without Observables - but that's not the point. Observables are here to make your life easier.

另请注意,interval/timer 不是用于轮询的良好可观察工厂,因为它们不会等待"您的异步操作完成(您可能会以多个异步调用相互运行而告终).为此,我倾向于像这样使用 deferrepeatWhen:

Also note that interval/timer are not good observable factories for polling because they do not "wait" for your async action to finish (you can end up with multiple async calls running over each other). For that I tend to use defer and repeatWhen like this:

defer(() => doAsyncAction())
  .pipe(
    repeatWhen(notifications => notifications.pipe(delay(1234)))
  );

这篇关于为什么我要使用 RxJS interval() 或 timer() 轮询而不是 window.setInterval()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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