ReactiveX / RxSwing

RxJava bindings for Swing
Apache License 2.0
98 stars 23 forks source link

RxSwing Sample applications? #13

Closed Petikoch closed 9 years ago

Petikoch commented 9 years ago

Hi there!

Are there any Java Swing sample applications showing the usage of RxSwing? Or real world applications?

I'm thinking especially of examples with "slow" backends, where the calls to the backend are executed async (sep. thread) and optionally dropped/aborted/merged.

E.g. a user is changing wildly the selection in a list / table in the sample application, firing off calls to the "slow" backend. Think of issues like async-calls bypassing each other, calls queueing up...

Best regards, Peti

epb-644 commented 9 years ago

Hi Peti! Sorry, I intend to add some examples to the wiki in the near future. I believe @samuelgruetter has some examples at: https://github.com/samuelgruetter/rx-playground

Personally, I use rx all the time for slow IO calls, just like what you describe. Typically what I'll do is run a slow task on a background thread via Rx's IO scheduler, then kick off a swing task on completion using the SwingScheduler.

Petikoch commented 9 years ago

@eddieburns55 , thanks for the fast reply!

Petikoch commented 9 years ago

Hi @eddieburns55 and @samuelgruetter ,

I published some RxSwing examples: https://github.com/Petikoch/Java_MVVM_with_Swing_and_RxJava_Examples

Because there are "few" RxSwing examples "out there", I wanted to share my examples with you.

Best regards from Lucerne in Switzerland, Peti

epb-644 commented 9 years ago

Thanks for for this impressive guide, Peti! I'll link it to our readme.

mikebaum commented 9 years ago

@Petikoch @eddieburns55

Impressive guide Peti. Funny, I have also moved towards the MVVM architecture, since it seems to work well with RxJava/RxSwing. Property binding is for sure the way to go.

In fact I have created a lot of code already to support MVVM using RxJava and RxSwing. At this time it has been committed at my work, however in the future I will try to open source it. Just need to figure out internally how to go about that best. That said, I can discuss the design, so I'd be interested in helping to develop a MVVM toolkit for Swing.

As I can see from your examples repo, you have been reading the same articles as I have since I recognize the MVVM diagram you posted. Here's some thoughts I have around the model properties, those which you described as using BehaviorSubjects for. I chose to create a Property class, which is backed by a BehaviorSubject. The reason I chose not to use BehaviorSubjects directly are the following: 1) Care must be taken when binding Subjects, since in my opinion you are only really interested in propagating onNext events. onError events and onComplete events can complete bound properties if your not careful. 2) Reentrancy - If you create binding loops, you can end up with reentrancy which could cause problems. 3) Property Api - Introducing a new type means that we can add all the methods directly to that class and not have to use a separate class to setup the bindings.

This past weekend I got possessed to create an updated version of what I've committed at work already, so here's the api. For compactness, I merged all the methods into one interface:

public interface ReactiveProperty<M>
{
    void destroy();

    Observable<Boolean> isDestroyed();

    void reset();

    void setValue( M value );

    M getValue();

    M getInitialValue();

    PropertyId<M> getId();

    Observable<Boolean> hasChanges();

    boolean hasSubscribers();

    default Subscription bindTo( Observable<M> source ) {  <<implementation left out>> }

    <P extends Observable<M> & ReactiveProperty<M>> Subscription synchronizeWith( P property );
}

And the only implementation of ReactiveProperty I have so far extends Observable...

public class ObservableProperty<M> extends Observable<M> implements ReactiveProperty<M>
{
    <<implementation left out>>
}

So in essence an ObservableProperty is a kind of Subject specialized for GUI development.

As I'm sure you are already aware, there exists ReactiveUI for Microsoft and ReactiveCocoa for Apple, so maybe there is already a lot of examples and code on how a desktop MVVM library could look.

Anyway, maybe RxSwing is not the right place for this type of code, but for sure there should exist some project somewhere.

Sorry for the long post. :)

Petikoch commented 9 years ago

Hi @mikebaum ,

thank you for the feedback to my MVVM examples.

I also thought about "extracting" some code to build something like a "MVVM4J" library and to build somehting like a "property" class for the ViewModel fields to have something more UI-domain'sh than a vanilla BehaviourSubject. The "killer" features of that property class are IMO the "dirty flag" (like you already have) and undo/redo, beside the binding capabilities.

I'm not sure when or if I will start with something like the "MVVM4J" library (ReactiveSwing? that raises huge expectations), but there will be definitively something like this for the JVM in the future.

What do you think?`

Best regards, Peti

mikebaum commented 9 years ago

@Petikoch If I was asked to select between the names you suggest, I think I'd prefer ReactiveSwing. That name is less specific and would probably be more easy to find when searching google. Also, it opens the door for having an architecture that is not necessarily pure MVVM, if the need presents itself.

Undo/Redo, that's a great idea. I imagine that there would be need for a hierarchy of Property classes, perhaps an UndoableProperty could be one. I say this since, perhaps not every property should (or would need to) be undoable.

@eddieburns55 Am I right to assume that this type of code does not belong in the RxSwing project? If this assumption is incorrect, then perhaps the RxSwing project is the right place. Also, I know that RxJava and consequently RxSwing must be backwards compatible to Java 1.6. I wonder though if this is necessarily true for RxSwing, since if I understand correctly the motivation for this is to support Android. So my question is, can RxSwing drop this requirement, considering Android does not support Swing, and be baselined to java 1.8?