为什么要使用RxJS interval()或timer()轮询而不是window.setInterval()?

编程入门 行业动态 更新时间:2024-10-28 02:27:02
本文介绍了为什么要使用RxJS interval()或timer()轮询而不是window.setInterval()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

用例:每分钟(60000毫秒)调用一个函数,该函数调度商店操作以获取商品的 lastUpdated 状态,并在响应和过滤后更新商店,且已更新的商店将被视为可观察并显示在视图中).只要Web应用处于打开状态(无限期),就需要执行此操作.

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).

当前,我正在使用此

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()与RxJS timer()/ interval()

使用RxJS函数设置间隔或执行轮询有很大的好处,这些好处在 选择的答案中进行了说明 以及评论中,但结论(通过评论中的讨论)得出结论,对于本文开头的"用例"部分中定义的非常简单的要求帖子中,没有必要使用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 为止,什么都没有发生.可观察=纯函数.这样可以给您更多的控制权,更轻松的推理,并允许下一点...

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 与其他 operator 结合在一起,以统一的方式非常轻松地创建自定义逻辑-例如,您可以 map , repeat , retry , take ...等.查看所有运算符

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的情况下进行-但这不是重点.可观察的物体在这里使您的生活更轻松.

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 并不是很好的可观察的轮询工厂,因为它们不会等待"异步操作完成(您最终可能会在彼此之间运行多个异步调用).为此,我倾向于像这样使用 defer 和 repeatWhen :

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()?

本文发布于:2023-10-24 05:59:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要使   而不是   RxJS   timer   interval

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!