Skip to content

Commit 68f59d3

Browse files
author
Asaf Shakarzy
committed
createReducer utility
1 parent a654e7d commit 68f59d3

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# `redux-immutablejs`
22

3-
An alternative to [combineReducers](http://rackt.github.io/redux/docs/api/combineReducers.html) that supports
4-
[ImmutableJs](https://facebook.github.io/immutable-js/).
3+
Redux & Immutable integration
4+
5+
This is a small library that aims to provide integration tools between [Redux](https://github.com/rackt/redux)
6+
& [ImmutableJs](https://facebook.github.io/immutable-js/) that fully conforms Redux _actions_ & _reducers_ standards.
7+
8+
1. An alternative to [combineReducers](http://rackt.github.io/redux/docs/api/combineReducers.html) that supports
9+
[ImmutableJs](https://facebook.github.io/immutable-js/) for store initial state.
10+
1. An optional handler map reducer creator with immutable support.
11+
512

613
# Setup
714

@@ -23,6 +30,36 @@ const state = reducer(state);
2330
export default createStore(reducer, state);
2431
```
2532

33+
## Immutable Handler Map reducer creator
34+
35+
Using `createReducer` is an optional function that creates a reducer from a collection of handlers, except
36+
getting ride of the _switch_ statement, it also provides the following benefits:
37+
38+
1. If the given `initialState` type is mutated, it will get converted to an immutable type.
39+
1. An error is produced in case a reducer handler returns a mutated state (not recommended but this behavior can be disabled)
40+
41+
```js
42+
import { createReducer } from 'redux-immutablejs'
43+
const initialState = Immutable.fromJS({ isAuth: false })
44+
45+
/**
46+
* Reducer domain that handles authentication & authorization.
47+
**/
48+
export default createReducer(initialState, {
49+
[LOGIN] (state, action) {
50+
return state.merge({ 'isAuth': true, token: action.payload.token })
51+
},
52+
53+
[LOGOUT] (domain) {
54+
return domain.merge({ 'isAuth': false, 'current_identity': {}, token: undefined })
55+
}
56+
})
57+
```
58+
59+
60+
Please note that this is optional and `combineReducers` should work just fine if you prefer the old `switch` way.
61+
62+
2663
# FAQ
2764

2865
## How this library is different from 'redux-immutable' ?

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "redux-immutable",
2+
"name": "redux-immutablejs",
33
"version": "0.0.1",
44
"description": "Redux Immutable facilities",
55
"scripts": {
@@ -14,7 +14,7 @@
1414
"author": "Indexia Tech",
1515
"license": "BSD-3-Clause",
1616
"bugs": {
17-
"url": "https://github.com/indexiatech/redux-immutable/issues"
17+
"url": "https://github.com/indexiatech/redux-immutablejs/issues"
1818
},
1919
"homepage": "http://indexiatech.github.io/redux-immutable",
2020
"peerDependencies": {

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import combineReducers from './utils/combineReducers';
2+
import createReducer from './utils/createReducer';
23

34
export {
4-
combineReducers
5-
}
5+
combineReducers,
6+
createReducer
7+
};

src/utils/createReducer.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Immutable from 'immutable';
2+
3+
/**
4+
* Create a handler (action) map reducer for the given list of handlers
5+
*
6+
* @param {object} initialState The initial state of the reducer, expecting an Immutable.Iterable instance,
7+
* otherwise given initialState is converted to immutable.
8+
* @param {object} handlers A map of actions where key is action name and value is a reducer function
9+
* @param {boolean} enforceImmutable = true if to enforce immutable, in other words a TypeError is thrown in case
10+
* a handler returned anything that is not an Immutable.Iterable type.
11+
* @return {object} The calculated next state
12+
*/
13+
export default function createReducer(initialState, handlers, enforceImmutable = true) {
14+
return (state = initialState, action) => {
15+
// convert the initial state to immutable
16+
if (!Immutable.Iterable.isIterable) {
17+
state = Immutable.fromJS(state);
18+
}
19+
20+
const handler = (action && action.type) ? handlers[action.type] : undefined;
21+
22+
if (!handler) {
23+
return state;
24+
}
25+
26+
state = handler(state, action);
27+
28+
if (enforceImmutable && !Immutable.Iterable.isIterable(state)) {
29+
throw new TypeError('Reducers must return Immutable objects.');
30+
}
31+
32+
return state;
33+
};
34+
}

0 commit comments

Comments
 (0)