Last active
March 22, 2016 20:46
-
-
Save l0gicgate/ed2b831a86d13f941a98 to your computer and use it in GitHub Desktop.
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
export default class ReducerChain { | |
constructor(reducers) { | |
this.reducers = reducers; | |
if (!Array.isArray(reducers)) { | |
throw new Error('To create a reducer chain you must pass in an array of functions.'); | |
} | |
return this.reducer; | |
} | |
reducer = (state, action) => { | |
let result = state; | |
for (let i = 0, len = this.reducers.length; i < len; i++) { | |
result = this.reducers[i](result, action); | |
} | |
return result; | |
}; | |
} |
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
import ReducerChain from './ReducerChain'; | |
const SOME_ACTION = 'SOME_ACTION'; | |
const SOME_OTHER_ACTION = 'SOME_OTHER_ACTION'; | |
const initialState = { | |
someProp: 'someValue', | |
someOtherProp: 'someOtherValue', | |
} | |
function reducer1 = function (state, action) { | |
switch (action.type) { | |
case SOME_ACTION: | |
return Object.assign({}, state, { | |
someProp: 'someNewValue', | |
}); | |
default: | |
return state; | |
} | |
} | |
function reducer2 = function (state, action) { | |
switch (action.type) { | |
case SOME_OTHER_ACTION: | |
return Object.assign({}, state, { | |
someOtherProp: 'someOtherNewValue', | |
}); | |
default: | |
return state; | |
} | |
} | |
export default new ReducerChain([ | |
reducer1, | |
reducer2, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment