立即交付第一个项目,“去抖动"后续项目 [英] Deliver the first item immediately, 'debounce' following items
问题描述
考虑以下用例:
- 需要尽快交付第一件商品
- 需要在 1 秒超时后去抖动以下事件
我最终实现了基于 OperatorDebounceWithTime
的自定义运算符,然后像这样使用它
I ended up implementing custom operator based on OperatorDebounceWithTime
then using it like this
.lift(new CustomOperatorDebounceWithTime<>(1, TimeUnit.SECONDS, Schedulers.computation()))
CustomOperatorDebounceWithTime
立即交付第一个项目,然后使用 OperatorDebounceWithTime
运算符的逻辑来debounce 后面的项目.
CustomOperatorDebounceWithTime
delivers the first item immediately then uses OperatorDebounceWithTime
operator's logic to debounce later items.
是否有更简单的方法来实现所描述的行为?让我们跳过 compose
操作符,它不能解决问题.我正在寻找一种无需实现自定义运算符即可实现此目的的方法.
Is there an easier way to achieve described behavior? Let's skip the compose
operator, it doesn't solve the problem. I'm looking for a way to achieve this without implementing custom operators.
推荐答案
更新:
根据@lopar 的评论,更好的方法是:
Update:
From @lopar's comments a better way would be:
Observable.from(items).publish(publishedItems -> publishedItems.limit(1).concatWith(publishedItems.skip(1).debounce(1, TimeUnit.SECONDS)))
像这样的工作:
String[] items = {"one", "two", "three", "four", "five", "six", "seven", "eight"};
Observable<String> myObservable = Observable.from(items);
Observable.concat(myObservable.first(), myObservable.skip(1).debounce(1, TimeUnit.SECONDS))
.subscribe(s -> System.out.println(s));
这篇关于立即交付第一个项目,“去抖动"后续项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!