Giter VIP home page Giter VIP logo

reactorfx's Introduction

ReactorFX

Build Status codecov GitHub license GitHub forks GitHub issues Maven Central

This lightweight convenience library allows for simple integration between Project Reactor and JavaFX. ReactorFX provides fluent factories to create Flux for the propagation of events from JavaFX Controls, Collections, and Observables.

Usage

ReactorFX currently supports Java8+

compile "com.github.shadskii:reactorfx:2.0.0" //Java 8

Events

In JavaFX user interactions are propagated through Events. These Events can be emitted from Node, Scene, MenuItem, and Window. ReactorFX provides simple, fluent, and consistent factories for the creation of Fluxes from these sources. You can create Fluxes by using FxFlux.from() and passing the source and EventType to listen to. FxFlux.from() provides overloaded factories such that omitting the EventType will result in a Flux that listens for ActionEvents.

Events From A Control
Button btn = new Button("Hey I'm A Button!");
Flux<Event> buttonEvents = FxFlux.from(btn)
                                 .subscribeOn(FxSchedulers.fxThread())
                                 .publishOn(anotherScheduler);
Events From A Scene
Scene scene = new Scene(new Label("Hey I'm A Label!"));
Flux<MouseEvent> mouseEvents = FxFlux.from(scene, MouseEvent.MOUSE_CLICKED)
                                 .subscribeOn(FxSchedulers.fxThread())
                                 .publishOn(anotherScheduler);
Events From A Window
Flux<WindowEvent> windowEvents = FxFlux.from(primaryStage, WindowEvent.WINDOW_HIDING)
                                 .subscribeOn(FxSchedulers.fxThread())
                                 .publishOn(anotherScheduler);

ObservableValue

Updates of any JavaFX ObservableValue can be emitted onto a Flux by using the factory FxFlux.from(ObservableValue<T> observableValue) which creates a Flux that emits the initial value of the observable followed by any subsequent changes to the Observable. Often the initial value of an ObservableValue is null. The reactive streams specification disallows null values in a sequence so these null values are not emitted.

SimpleObjectProperty<String> observable = new SimpleObjectProperty<>();
Flux<String> flux = FxFlux.from(observable); 

Changes from an ObservableValue can also be emitted as a Change which is a pairing of the old value and the new value. This Flux can be produced from the factory FxFlux.fromChangesOf(ObservableValue<T> observableValue).

SimpleObjectProperty<String> observable = new SimpleObjectProperty<>();
Flux<Change<String>> flux = FxFlux.fromChangesOf(observable)
                                  .filter(change -> "Hello".equals(change.getOldValue()))
                                  .filter(change -> "World".equals(change.getNewValue()));

JavaFX Scheduler

JavaFX controls are required to be updated on the JavaFX Application Thread. FxSchedulers.fxThread() is a Scheduler that provides a way to easily the JavaFX Application Thread. Using this scheduler makes it possible to listen to JavaFX controls using Reactive Streams.

ProgressBar p1 = new ProgressBar();

Flux.interval(Duration.ofMillis(1000))
    .map(l -> l/100.0)
    .publishOn(FxSchedulers.fxThread())
    .subscribe(p1::setProgress);

JavaFX Collections Support

ReactorFX also provides fluent factories for creating a Flux from any JavaFX Collection by four overloaded factory methods.

from()

Using this factory will produce a Flux that emits the argument JavaFX Collection whenever it has been changed.

fromAdditionsOf()

Using this factory produces a Flux that emits any element added to the argument collection after it has been added.

fromRemovalsOf()

Using this factory produces a Flux that emits any element removed from the argument collection whenever it has been removed.

fromChangesOf()

Using this factory produces a Flux that emits a Change element as soon as any change (add/remove/modify) is performed on the underlying collection. If you want access to the actual underlying event from JavaFx collection, use this

The below factory method is provided for ObservableArray and it emits the changed sub-array of the argument array whenever it has been changed

fromChangedSubArrayOf()

Collections

Licensed under Apache Software License 2.0

reactorfx's People

Contributors

shadskii avatar thekalinga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

reactorfx's Issues

Missing 's' in dependency for gradle ;-)

Dear shadskii,

thanks a lot for this great tool! I just discovered a minor thing:
in your README.md there is a missing 's' in the group-id:

compile "com.github.shadkii:reactorfx:1.8.0" //Java 8

... I was just struggling a bit, because I was simply copying this line ;-)
Greetings

Scheduler naming convention.

In the class FxSchedulers, recommend renaming the function platform to fxThread. It will read better when used in a static import and will likely hold meaning even if you change the the internal implementation.

discution

hi

i have a simple senaio: i have a loop with (1000 iteration) and just i want to launch this loop aync, son i use jfxdialog, i launch this senaio in Task, onrunning event i show my jfxdialog and onsuccess or onfaild i close it, i see that jfxdialog is show after loop terminate !!! however i show it in onrunning event (normaly it show before loop start) , any explanation please

Allow users to get `Change` object when subscribing to `Observable*` collections

Currently, this library consumes the most important information about the changes that happened in the underlying library

For example, if I have a dynamic tree whose children can be added & removed dynamically & I want to expose this information to a TreeView, each node needs to know what changes happened to the child ObservableLists & in what position. This position information can never be accessed because the library swallows this information & only emits a subset of the this important information

For eg., for ObservableList currently I can only get

  1. A new ObservableList via Flux<ObservableList<T>> from(ObservableList<T> source)
  2. Only the added elements via Flux<T> fromAdditionsOf(ObservableList<T> source)
  3. Only removed elements via Flux<T> fromRemovalsOf(ObservableList<T> source)

Instead the user should get the full power of underlying *Change (ListChangeListener.Change incase of ObservableList) objects. This way user will know what happened & most importantly where in the underlying child Observable*

This has to be applied to every Observable* collection as the library should never hinder the user from getting the underlying raw information if the user needs it as my above example demonstrates it

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.