Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 4.1 KB

README.md

File metadata and controls

79 lines (62 loc) · 4.1 KB

DurianRx: Reactive getters, powered by RxJava and ListenableFuture

Maven artifact Latest version Javadoc License

Changelog Travis CI

DurianRx unifies RxJava's Observable with Guava's ListenableFuture.

Observable<SomeType> observable = someObservable();
ListenableFuture<SomeType> future = someFuture();
Rx.subscribe(observable, val -> doSomething(val));
Rx.subscribe(future, val -> doSomething(val));

It also provides reactive getters, a simple abstraction for piping data which allows access via T get() or Observable<T> asObservable().

RxBox<Point> mousePos = RxBox.of(new Point(0, 0));
this.addMouseListener(e -> mousePos.set(new Point(e.x, e.y)));

Rectangle hotSpot = new Rectangle(0, 0, 10, 10)
RxGetter<Boolean> isMouseOver = mousePos.map(hotSpot::contains);

Debugging an error which involves lots of callbacks can be difficult. To make this easier, DurianRx includes a tracing capability, which makes this task easier.

// anytime an error is thrown in an Rx callback, the stack trace of the error
// will be wrapped by the stack trace of the original subscription
DurianPlugins.set(RxTracingPolicy.class, new LogSubscriptionTrace()).

Lastly, DurianRx provides convenience classes for manipulating Guava's immutable collections inside reactive containers, such as RxSet<T> extends RxBox<ImmutableSet<T>>, which can be used as such:

public void mouseClicked(MouseEvent e) {
	rxMouseOver.get().ifPresent(cell -> {
		Set<Integer> currentSelection = rxSelection.get();
		if (e.isControlDown()) {
			// control => toggle mouseOver item in selection
			if (currentSelection.contains(cell)) {
				rxSelection.remove(cell);
			} else {
				rxSelection.add(cell);
			}
		} else {
			// no control => set selection to mouseOver
			rxSelection.set(Collections.singleton(cell));
		}
	});
}

...

Rx.subscribe(rxSelection, set -> {
	// take some action in response to
	// selection change
});

Perhaps most useful of all is the Immutables utility class, which helps with all kinds of manipulations of Guava's immmutable collections.

DurianRx's only requirements are Guava, RxJava, and Durian.

Acknowledgements