簡體   English   中英

RxJava反跳運算符取決於消息

[英]RxJava debounce operator depend on message

我研究RxJava並嘗試了解如何實現非標准的反應式“推銷”邏輯。 取決於消息,新操作員必須延遲某種消息,或者如果可觀察到另一種消息,則跳過該消息。

僅取消取消A消息,如果其他消息到達則忽略它

請幫助我撰寫此邏輯。

這需要運算符的簡單組合:

public static <T> ObservableTransformer<T, T> debounceOnly(
        Predicate<? super T> condition, long time, 
        TimeUnit unit, Scheduler scheduler) {
    return o -> o.publish(f ->
        f.concatMapEager(v -> {
            if (condition.test(v)) {
                return Observable.just(v).delay(time, unit, scheduler).takeUntil(f);
            }
            return Observable.just(v);
        })
    );
}


@Test
public void test() {
    PublishSubject<String> subject = PublishSubject.create();

    TestScheduler sch = new TestScheduler();

    subject
    .compose(debounceOnly(v -> v.startsWith("A"), 
         100, TimeUnit.MILLISECONDS, sch))
    .subscribe(System.out::println, Throwable::printStackTrace, 
         () -> System.out.println("Done"));

    subject.onNext("A1");

    sch.advanceTimeBy(100, TimeUnit.MILLISECONDS);

    subject.onNext("B1");
    sch.advanceTimeBy(1, TimeUnit.MILLISECONDS);

    subject.onNext("C1");
    sch.advanceTimeBy(1, TimeUnit.MILLISECONDS);

    subject.onNext("A2");
    sch.advanceTimeBy(50, TimeUnit.MILLISECONDS);

    subject.onNext("A3");
    sch.advanceTimeBy(100, TimeUnit.MILLISECONDS);

    subject.onNext("A4");
    sch.advanceTimeBy(50, TimeUnit.MILLISECONDS);

    subject.onNext("B2");
    sch.advanceTimeBy(100, TimeUnit.MILLISECONDS);

    subject.onNext("C2");
    sch.advanceTimeBy(100, TimeUnit.MILLISECONDS);

    subject.onComplete();
}

RxJava 1.2.10的Kotlin代碼:

val sequence = listOf(
            Pair(0, "A"),  // delay
            Pair(3, "B"),  // immediately
            Pair(4, "C"),  // immediately
            Pair(5, "A"),  // skip by next A
            Pair(6, "A"),  // delay
            Pair(9, "A"),  // skip by next B
            Pair(10, "B"), // immediately
            Pair(11, "C")  // immediately
    )

    val startTime = LocalDateTime.now()
    Observable
            .from(sequence)
            .flatMap { tm ->
                Observable.just(tm.second)
                        .delay(tm.first.toLong(), TimeUnit.SECONDS)
            }
            .compose { o ->
                o.publish { f ->
                    f.concatMap { v ->
                        if (v == "A")
                            Observable.just(v).delay(2, TimeUnit.SECONDS).takeUntil(f)
                        else
                            Observable.just(v)
                    }
                }
            }
            .toBlocking()
            .subscribe {
                        val currentTime = LocalDateTime.now()
                        val sec = currentTime.toEpochSecond(ZoneOffset.UTC) - startTime.toEpochSecond(ZoneOffset.UTC)
                        println("$sec - $it")
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM