2018-09-02 03:47:24 +02:00
|
|
|
package org.warp.picalculator.flow;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
2018-09-02 12:45:51 +02:00
|
|
|
public class ObservableCombinedChanged<T, U> extends Observable<Pair<T, U>> {
|
2018-09-02 03:47:24 +02:00
|
|
|
private volatile boolean initialized = false;
|
|
|
|
private Observable<T> a;
|
|
|
|
private Observable<U> b;
|
|
|
|
private Disposable disposableA;
|
|
|
|
private Disposable disposableB;
|
2018-09-02 12:45:51 +02:00
|
|
|
|
2018-09-02 03:47:24 +02:00
|
|
|
public ObservableCombinedChanged(Observable<T> a, Observable<U> b) {
|
|
|
|
super();
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialize() {
|
|
|
|
this.disposableA = a.subscribe((t) -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onNext(Pair.of(t, null));
|
|
|
|
}
|
|
|
|
}, (e) -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onError(e);
|
2018-09-02 12:45:51 +02:00
|
|
|
} ;
|
2018-09-02 03:47:24 +02:00
|
|
|
}, () -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onComplete();
|
2018-09-02 12:45:51 +02:00
|
|
|
} ;
|
2018-09-02 03:47:24 +02:00
|
|
|
});
|
|
|
|
this.disposableB = b.subscribe((t) -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onNext(Pair.of(null, t));
|
2018-09-02 12:45:51 +02:00
|
|
|
} ;
|
2018-09-02 03:47:24 +02:00
|
|
|
}, (e) -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onError(e);
|
2018-09-02 12:45:51 +02:00
|
|
|
} ;
|
2018-09-02 03:47:24 +02:00
|
|
|
}, () -> {
|
|
|
|
for (Subscriber<? super Pair<T, U>> sub : this.subscribers) {
|
|
|
|
sub.onComplete();
|
2018-09-02 12:45:51 +02:00
|
|
|
} ;
|
2018-09-02 03:47:24 +02:00
|
|
|
});
|
|
|
|
}
|
2018-09-02 12:45:51 +02:00
|
|
|
|
2018-09-02 03:47:24 +02:00
|
|
|
private void chechInitialized() {
|
|
|
|
if (!initialized) {
|
|
|
|
initialized = true;
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 12:45:51 +02:00
|
|
|
|
2018-09-02 03:47:24 +02:00
|
|
|
@Override
|
2018-09-02 12:45:51 +02:00
|
|
|
public Disposable subscribe(Subscriber<? super Pair<T, U>> sub) {
|
2018-09-02 03:47:24 +02:00
|
|
|
Disposable disp = super.subscribe(sub);
|
|
|
|
chechInitialized();
|
|
|
|
return disp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisposed(Subscriber<? super Pair<T, U>> sub) {
|
|
|
|
super.onDisposed(sub);
|
|
|
|
this.disposableA.dispose();
|
|
|
|
this.disposableB.dispose();
|
|
|
|
}
|
|
|
|
}
|