-
Notifications
You must be signed in to change notification settings - Fork 67
Start implementation of BindingSubscriber #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Got it. I was able to make I'll let this sit for a week if anybody has feedback on how the API is organized.
|
Unfortunately, I don't think there is a way to return a |
I may rename the method to bind(), instead of toBinding(). That would make the purpose more clear. |
In my oppinion your first proposal ( See this example: TextField textInput = new TextField();
Observable<String> textInputs = JavaFxObservable.fromObservableValue(textInput.textProperty())
.map(s -> new StringBuilder(s).reverse().toString());
Label fippedTextLabel = new Label();
Binding flippedText = JavaFxSubscriber.toBinding(textInputs);
flippedTextLabel.textProperty().bind(Bindings.concat("Flipped text is:", flippedText)); With the Binding instance I'm more flexible on how to use it. In the example I'm using the Binding to create a new Binding that has a concatenation of the Binding value. Maybe it would be even better to return With the second approach I would have to create an extra Property to implement this: TextField textInput = new TextField();
Observable<String> textInputs = JavaFxObservable.fromObservableValue(textInput.textProperty())
.map(s -> new StringBuilder(s).reverse().toString());
Label fippedTextLabel = new Label();
StringProperty flippedText = new SimpleStringProperty();
textInputs.subscribe(JavaFxSubscriber.toBinding(flippedText));
flippedTextLabel.textProperty().bind(Bindings.concat("Flipped text is:", flippedText)); |
@lestard I forgot about those helper methods in So I think you are right. I should be returning a |
Okay you are right. This is much more intuitive and flexible. public class RxJavaFXTest extends Application {
private final Button incrementBttn;
private final Label incrementLabel;
private final TextField textInput;
private final Label fippedTextLabel;
private final Binding<String> binding1;
private final Binding<String> binding2;
public RxJavaFXTest() {
//initialize increment demo
incrementBttn = new Button("Increment");
incrementLabel = new Label("");
Observable<String> bttnEvents =
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION)
.map(e -> 1).scan(0,(x,y) -> x + y)
.map(Object::toString);
binding1 = JavaFxSubscriber.toBinding(bttnEvents);
incrementLabel.textProperty().bind(binding1);
//initialize text flipper
textInput = new TextField();
fippedTextLabel = new Label();
Observable<String> textInputs = JavaFxObservable.fromObservableValue(textInput.textProperty())
.observeOn(Schedulers.computation())
.map(s -> new StringBuilder(s).reverse().toString())
.observeOn(JavaFxScheduler.getInstance());
binding2 = JavaFxSubscriber.toBinding(textInputs);
fippedTextLabel.textProperty().bind(binding2);
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.add(incrementBttn,0,0);
gridPane.add(incrementLabel,1,0);
gridPane.add(textInput,0,1);
gridPane.add(fippedTextLabel, 1,1);
Scene scene = new Scene(gridPane);
primaryStage.setWidth(265);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
binding1.dispose();
binding2.dispose();
}
} It also makes it easier for us Kotlin people. We can easily turn this into an extension function on the fun bindingTest() {
val source = Observable.just("Alpha","Beta","Gamma");
val binding = source.toBinding()
}
//extension declaration
fun <T> Observable<T>.toBinding() = JavaFxSubscriber.toBinding(this) |
I think a |
You are right: Returning |
Start implementation of BindingSubscriber
Whomever is watching this project, I am starting the implementation of
Binding
support in a similar vein as ReactFX. The unfortunate constraint is since RxJavaFX does not own the RxJavaObservable
orSubscriber
types, it will probably feel somewhat like an anti-pattern to pass an entireObservable
to a static factory method, which subscribes to theObservable<T>
and returns aBinding<T>
.Unfortunately, there is no
compose()
equivalent for Subscribers that I know of. I cannot extend two classes for bothBinding
andObservableValueBase
, so I chose the latter.Please take a look at my PR and let me know if you have any suggestions to make this API as intuitive as possible.