-
-
Save barbagrigia/c66973b2297ab651b101126c7bf37e16 to your computer and use it in GitHub Desktop.
Redux reducer enhancer to store specific control instance state by key
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
/** | |
* Use this reducer enhancer to store specific control instance state by key. | |
* The key will be resolved using the controlInstanceKeyResolver function parmeter which defaults to use the controlInstanceKey member of the action's meta object (i.e action.meta.controlInstanceKey) | |
* If the key is not a string then the action will be ignored and will not pass to the enhanched reducer. | |
* @param {function} reducer - the reducer to enhance | |
* @param {function} controlInstanceKeyResolver - an optional function to get the instance key from the action | |
*/ | |
export function instanceMapReducerEnhancer( | |
reducer: Redux.Reducer, | |
controlInstanceKeyResolver: ((action) => string) = defaultKeyResolver) { | |
return function (state = {}, action) { | |
const instanceKey = controlInstanceKeyResolver(action); | |
if (typeof (instanceKey) === "string") { | |
let instanceState = reducer(state[instanceKey], action); | |
const newState = Object.assign({}, state, { [instanceKey]: instanceState }); | |
return newState | |
} else { | |
return state; | |
} | |
} | |
} | |
function defaultKeyResolver(action) { | |
return action.meta ? action.meta.controlInstanceKey : undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment