days
-6
-3
hours
-1
-6
minutes
-5
-4
seconds
-3
-9
search
Have we mentioned it's written *entirely* in Kotlin?

RxJava-based RxRedux is now open source

Gabriela Motroc
RxJava
© Shutterstock / nikkytok

If you’re already using RxJava, RxRedux might come in handy. And by the way, it’s now open source. Let’s have a closer look at it!

RxRedux is a RxJava-based Redux implementation by Freeletics. Its goal is to help isolate side effects and it is billed as a replacement for RxJava’s .scan() operator. And now it’s open source!

According to the blog post announcing the decision, RxRedux drew inspiration from redux-observable, also known as a “middleware” for ReduxA middleware is a way to deal with async Actions and this is where RxRedux comes in handy. Not only is RxRedux based entirely on RxJava but one can actually consider it a RxJava operator: .reduxStore()

The difference between RxRedux and other Redux-inspired libraries out there (such as Mobius), this library is backed on top of RxJava. It offers a custom RxJava operator .reduxStore( initialState, sideEffects, reducer ) and treats upstream events as Actions.

RxRedux: Everything you need to know

If you want to make a pie, you need all the ingredients at hand and this situation is no different. Here’s what you need to know:

ReduxStore

A Store is an observable container for state. RxRedux offers an extension function .reduxStore<State, Action>(initialState, sideEffects, reducer) for RxJava’s Observable<T> to create such a container for state. It takes an initialState and a list of SideEffect<State, Action> and a Reducer<State, Action>.

Action

This term refers to a command to “do something” in the Store. An Action can be triggered by the app’s user but it can also be a system notification such as changes in the battery level of the user’s device. An Action can also be triggered by a SideEffect.

Reducer

This is actually a function (State, Action) -> State that takes the current Stateand an Action to compute a new State. It’s important to know that all Actions go through the Reducer.

The Reducer can ignore an Action by simply returning the previous state (which means the state has not changed).  SideEffects are usually “in charge” of the ignored Actions.

Side Effect

A Side Effect is a function of type (Observable<Action>, StateAccessor<State>) -> Observable<Action>. You can think of it as a use case in clean architecture: It should do just one job.

Every SideEffect can trigger multiple Actions which go through the Reducer.However, it can also trigger other SideEffects registered for the corresponding Action.

StateAccessor

When a SideEffect needs to know the current State of the Redux Store, it can use StateAccessor to grab the latest state from Redux Store. In short, StateAccessor is just a function () -> State (think fun getState() : State).

If you’re interested in learning how you can use RxRedux to create a simple Android application which displays a list of popular Github repositories, check out this blog post.

If you’re interested in the FAQs, this is where you’ll find them. 

Author
Gabriela Motroc
Gabriela Motroc is editor of JAXenter.com and JAX Magazine. Before working at Software & Support Media Group, she studied International Communication Management at the Hague University of Applied Sciences.