Rx立即反弹 [英] Immediate debounce in Rx

查看:88
本文介绍了Rx立即反弹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找操作员来去抖动一系列事件,我们说用户的点击。输入和输出应如下所示:

  interval:-> <--> <-
in:1--2--3 ------- 4--5--5--6-7-8 --------
出:1 ------------- 4 ---------------------

这个想法就像下划线的反跳,并在上使用立即选项c> ,可在原始文章

解决方案

编辑:根据说明,RxJava没有此类流的运算符,但可以由一组非平凡的其他运算符组成: / p>

  import java.util.concurrent.TimeUnit; 

导入rx。

公共类DebounceFirst {

public static void main(String [] args){
Observable.just(0,100,200,1500,1600,1800 ,2000,10000)
.flatMap(v-> Observable.timer(v,TimeUnit.MILLISECONDS).map(w-> v))
.doOnNext(v-> System.out .println( T = + v))
.compose(debounceFirst(500,TimeUnit.MILLISECONDS))
.toBlocking()
.subscribe(v-> System.out。 println(防反跳: + v));
}

静态< T>可观察的变压器T,T debounceFirst(长时间超时,TimeUnit单位){
return f->
f.publish(g->
g.take(1)
.concatWith(
g.switchMap(u-> Observable.timer(timeout,unit)。 map(w-> u))
.take(1)
.ignoreElements()

.repeatWhen(h-> h.takeUntil(g.ignoreElements( )))
);
}
}


I am looking an operator to debounce a series of event, let us say user's click. The input and output should be like this:

interval :      ->     <-            ->     <-
in       : 1--2--3-------4--5--5--6-7-8--------
out      : 1-------------4---------------------

The idea is like underscore's debounce with immediate option on http://underscorejs.org/#debounce. The operator can be presented/implemented in any language that supports Reactive Extensions

Edit: Clarify the interval, let say 5 seconds (5 spaces between two arrows) : -> <-

Edit2: A more understandable version: I have a user, he clicks repeatedly a button (1, 2, 3); I want to catch the first click (1) and ignore the rest. After a while, he is tired and rest for 7 seconds (which is longer than the 5 seconds interval between two arrows) and continue clicking the button again (4, 5, 6, 7, 8) I want to catch the first click (4) and ignore the rest.

If he clicks after the forth arrow, I want to catch that click, too.

Edit3: Here is an image which can be found at the original article

解决方案

Edit: Based on the clarifications, RxJava doesn't have an operator for this type of flow but it can be composed from a non-trivial set of other operators:

import java.util.concurrent.TimeUnit;

import rx.Observable;

public class DebounceFirst {

    public static void main(String[] args) {
        Observable.just(0, 100, 200, 1500, 1600, 1800, 2000, 10000)
        .flatMap(v -> Observable.timer(v, TimeUnit.MILLISECONDS).map(w -> v))
        .doOnNext(v -> System.out.println("T=" + v))
        .compose(debounceFirst(500, TimeUnit.MILLISECONDS))
        .toBlocking()
        .subscribe(v -> System.out.println("Debounced: " + v));
    }

    static <T> Observable.Transformer<T, T> debounceFirst(long timeout, TimeUnit unit) {
        return f -> 
            f.publish(g ->
                g.take(1)
                .concatWith(
                    g.switchMap(u -> Observable.timer(timeout, unit).map(w -> u))
                    .take(1)
                    .ignoreElements()
                )
                .repeatWhen(h -> h.takeUntil(g.ignoreElements()))
            );
    }
}

这篇关于Rx立即反弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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