Rx debounce运算符有第一个和最后一个 [英] Rx debounce operator with first and last

查看:71
本文介绍了Rx debounce运算符有第一个和最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有rx运算符的任意组合才能获得第一个和最后一个去抖动事件?

Is there any combination of rx operators so as to get the first and last debounced event?

这将用于主细节场景(甚至是seach)方案)我们想要立即加载第一个选定项目,最后一个用户停止更改选择。

This is going to be used in master detail scenarios (or even seach scenarios) where we want immediate loading of first selected item and the last after user stops changing selection.

这可以防止在用户缓慢导航时注入去抖时间但还可以防止突发变化。

This would prevent the debounce time to be injected when the user navigates slowly but also prevent bursts of changes.

如果去抖动运营商有一个立即选项,如underscore.js debounce functoin ,然后合并两个版本debounce运算符会生成所需的结果。

If debounce operator had an option "immediate" like underscore.js debounce functoin then a merge of the 2 versions of debounce operator would generate the needed result.

推荐答案

要获得第一个去抖动元素,可以使用油门 。要获得最后一个,您可以使用 去抖 。但是,您应该确保您正在进行去抖动或限制的源是一个热门来源,因为您将两个运算符应用于同一个源。然后,如果你想要同一个observable中的两个元素,那么你可以合并限制和去抖动的流。

To get the the first debounced element, you can use throttle. To get the last, you can use debounce. You however should make sure that the source you are debouncing or throttling is a hot source as you will apply both operators to the same source. Then if you want both elements in the same observable, then you can just merge the throttled and debounced streams.

例如,调用 source $ 你的来源可观察:

For example, calling source$ your source observable:

firstAndLast$ = Rx.Observable.merge(source$.debounce(Xms), source$.throttle(Xms))

这应该发出第一项和最后一项在同一个流中爆发。请注意,在边缘情况下,这不是一个突发,而是在Xms时间段内发生的单个值,您可能有两倍相同的值,一个在句点的开头,一个在结尾。一种防止这种情况的方法,如果有意义的是使用 distinctUntilChanged ,那么这就变成:

This should issue the first item and the last item of a burst in the same stream. Note that in the edge case that this is not a burst but a single value that is occuring within the Xms time period, you might have twice the same value, one at the beginning of the period, and one at the end. A way to protect against that, if that makes sense is to use distinctUntilChanged, so this becomes:

firstAndLast$ = Rx.Observable.merge(source$.debounce(Xms), source$.throttle(Xms)).distinctUntilChanged()

这篇关于Rx debounce运算符有第一个和最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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