https://github.com/agrison/redux4j
Java implementation of redux with Vavr
https://github.com/agrison/redux4j
java redux vavr
Last synced: 6 months ago
JSON representation
Java implementation of redux with Vavr
- Host: GitHub
- URL: https://github.com/agrison/redux4j
- Owner: agrison
- License: mit
- Created: 2016-10-16T21:21:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-05-20T20:51:46.000Z (over 3 years ago)
- Last Synced: 2024-08-22T22:22:31.413Z (over 1 year ago)
- Topics: java, redux, vavr
- Language: Java
- Homepage:
- Size: 44.9 KB
- Stars: 38
- Watchers: 5
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Redux in Java

[](https://codecov.io/gh/agrison/redux4j)
Using Java and Vavr.
## Features
* Store
* Reducer
* CombineReducers
* Middlewares
## Install
```xml
me.grison
redux4j
1.0
```
## Counter example
```java
public class Counter {
// Actions
static final String INC = "INC";
static final String DEC = "DEC";
// this is our reducer which increments if INC, decrement if DEC
// and does nothing otherwise
final Reducer reducer =
(action, state) -> state + switch (action) {
case INC -> 1;
case DEC -> -1;
default -> 0;
};
public void foo() {
// This is our store with its initial state of zero and the reducer seen above
Store store = Redux.createStore(0, reducer);
// dispatch an INC action
store.dispatch(INC);
store.getState(); // 1
// dispatch an DEC action
store.dispatch(DEC);
store.getState(); // 0
}
}
```
## Middlewares
```java
public class Counter {
final Middleware middleware = (store, action, next) -> {
System.out.println("Before " + store.getState());
next.accept(store, action, null);
System.out.println("After " + store.getState());
};
public void foo() {
Store store = Redux.createStore(0, reducer, middleware);
store.dispatch(INC);
}
}
```
Outputs:
Before 0
After 1
## Combine Reducers
```java
public class Foo {
Reducer concatBar =
(action, state) -> "CONCAT".equals(action) ? state + "bar" : state;
Reducer plus2 =
(action, state) -> "PLUS".equals(action) ? state + 2 : state;
Reducer> addFoo = (action, state) -> {
if ("ADD".equals(action))
state.add("foo");
return state;
};
Reducer reducers = Redux.combineReducers(Tuple.of("str", concatBar), Tuple.of("int", plus2), Tuple.of("list", addFoo));
@Test
public void testCombineReducersMap() {
Map initialState = new HashMap() {{
put("str", "foo");
put("int", 1);
put("list", new ArrayList());
}};
Store, String> store = Redux.createStore(initialState, reducers);
assertThat(store.getState().get("str"), equalTo("foo"));
store.dispatch("CONCAT");
assertThat(store.getState().get("str"), equalTo("foobar"));
assertThat(store.getState().get("int"), equalTo(1));
store.dispatch("PLUS");
assertThat(store.getState().get("int"), equalTo(3));
store.dispatch("PLUS");
assertThat(store.getState().get("int"), equalTo(5));
store.dispatch("CONCAT");
assertThat(store.getState().get("str"), equalTo("foobarbar"));
assertThat(store.getState().get("list"), equalTo(Arrays.asList()));
store.dispatch("ADD");
assertThat(store.getState().get("list"), equalTo(Arrays.asList("foo")));
store.dispatch("ADD");
assertThat(store.getState().get("list"), equalTo(Arrays.asList("foo", "foo")));
}
// same with a javabean with getters/setters
}
```