Created
August 4, 2015 19:30
-
-
Save quicksnap/00e5d816b9ce070cf80f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Create reducer | |
| * @flow | |
| */ | |
| // SOURCE: https://github.com/quangbuule/redux-example/blob/redux%40v1.0.0-rc/src/js/lib/createReducer.js | |
| import Immutable, { Map, List } from 'immutable' | |
| export default function createReducer (initialState, handlers) { | |
| const StateConstructor = initialState.constructor | |
| return (state = initialState, action) => { | |
| if (!Map.isMap(state) && !List.isList(state) && !(state instanceof StateConstructor)) { | |
| state = Immutable.fromJS(state) | |
| } | |
| const handler = handlers[action.type] | |
| if (!handler) { | |
| return state | |
| } | |
| state = handler(state, action) | |
| if (!Map.isMap(state) && !List.isList(state) && !(state instanceof StateConstructor)) { | |
| throw new TypeError('Reducers must return Immutable objects.') | |
| } | |
| return state | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment