Skip to content

Commit 01f2466

Browse files
committed
Merge pull request #20 from thomasnield/0.x
Start implementation of BindingSubscriber
2 parents 6f1efff + bad69a4 commit 01f2466

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.Subscriber;
26+
import rx.Subscription;
27+
import rx.functions.Action1;
28+
29+
30+
final class BindingSubscriber<T> extends Subscriber<T> implements ObservableValue<T>, Binding<T>, Subscription {
31+
32+
private final Action1<Throwable> onError;
33+
private ExpressionHelper<T> helper;
34+
private T value;
35+
36+
BindingSubscriber(final Action1<Throwable> onError) {
37+
this.onError = onError;
38+
}
39+
@Override
40+
public void onCompleted() {
41+
//do nothing
42+
}
43+
44+
@Override
45+
public void onError(Throwable e) {
46+
onError.call(e);
47+
}
48+
49+
@Override
50+
public void onNext(T t) {
51+
value = t;
52+
fireValueChangedEvent();
53+
}
54+
@Override
55+
public T getValue() {
56+
return value;
57+
}
58+
@Override
59+
public boolean isValid() {
60+
return true;
61+
}
62+
63+
@Override
64+
public void invalidate() {
65+
//does nothing
66+
}
67+
68+
@Override
69+
public ObservableList<?> getDependencies() {
70+
throw new UnsupportedOperationException();
71+
}
72+
73+
@Override
74+
public void dispose() {
75+
this.unsubscribe();
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
@Override
82+
public void addListener(InvalidationListener listener) {
83+
helper = ExpressionHelper.addListener(helper, this, listener);
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
@Override
90+
public void addListener(ChangeListener<? super T> listener) {
91+
helper = ExpressionHelper.addListener(helper, this, listener);
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*/
97+
@Override
98+
public void removeListener(InvalidationListener listener) {
99+
helper = ExpressionHelper.removeListener(helper, listener);
100+
}
101+
102+
/**
103+
* {@inheritDoc}
104+
*/
105+
@Override
106+
public void removeListener(ChangeListener<? super T> listener) {
107+
helper = ExpressionHelper.removeListener(helper, listener);
108+
}
109+
110+
/**
111+
* Notify the currently registered observers of a value change.
112+
*
113+
* This implementation will ignore all adds and removes of observers that
114+
* are done while a notification is processed. The changes take effect in
115+
* the following call to fireValueChangedEvent.
116+
*/
117+
protected void fireValueChangedEvent() {
118+
ExpressionHelper.fireValueChangedEvent(helper);
119+
}
120+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 javafx.beans.binding.Binding;
20+
import rx.Observable;
21+
import rx.functions.Action1;
22+
23+
public enum JavaFxSubscriber {
24+
;//no instances
25+
26+
public static <T> Binding<T> toBinding(Observable<T> obs) {
27+
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(e -> {});
28+
obs.subscribe(bindingSubscriber);
29+
return bindingSubscriber;
30+
}
31+
public static <T> Binding<T> toBinding(Observable<T> obs, Action1<Throwable> onErrorAction ) {
32+
BindingSubscriber<T> bindingSubscriber = new BindingSubscriber<>(onErrorAction);
33+
obs.subscribe(bindingSubscriber);
34+
return bindingSubscriber;
35+
}
36+
}

0 commit comments

Comments
 (0)