Skip to content

Commit 8bef523

Browse files
committed
start implementation of BindingSubscriber
1 parent 6f1efff commit 8bef523

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 javafx.beans.value.ObservableValueBase;
21+
import javafx.collections.ObservableList;
22+
import rx.Observable;
23+
import rx.Subscriber;
24+
import rx.functions.Action1;
25+
26+
import java.util.Optional;
27+
28+
29+
public final class BindingSubscriber<T> extends ObservableValueBase<T> implements Binding<T> {
30+
31+
private final Subscriber<T> subscriber;
32+
33+
private T value;
34+
35+
BindingSubscriber(Observable<T> observable, Optional<T> initialValue, final Action1<Throwable> onError) {
36+
37+
this.subscriber = new Subscriber<T>() {
38+
@Override
39+
public void onCompleted() {
40+
//do nothing
41+
}
42+
43+
@Override
44+
public void onError(Throwable e) {
45+
onError.call(e);
46+
}
47+
48+
@Override
49+
public void onNext(T item) {
50+
value = item;
51+
fireValueChangedEvent();
52+
}
53+
};
54+
55+
observable.subscribe(subscriber);
56+
57+
}
58+
@Override
59+
public T getValue() {
60+
return value;
61+
}
62+
@Override
63+
public boolean isValid() {
64+
return true;
65+
}
66+
67+
@Override
68+
public void invalidate() {
69+
//does nothing
70+
}
71+
72+
@Override
73+
public ObservableList<?> getDependencies() {
74+
throw new UnsupportedOperationException();
75+
}
76+
77+
@Override
78+
public void dispose() {
79+
subscriber.unsubscribe();
80+
}
81+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 rx.Observable;
20+
import rx.functions.Action1;
21+
22+
import java.util.Optional;
23+
24+
public enum JavaFxSubscriber {
25+
;//no instances
26+
27+
public static <T> BindingSubscriber<T> toBinding(Observable<T> observable) {
28+
return new BindingSubscriber<T>(observable, Optional.empty(), e -> {});
29+
}
30+
public static <T> BindingSubscriber<T> toBinding(Observable<T> observable,Action1<Throwable> onErrorAction ) {
31+
return new BindingSubscriber<T>(observable, Optional.empty(), onErrorAction);
32+
}
33+
public static <T> BindingSubscriber<T> toBinding(Observable<T> observable, T initialValue, Action1<Throwable> onErrorAction ) {
34+
return new BindingSubscriber<T>(observable, Optional.of(initialValue), onErrorAction);
35+
}
36+
public static <T> BindingSubscriber<T> toBinding(Observable<T> observable, T initialValue) {
37+
return new BindingSubscriber<T>(observable, Optional.of(initialValue), e -> {});
38+
}
39+
}

0 commit comments

Comments
 (0)