RxJava-based RxRedux is now open source
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!
According to the blog post announcing the decision, RxRedux drew inspiration from redux-observable, also known as a “middleware” for Redux. A 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:
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
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:
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
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
This is actually a function
(State, Action) -> State that takes the current
Action to compute a new
State. It’s important to know that all
Actions go through the Reducer.
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.
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.
SideEffect can trigger multiple
Actions which go through the
Reducer.However, it can also trigger other
SideEffects registered for the corresponding
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).