Skip to content

Commit b00e15b

Browse files
committed
implement LazyBindingSubscriber
1 parent f779056 commit b00e15b

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,34 @@
2323
public enum JavaFxSubscriber {
2424
;//no instances
2525
/**
26-
* Turns an Observable into a JavaFX Binding. Calling the Binding's dispose() method will handle the unsubscription.
26+
* 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) {
2929
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(e -> {});
3030
obs.subscribe(bindingSubscriber);
3131
return bindingSubscriber;
3232
}
3333
/**
34-
* Turns an Observable into a JavaFX Binding. Calling the Binding's dispose() method will handle the unsubscription.
34+
* Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription.
3535
*/
3636
public static <T> Binding<T> toBinding(Observable<T> obs, Action1<Throwable> onErrorAction ) {
3737
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(onErrorAction);
3838
obs.subscribe(bindingSubscriber);
3939
return bindingSubscriber;
4040
}
41+
42+
/**
43+
* 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.
44+
*/
45+
public static <T> Binding<T> toLazyBinding(Observable<T> obs) {
46+
return toLazyBinding(obs, e -> {});
47+
}
48+
49+
/**
50+
* 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.
51+
*/
52+
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);
55+
}
4156
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package rx.subscribers;
18+
19+
import com.sun.javafx.binding.ExpressionHelper;
20+
import javafx.beans.InvalidationListener;
21+
import javafx.beans.binding.Binding;
22+
import javafx.beans.value.ChangeListener;
23+
import javafx.beans.value.ObservableValue;
24+
import javafx.collections.ObservableList;
25+
import rx.Observable;
26+
import rx.Subscription;
27+
28+
29+
final class LazyBindingSubscriber<T> implements ObservableValue<T>, Binding<T> {
30+
31+
private final Observable<T> observable;
32+
private final BindingSubscriber<T> binding;
33+
private ExpressionHelper<T> helper;
34+
private Subscription subscription;
35+
36+
LazyBindingSubscriber(Observable<T> observable, BindingSubscriber<T> binding) {
37+
this.observable = observable;
38+
this.binding = binding;
39+
}
40+
@Override
41+
public T getValue() {
42+
if (subscription == null) {
43+
subscription = observable.subscribe(binding);
44+
}
45+
return binding.getValue();
46+
}
47+
@Override
48+
public boolean isValid() {
49+
return true;
50+
}
51+
52+
@Override
53+
public void invalidate() {
54+
//does nothing
55+
}
56+
57+
@Override
58+
public ObservableList<?> getDependencies() {
59+
throw new UnsupportedOperationException();
60+
}
61+
62+
@Override
63+
public void dispose() {
64+
if (subscription != null) {
65+
binding.dispose();
66+
}
67+
}
68+
69+
/**
70+
* {@inheritDoc}
71+
*/
72+
@Override
73+
public void addListener(InvalidationListener listener) {
74+
helper = ExpressionHelper.addListener(helper, this, listener);
75+
}
76+
77+
/**
78+
* {@inheritDoc}
79+
*/
80+
@Override
81+
public void addListener(ChangeListener<? super T> listener) {
82+
helper = ExpressionHelper.addListener(helper, this, listener);
83+
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
@Override
89+
public void removeListener(InvalidationListener listener) {
90+
helper = ExpressionHelper.removeListener(helper, listener);
91+
}
92+
93+
/**
94+
* {@inheritDoc}
95+
*/
96+
@Override
97+
public void removeListener(ChangeListener<? super T> listener) {
98+
helper = ExpressionHelper.removeListener(helper, listener);
99+
}
100+
101+
/**
102+
* Notify the currently registered observers of a value change.
103+
*
104+
* This implementation will ignore all adds and removes of observers that
105+
* are done while a notification is processed. The changes take effect in
106+
* the following call to fireValueChangedEvent.
107+
*/
108+
protected void fireValueChangedEvent() {
109+
ExpressionHelper.fireValueChangedEvent(helper);
110+
}
111+
}

0 commit comments

Comments
 (0)