forked from indexiatech/redux-immutablejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Asaf Shakarzy
committed
Sep 4, 2015
1 parent
a654e7d
commit 68f59d3
Showing
4 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import combineReducers from './utils/combineReducers'; | ||
import createReducer from './utils/createReducer'; | ||
|
||
export { | ||
combineReducers | ||
} | ||
combineReducers, | ||
createReducer | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Immutable from 'immutable'; | ||
|
||
/** | ||
* Create a handler (action) map reducer for the given list of handlers | ||
* | ||
* @param {object} initialState The initial state of the reducer, expecting an Immutable.Iterable instance, | ||
* otherwise given initialState is converted to immutable. | ||
* @param {object} handlers A map of actions where key is action name and value is a reducer function | ||
* @param {boolean} enforceImmutable = true if to enforce immutable, in other words a TypeError is thrown in case | ||
* a handler returned anything that is not an Immutable.Iterable type. | ||
* @return {object} The calculated next state | ||
*/ | ||
export default function createReducer(initialState, handlers, enforceImmutable = true) { | ||
return (state = initialState, action) => { | ||
// convert the initial state to immutable | ||
if (!Immutable.Iterable.isIterable) { | ||
state = Immutable.fromJS(state); | ||
} | ||
|
||
const handler = (action && action.type) ? handlers[action.type] : undefined; | ||
|
||
if (!handler) { | ||
return state; | ||
} | ||
|
||
state = handler(state, action); | ||
|
||
if (enforceImmutable && !Immutable.Iterable.isIterable(state)) { | ||
throw new TypeError('Reducers must return Immutable objects.'); | ||
} | ||
|
||
return state; | ||
}; | ||
} |