Comments (4)
Yeah, some of these applications could be really helpful. I put out a new branch called fx_collections_interop
for anyone that is interested.
But here are simple factories that emits additions and removals from an ObservableList
.
public final class ObservableListSource {
public static <T> Observable<T> fromObservableListAdds(final ObservableList<T> source) {
return Observable.create((Observable.OnSubscribe<T>) subscriber -> {
source.addListener((ListChangeListener<T>) c -> {
while (c.next()) {
if (c.wasAdded()) {
c.getAddedSubList().forEach(subscriber::onNext);
}
}
});
}).subscribeOn(JavaFxScheduler.getInstance());
}
public static <T> Observable<T> fromObservableListRemovals(final ObservableList<T> source) {
return Observable.create((Observable.OnSubscribe<T>) subscriber -> {
source.addListener((ListChangeListener<T>) c -> {
while (c.next()) {
if (c.wasRemoved()) {
c.getRemoved().forEach(subscriber::onNext);
}
}
});
}).subscribeOn(JavaFxScheduler.getInstance());
}
}
Unit Test
@Test
public void testObservableList() {
JFXPanel panel = new JFXPanel();
ObservableList<String> items = FXCollections.observableArrayList();
ObservableListSource.fromObservableListAdds(items).subscribe(v -> System.out.println("ADDED: " + v));
ObservableListSource.fromObservableListRemovals(items).subscribe(v -> System.out.println("REMOVED: " + v));
items.add("ABQ");
items.add("HOU");
items.setAll("LAX","PHX","FLL");
}
OUTPUT
ADDED: ABQ
ADDED: HOU
ADDED: LAX
ADDED: PHX
ADDED: FLL
REMOVED: ABQ
REMOVED: HOU
from rxjavafx.
Yeah, actually a lot of new utility can be created here. For example, here is a factory that will only emit items when they are distinctly added to the list for the first time, or when the item is removed and there are no dupes remaining.
We could use this to create a DistinctObservableList
implementation that is driven off the distinct values of another ObservableList
. For business intelligence applications, this can be pretty invaluable. We can overload to map a lambda for an alternative property to use for distinction.
@Test
public void testObservableList() {
JFXPanel panel = new JFXPanel();
Platform.runLater(() -> {
ObservableList<String> items = FXCollections.observableArrayList();
ObservableListSource.fromObservableListDistinctChanges(items)
.subscribe(System.out::println, Throwable::printStackTrace);
items.add("ABQ");
items.add("HOU");
items.add("CHI");
items.add("ABQ");
items.add("HOU");
items.remove("ABQ");
items.remove("CHI");
items.remove("ABQ");
items.add("ABQ");
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
OUTPUT
ADDED ABQ
ADDED HOU
ADDED CHI
REMOVED CHI
REMOVED ABQ
ADDED ABQ
from rxjavafx.
Please refer to PR #28 for API proposals.
from rxjavafx.
Closing this issue as this is now implementing, and pending for release in #28.
from rxjavafx.
Related Issues (20)
- Ebook Learning RxJava with JavaFX not navigatable HOT 3
- Additional Transformer factories HOT 1
- Getting index in ListChange HOT 1
- Help with Deployment of 2.11 HOT 20
- Java 11 Automatic-Module-Name HOT 2
- JavaFxObservable.valuesOf(fxObservable, nullSentinel) does not emmit first nullSentinel HOT 1
- JavaFxObservable.valuesOf() and Bindings.bindBidirectional() strange behaviour. HOT 3
- JavaFxObservable.actionEventsOf().subscribeOn(Schedulers.newThread()) misses some emissions HOT 3
- 2.11.0-RC16 / error: cannot access JavaFxObserver HOT 4
- Mockito complaining with Java 11/Gradle 5 HOT 1
- No factories should subscribeOn() the Platform thread
- JavaFxObserver#onError: custom implementation HOT 1
- Need help with maintenance, call for collaborators HOT 9
- Support scene accelerators HOT 4
- Change object not emitted when observed property had changed to null
- A typo on Gitbook guide
- It looks like passing all the tests with openjfx 13.0.1 HOT 2
- Error - cannot access io.reactivex.Observable HOT 1
- Is there a plan to use RXJava 3 ? HOT 2
- Setting up a new build chain for stable/nightly releases.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxjavafx.