RxJava - 用另一个控制一个Observable

时间:2018-02-13 13:19:42

标签: java rx-java rx-java2

更新

我正在寻找一种方法来控制另一个观察者的流量。例如,让我们有2个单调增加(重要)整数的可观察量:

source  : 1----2-2---2--3--3--4----4--5---6----8---9---10--------11------
control : -1----3----------------5-----------6-------9-----------12------

我需要生成一个新的observable,其元素与源完全匹配,但它们的时序由控制可观察的控制方式如下:源值应始终小于或等于控制值。这意味着只有所有源值(大于最近发布的控件)应该等到它们被控件“释放”

source         : 1----2-2---2--3--3--4----4--5---6----8---9---10--------11------
control        : -1----3----------------5-----------6-------9-----------12------
expected result: -1----2-2--2--3--3-----4-4--5------6-------8-9---------10-11---

请查看以下代码示例:

private static <T, C> Observable<T> combine(Observable<T> source, Observable<C> control, BiFunction<T, C, Boolean> predicate) {
    // ???
}

@Test
public void testControl() throws InterruptedException {
    Subject<Integer> control = PublishSubject.create();
    Observable<Integer> source = Observable.fromArray(1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 8, 10, 11);
    Observable<Integer> combined = combine(source, control, (s, c) -> s <= c);
    control.subscribe(val -> System.out.println("Control: " + val));
    combined.observeOn(Schedulers.io()).subscribe(val -> System.out.println("Value: " + val));

    control.onNext(3); // should release 1,2,2,2,3,3
    Thread.sleep(1000);
    control.onNext(6); // should release 4,4,5,6
    Thread.sleep(1000);
    control.onNext(11); // should release 8,10,11
    Thread.sleep(1000);
}

2 个答案:

答案 0 :(得分:1)

由于我没有找到任何优雅的解决方案,我最终自己实现了它。如果我有人可以提出更优雅的解决方案,我将很高兴(在这种情况下,我将不接受这个答案,并接受更好的解决方案)。以下是我的解决方案:

private static <T, C> Observable<T> combine(Observable<T> source, Observable<C> control, BiFunction<T, C, Boolean> predicate) {
    return Observable.create(emitter -> {
        Queue<T> buffer = new ArrayDeque<>();
        AtomicReference<C> lastControl = new AtomicReference<>();
        CompletableSubject sourceCompletable = CompletableSubject.create();
        CompletableSubject controlCompletable = CompletableSubject.create();
        Disposable disposable = new CompositeDisposable(
                control.subscribe(
                        val -> {
                            lastControl.set(val);
                            synchronized (buffer) {
                                while (!buffer.isEmpty() && predicate.apply(buffer.peek(), val)) {
                                    emitter.onNext(buffer.poll());
                                }
                            }
                        },
                        emitter::onError,
                        controlCompletable::onComplete),
                source.subscribe(
                        val -> {
                            C lastControlVal = lastControl.get();
                            synchronized (buffer) {
                                if (lastControlVal != null && predicate.apply(val, lastControlVal)) {
                                    emitter.onNext(val);
                                } else {
                                    buffer.add(val);
                                }
                            }
                        },
                        emitter::onError,
                        sourceCompletable::onComplete),
                controlCompletable.andThen(sourceCompletable).subscribe(emitter::onComplete));
        emitter.setDisposable(disposable);
    });
}

答案 1 :(得分:0)

您需要将buffer功能(将为您缓冲项目)与combineLatest功能组合在一起(这将确定何时可以释放缓冲区)。

Observable<Integer> source;
Observable<Integer> control;

Observable<Integer> canRelease = Observable.combineLatest(source, control, (s, c) -> s < c ? s : null).filter(val -> val != null);

Observable<Integer> result = source.buffer(canRelease).flatMap(Observable::from);

生成的Observable将缓冲源项,直到canRelease中有值。每次源Observable最新项目变得小于控制Observable最新项目时,canRelease将发出;