Skip to content

Commit aca3900

Browse files
committed
Consolidate and lean up lazy bindings
1 parent b923ece commit aca3900

File tree

3 files changed

+24
-116
lines changed

3 files changed

+24
-116
lines changed

src/main/java/rx/subscribers/BindingSubscriber.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,37 @@
2222
import javafx.beans.value.ChangeListener;
2323
import javafx.beans.value.ObservableValue;
2424
import javafx.collections.ObservableList;
25+
import rx.Observable;
2526
import rx.Subscriber;
2627
import rx.Subscription;
2728
import rx.functions.Action1;
2829

2930

3031
final class BindingSubscriber<T> extends Subscriber<T> implements ObservableValue<T>, Binding<T>, Subscription {
3132

33+
private final Observable<T> observable;
3234
private final Action1<Throwable> onError;
35+
private Subscription subscription;
3336
private ExpressionHelper<T> helper;
3437
private T value;
3538

36-
BindingSubscriber(final Action1<Throwable> onError) {
39+
private BindingSubscriber(Observable<T> observable, Action1<Throwable> onError) {
40+
this.observable = observable;
3741
this.onError = onError;
3842
}
43+
44+
public static <T> BindingSubscriber<T> forObservable(Observable<T> observable, Action1<Throwable> onError, boolean isLazy) {
45+
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(observable, onError);
46+
47+
if (!isLazy) {
48+
bindingSubscriber.connect();
49+
}
50+
51+
return bindingSubscriber;
52+
}
53+
private void connect() {
54+
subscription = observable.subscribe(this);
55+
}
3956
@Override
4057
public void onCompleted() {
4158
//do nothing
@@ -53,6 +70,9 @@ public void onNext(T t) {
5370
}
5471
@Override
5572
public T getValue() {
73+
if (subscription == null) {
74+
connect();
75+
}
5676
return value;
5777
}
5878
@Override

src/main/java/rx/subscribers/JavaFxSubscriber.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ public enum JavaFxSubscriber {
2626
* Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription.
2727
*/
2828
public static <T> Binding<T> toBinding(Observable<T> obs) {
29-
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(e -> {});
30-
obs.subscribe(bindingSubscriber);
31-
return bindingSubscriber;
29+
return BindingSubscriber.forObservable(obs, e -> {}, false);
3230
}
3331
/**
3432
* Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription.
3533
*/
3634
public static <T> Binding<T> toBinding(Observable<T> obs, Action1<Throwable> onErrorAction ) {
37-
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(onErrorAction);
38-
obs.subscribe(bindingSubscriber);
39-
return bindingSubscriber;
35+
return BindingSubscriber.forObservable(obs, onErrorAction, false);
4036
}
4137

4238
/**
@@ -50,7 +46,6 @@ public static <T> Binding<T> toLazyBinding(Observable<T> obs) {
5046
* Turns an Observable into a lazy JavaFX Binding that subscribes to the Observable the first time the Binding's getValue() method is called. Calling the Binding's dispose() method will handle the unsubscription.
5147
*/
5248
public static <T> Binding<T> toLazyBinding(Observable<T> obs, Action1<Throwable> onErrorAction ) {
53-
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(onErrorAction);
54-
return new LazyBindingSubscriber<>(obs, bindingSubscriber);
49+
return BindingSubscriber.forObservable(obs, onErrorAction, true);
5550
}
5651
}

src/main/java/rx/subscribers/LazyBindingSubscriber.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)