Coder Social home page Coder Social logo

reactive-gwt's Introduction

Reactive GWT

This library supports reactive programming in a way that integrates nicely with Google Web Toolkit.

AbstractHasValue<T>

The standard GWT way to implement HasValue is to extend Widget (usually Composite), which provides fireEvent(GwtEvent<?>):void (required by HasHandlers, a supertype of HasValue). If you want to design a widget that has more than one piece of observable state, you're out of luck, because your widget can only implement HasValue once.

// Sorry, no can do.
class StatefulWidget extends Composite
    implements HasValue<Integer>,
               HasValue<String> {

An alternative is to let the HasValue implementations be components of your widget:

class StatefulWidget extends Composite {
  HasValue<Integer> getIntegerHasValue() { ... }
  HasValue<String> getStringHasValue() { ... }

Reactive GWT provides AbstractHasValue to use for these HasValue implementations. AbstractHasValue also implements Signal.

Signal<T>

A Signal is a variable observable value. Signal<T> extends:

  • Source<T> (for retrieving the signal's current value)
  • HasValueChangeHandlers<T> (for observing changes to the signal's value)

This is very similar to HasValue. The difference is that the Signal interface does not not have methods that mutate its state. This allows us to derive signals from other signals. The main purpose of Reactive GWT is to support Signal composition, because it is an incredibly useful and intuitive concept.

Example 1: Boolean signal composition

Suppose you have a CheckBox, a list of TextBox widgets, and a Label. The label needs to be visible only when the checkbox is checked and at least one of the text boxes is nonempty. This can be expressed naturally by declaring the label's visibility change as a reaction to the composite state of the other widgets:

// Represent the checkbox's value as a boolean signal
Signal<Boolean> boxIsChecked = Signals.valueOf(checkBox);

// Create a signal indicating whether each text box is nonempty, then
// OR them to get a signal that is true when any of them are nonempty
Signal<Boolean> anyTextBoxesAreFilled = Signals.or(Signals.transform(
  Signals.valueOf(textBoxes), Functions.not(Predicates.emptyString)));

// Declare that the label is visible exactly when both of these signals are true
Signal<Boolean> labelIsVisible = Signals.and(boxIsChecked, anyTextBoxesAreFilled);
Signals.connect(labelIsVisible, Sinks.setVisible(label));

Example 2: String signal composition

Example 1 demonstrated some of the commonly-used composition operations directly supported by the Signals class (such as and/or). You can also merge signals using arbitrary functions. Suppose you want the label to always display the contents of the nonempty text boxes, separated by commas:

MergeFunction<String, String> mergeFunction = Functions.filterThenMerge(
  Functions.not(Predicates.emptyString), Functions.join(", "));
Signal<String> labelText = Signals.merge(Signals.valueOf(textBoxes), mergeFunction);
Signals.connect(labelText, Sinks.setText(label));

reactive-gwt's People

Contributors

chris-martin avatar

Stargazers

Juan Liska avatar Florian M. avatar Andrey avatar Federico Pugnali avatar Corey avatar Hilbrand Bouwkamp avatar Ignacio Baca Moreno-Torres avatar Taiga Tsutsumi avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

cgb-gwt

reactive-gwt's Issues

Write tests

The classes I put in the test directory are just there to hold example code so far. There should be both GWTTestCases and runnable example projects.

guava-gwt isn't available in Maven Central

The GWT build of Guava isn't currently in Maven Central, which might make reactive-gwt a little harder to actually use via Maven, since the guava-gwt JAR would have to be installed manually.

It's allegedly in the works to add it to Central in r10 (see http://code.google.com/p/guava-libraries/issues/detail?id=501). Until then, maybe we should redistribute it with reactive-gwt (or as a separate artifact), if we can, or at least provide some instructions on which artifacts to download and install locally manually.

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.