Skip to content

Commit 7462d71

Browse files
committed
Merge remote-tracking branch 'origin/0.x' into 0.x
2 parents ffd3e74 + a3daa2f commit 7462d71

File tree

15 files changed

+473
-30
lines changed

15 files changed

+473
-30
lines changed

README.md

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Learn more about RxJava on the <a href="https://github.com/ReactiveX/RxJava/wiki">Wiki Home</a> and the <a href="http://techblog.netflix.com/2013/02/rxjava-netflix-api.html">Netflix TechBlog post</a> where RxJava was introduced.
44

5-
RxJavaFX is a simple API to convert JavaFX events into RxJava Observables. It also has a scheduler to safely move emissions to the JavaFX Event Dispatch Thread.
5+
RxJavaFX is a simple API to convert JavaFX events into RxJava Observables and vice versa. It also has a scheduler to safely move emissions to the JavaFX Event Dispatch Thread.
66

77
## Master Build Status
88

@@ -55,7 +55,7 @@ $ ./gradlew build
5555
## Features
5656

5757
RxJavaFX has a lightweight set of features:
58-
- Factories to turn `Node`, `ObservableValue`, and other component events into an RxJava `Observable`
58+
- Factories to turn `Node`, `ObservableValue`, `ObservableList`, and other component events into an RxJava `Observable`
5959
- Factories to turn an RxJava `Observable` into a JavaFX `Binding`.
6060
- A scheduler for the JavaFX dispatch thread
6161

@@ -137,6 +137,32 @@ Subscription subscription = JavaFxObservable.fromObservableValueChanges(spinner.
137137
.subscribe(spinnerChangesLabel::setText);
138138
```
139139

140+
###ObservableList
141+
142+
There are eight factories in `JavaFxObservable` for turning an `ObservableList` into an `Observable` of some form based on a `ListChange` event.
143+
144+
```java
145+
public static <T> Observable<ObservableList<T>> fromObservableList(final ObservableList<T> source
146+
147+
public static <T> Observable<T> fromObservableListAdds(final ObservableList<T> source)
148+
149+
public static <T> Observable<T> fromObservableListRemovals(final ObservableList<T> source)
150+
151+
public static <T> Observable<T> fromObservableListUpdates(final ObservableList<T> source)
152+
153+
public static <T> Observable<ListChange<T>> fromObservableListChanges(final ObservableList<T> source)
154+
155+
public static <T> Observable<ListChange<T>> fromObservableListDistinctChanges(final ObservableList<T> source)
156+
157+
public static <T,R> Observable<ListChange<T>> fromObservableListDistinctChanges(final ObservableList<T> source, Func1<T,R> mapper)
158+
159+
public static <T,R> Observable<ListChange<R>> fromObservableListDistinctMappings(final ObservableList<T> source, Func1<T,R> mapper)
160+
```
161+
162+
The first factory`fromObservableList()` will simply emit the entire `ObservableList` every time there is `ListChange` event fired. The rest of the factories fire only items impacted by the `ListChange` events. The last three emit only *distinct* additions and removals, which can be helpful to emit only the first item with a given `hashcode()`/`equals()` and ignore dupe additions. When the last item (meaning no more dupes) is removed, only then will it fire the removal.
163+
164+
See the JavaDocs for a more detailed description of each one.
165+
140166
###Binding
141167
You can convert an RxJava `Observable` into a JavaFX `Binding` by calling the `JavaFxSubscriber.toBinding()` factory. Calling the `dispose()` method on the `Binding` will handle the unsubscription from the `Observable`. You can then take this `Binding` to bind other control properties to it.
142168

@@ -146,9 +172,12 @@ Label incrementLabel = new Label("");
146172

147173
Observable<ActionEvent> bttnEvents =
148174
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION);
149-
150-
Binding<String> binding = JavaFxSubscriber.toBinding(bttnEvents.map(e -> 1).scan(0,(x, y) -> x + y)
151-
.map(Object::toString));
175+
176+
Observable<String> accumulations = bttnEvents.map(e -> 1)
177+
.scan(0,(x, y) -> x + y)
178+
.map(Object::toString);
179+
180+
Binding<String> binding = JavaFxSubscriber.toBinding(accumulations);
152181

153182
incrementLabel.textProperty().bind(binding);
154183

@@ -157,6 +186,19 @@ binding.dispose();
157186

158187
```
159188

189+
You also have the option to use a `CompositeBinding` to group multiple `Binding`s together, and `dispose()` them all at once. It is the JavaFX equivalent to `CompositeSubscription`.
190+
191+
```java
192+
Binding<Long> binding1 = ...
193+
bindings.add(binding1);
194+
195+
Binding<Long> binding2 = ...
196+
bindings.add(binding2);
197+
198+
//do stuff on UI, and dispose() both bindings
199+
bindings.dispose();
200+
```
201+
160202
### JavaFX Scheduler
161203

162204
When you update any JavaFX control, it must be done on the JavaFX Event Dispatch Thread. Fortunately, the `JavaFxScheduler` makes it trivial to take work off the JavaFX thread and put it back when the results are ready. Below we can use the `observeOn()` to pass text value emissions to a computation thread where the text will be flipped. Then we can pass `JavaFxScheduler.getInstance()` to another `observeOn()` afterwards to put it back on the JavaFX thread. From there it will update the `flippedTextLabel`.
@@ -181,29 +223,8 @@ Although ReactFX has some asynchronous operators like `threadBridge`, ReactFX em
181223

182224
If you are heavily dependent on RxJava, asynchronous processing, or do not want your entire reactive codebase to be UI-focused, you will probably want to use RxJavaFX.
183225

184-
185-
186226
##Notes for Kotlin
187-
If you are building your JavaFX application with [Kotlin](https://kotlinlang.org/), this library becomes even more useful with extension functions. These extension functions exist in the [RxKotlinFX](https://github.com/thomasnield/RxKotlinFX) project, but the API is so small it is not worth publishing at the moment. But you can simply add these extension functions below to your project and utilize Kotlin's fluent style.
188-
189-
```kotlin
190-
fun <T> Observable<T>.toBinding() = JavaFxSubscriber.toBinding(this)
191-
fun <T> Observable<T>.toBinding(errorHandler: (Throwable) -> Unit) = JavaFxSubscriber.toBinding(this,errorHandler)
192-
fun <T> ObservableValue<T>.toObservable() = JavaFxObservable.fromObservableValue(this)
193-
fun <T> ObservableValue<T>.toObservableChanges() = JavaFxObservable.fromObservableValueChanges(this)
194-
fun <T: Event> Node.toNodeEvents(eventType: EventType<T>) = JavaFxObservable.fromNodeEvents(this, eventType)
195-
fun <T> Observable<T>.observeOnFx() = this.observeOn(JavaFxScheduler.getInstance())
196-
```
197-
This allows you to better use Kotlin's features to interop JavaFX and RxJava much more cleanly.
198-
199-
```kotlin
200-
val textField = TextField()
201-
val textInputs = textField.toObservable()
202-
val lengthBinding = textInputs.map { it.length }.toBinding()
203-
```
204-
205-
If you are doing JavaFX and Kotlin development, definitely check out [TornadoFX](https://github.com/edvin/tornadofx) as well to utilize type-safe builders and other features enabled by Kotlin.
206-
227+
If you are building your JavaFX application with [Kotlin](https://kotlinlang.org/), check out [RxKotlinFX](https://github.com/thomasnield/RxKotlinFX) to leverage this library through Kotlin extension functions.
207228

208229
## Comprehensive Example
209230
```java

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.0.3
1+
version=0.0.5

src/main/java/rx/javafx/sources/ActionEventSource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
import javafx.event.ActionEvent;

src/main/java/rx/javafx/sources/Change.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
public final class Change<T> {

src/main/java/rx/javafx/sources/Flag.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
public enum Flag {

src/main/java/rx/javafx/sources/ListChange.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
/**

src/main/java/rx/javafx/sources/NodeEventSource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
import javafx.event.Event;

src/main/java/rx/javafx/sources/ObservableListSource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
import javafx.collections.ListChangeListener;

src/main/java/rx/javafx/sources/ObservableValueSource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
import javafx.beans.value.ChangeListener;

src/main/java/rx/javafx/sources/SceneEventSource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.javafx.sources;
217

318
import javafx.event.Event;

0 commit comments

Comments
 (0)