Giter Club home page Giter Club logo

Comments (4)

thomasnield avatar thomasnield commented on August 15, 2024

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.

thomasnield avatar thomasnield commented on August 15, 2024

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.

thomasnield avatar thomasnield commented on August 15, 2024

Please refer to PR #28 for API proposals.

from rxjavafx.

thomasnield avatar thomasnield commented on August 15, 2024

Closing this issue as this is now implementing, and pending for release in #28.

from rxjavafx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.